首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CoreBluetooth中枢->外周

CoreBluetooth中枢->外周
EN

Stack Overflow用户
提问于 2013-11-17 18:20:51
回答 2查看 4.9K关注 0票数 4

我对蓝牙通讯很陌生。我的第一个项目打算将数据从iOS设备传输到BLEshield (小型芯片)。

为了测试我的中央代码,我决定将一个iPhone设置为外围设备(一旦我得到它,芯片将扮演这个角色),并将一个iPad设置为Central。

我可以连接设备,也可以将数据从外设发送到中央。不过,这很容易:

代码语言:javascript
运行
复制
- (void)startService {
    _readChar = [[CBMutableCharacteristic alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
    _writeChar = [[CBMutableCharacteristics alloc] initWithType:[CBUUID ...] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsWriteable];

    _service = [[CBMutableService alloc] initWithType:[CBUUID ...] primary:YES];
    [_service setCharacteristics:@[_readChar, _writeChar]];

    _peripheral = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
    [_peripheral addService:_service];

    [_peripheral startAdvertising:@{CBAdvertisementDataServiceUUIDKey: @[[CBUUID ...]], CBAdvertisementDataLocalNameKey: @"ServiceName"}];
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
    [_peripheral updateValue:[@"HELLO WORLD" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_readChar onSubscribedCentrals:nil];
}

但我不能让另一个方向起作用。要从中央端发送数据,我有以下代码:

代码语言:javascript
运行
复制
[_activePeripheral writeValue:[@"PONG" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:_writeChar type:CBCharacteristicWriteWithoutResponse];

我假设这两种方法中的任何一种都应该在外围调用:

代码语言:javascript
运行
复制
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

但实际上什么都没发生。不幸的是,我的硬件项目将使用只能在外围模式工作的芯片,最后我将几乎完全写到外围设备上,因为它是控制信号的发送器。

我希望有人能帮我!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-17 21:55:40

以下各项的物业:

  • _readChar应该是CBCharacteristicPropertyRead
  • _writeChar应该是CBCharacteristicPropertyWriteWithoutResponse

有关更多详细信息,请参阅hereCBCharacteristicPropertyNotify不允许特征是可写的或可读的,它只是应该通知的(订阅/取消订阅)。

票数 6
EN

Stack Overflow用户

发布于 2014-02-15 10:18:27

只需使用https://github.com/LGBluetooth/LGBluetooth,它会使生活更轻松

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20034433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档