从 peripheral:didUpdateNotificationStateForCharacteristic 收到错误
Error received from peripheral:didUpdateNotificationStateForCharacteristic
我开发了一个蓝牙低功耗外设应用程序,它成功地与第二个 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];
不幸的是,这失败了,并且在委托方法 peripheral:didUpdateNotificationStateForCharacteristic 处收到错误(错误类型为 "unknown")。如果我在上面的代码中打印特征对象(aChar),我得到如下:
<CBCharacteristic: 0x1464f560, UUID = 2A06, properties = 0x2, value = (null), notifying = NO>
注意通知=否。如何设置外设使特性通知?
好的,我明白了。在外围端,需要设置特性的属性来启用通知。您可以使用 CBCharacteristicPropertyNotify 属性 执行此操作。例如,以下是您可以如何创建特征:
CBMutableCharacteristic *alertLevelCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:alertLevelCharacteristicUUID
properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify
value: nil permissions:CBAttributePermissionsReadable];
我开发了一个蓝牙低功耗外设应用程序,它成功地与第二个 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];
不幸的是,这失败了,并且在委托方法 peripheral:didUpdateNotificationStateForCharacteristic 处收到错误(错误类型为 "unknown")。如果我在上面的代码中打印特征对象(aChar),我得到如下:
<CBCharacteristic: 0x1464f560, UUID = 2A06, properties = 0x2, value = (null), notifying = NO>
注意通知=否。如何设置外设使特性通知?
好的,我明白了。在外围端,需要设置特性的属性来启用通知。您可以使用 CBCharacteristicPropertyNotify 属性 执行此操作。例如,以下是您可以如何创建特征:
CBMutableCharacteristic *alertLevelCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:alertLevelCharacteristicUUID
properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify
value: nil permissions:CBAttributePermissionsReadable];