CoreBluetooth writeValue:forDescriptor:issue

时间:2012-11-26 08:31:13

标签: ios5 descriptor core-bluetooth

我的CoreBluetooth应用程序需要启用客户端特征配置描述符中的“指示位”。这是我做的:

  1. 开始扫描
  2. 开始连接设备
  3. 致电discoverServices
  4. 在回调中调用discoverCharacteristics

    - (void)peripheral:(CBPeripheral *)peripheral diddiscoverServices:(NSError *)error

  5. 在回调中调用discoverDescriptorsForCharacteristic

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

  6. 内部回调

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)特征错误:(NSError *)错误

  7. 我致电:

            if ( [[descriptor.UUID representativeString] isEqualToString:@"2902" ] )
            {
                const unsigned char raw_data[] = {0x02};
                NSData *myData = [NSData dataWithBytes: raw_data length: 2];
                [self.cBCP writeValue:myData forDescriptor:descriptor];
            }
    

    但我的应用程序在writeVale崩溃了:。控制台中的错误消息是:

      

    无法使用编写客户端特征配置描述符   这个方法!

    有什么想法吗?感谢

2 个答案:

答案 0 :(得分:1)

很老的问题,但由于没有回答,似乎方法setNotifyValue(_:for:)将为您处理,这取决于下面的charcateristics属性:

  • 仅通知:将启用通知。
  • 仅指示:将启用指示。
  • 指示&通知:仅启用通知。

因此,如果您需要启用指示,则该特征必须只具有指示属性,并且应删除通知。

我认为背后的主要原因是指示速度要慢得多,因此iOS总是更喜欢最快的选项,除非需要指明。

Read more on the Apple docs

答案 1 :(得分:-1)

问题是您不能像使用writeValue:forDescriptor:方法一样写入客户端配置描述符(UUID = 2902)的值。

相反,您应该使用CBPeripheral类的setNotifyValue:forCharacteristic:方法来配置客户端指示或服务器上特征值的通知。