【问题标题】:CoreBluetooth writeValue:forDescriptor: issueCoreBluetooth writeValue:forDescriptor: 问题
【发布时间】:2012-11-13 17:03:21
【问题描述】:

我的 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 *)characteristic error:(NSError *)error

我打电话给:

        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: 中崩溃。控制台中的错误信息是:

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

有什么想法吗?谢谢

【问题讨论】:

  • 我想知道是否有任何使用 writeVale:forDescriptor 的示例代码。
  • 其实我的解决方案是不使用 writeValue:forDescriptor。
  • 您能解释一下如何在不使用 writeValue:forDescriptor 的情况下启用指示吗?我被困在同一个地方......
  • 你有什么解决办法吗?

标签: ios5 descriptor core-bluetooth


【解决方案1】:

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

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

所以如果你需要启用indications,那么characteristic必须只有indicate属性,并且notify应该被移除。

我猜这背后的主要原因是指示要慢得多,所以 iOS 总是更喜欢最快的选项,除非需要指示。

Read more on the Apple docs

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-03-18
      • 1970-01-01
      • 2012-09-21
      • 2020-01-31
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      相关资源
      最近更新 更多