我开发了一款蓝牙低功耗外设应用程序,可成功连接第二个BLE中央应用程序。但是,我无法让中央应用程序订阅外围设备提供的服务特性之一的通知。在中央应用程序中,我有以下内容:
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if ([service.UUID isEqual:[CBUUID UUIDWithString:IMMEDIATE_ALERT_SERVICE_UUID]]) {
for (CBCharacteristic *aChar in service.characteristics) {
if ([aChar.UUID isEqual:[CBUUID UUIDWithString:ALERT_LEVEL_CHARACTERISTIC_UUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:aChar];
不幸的是,这失败并且在委托方法外围设备上收到错误:didUpdateNotificationStateForCharacteristic(错误类型为"未知")。如果我在上面的代码(aChar)中打印特征对象,我会得到以下结果:
<CBCharacteristic: 0x1464f560, UUID = 2A06, properties = 0x2, value = (null), notifying = NO>
请注意,通知=否。如何设置外设以使特性能够通知?
答案 0 :(得分:3)
好的,我明白了。在外围设备上,需要设置特性的属性以启用通知。您可以使用CBCharacteristicPropertyNotify属性执行此操作。例如,以下是如何创建特征:
CBMutableCharacteristic *alertLevelCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:alertLevelCharacteristicUUID
properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify
value: nil permissions:CBAttributePermissionsReadable];