1

我开发了一个蓝牙低功耗外围应用程序,它成功连接了第二个 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 收到了错误(错误类型为“未知”)。如果我在上面的代码(aChar)中打印特征对象,我会得到以下信息:

<CBCharacteristic: 0x1464f560, UUID = 2A06, properties = 0x2, value = (null), notifying = NO>

请注意,通知 = 否。如何设置外围设备以启用特性通知?

4

1 回答 1

3

好的,我想通了。在外围设备端,需要设置特性的属性以启用通知。您可以使用 CBCharacteristicPropertyNotify 属性执行此操作。例如,以下是创建特征的方式:

CBMutableCharacteristic *alertLevelCharacteristic = 
[[CBMutableCharacteristic alloc] initWithType:alertLevelCharacteristicUUID
                                   properties:CBCharacteristicPropertyRead | CBCharacteristicPropertyNotify
                                        value: nil permissions:CBAttributePermissionsReadable];
于 2015-01-24T21:01:58.823 回答