如何使用响应和读取响应BLE来编写具有特征的值

问题描述 投票:3回答:1

蓝牙制造商告诉我,我需要将以下内容发送到外围设备:'P'(0x50)

如何使用Objective-C执行此操作以及如何获得响应?

代码样本将是更可取的。

这几乎回答了我的问题,但没有提供任何代码示例:peripheral writeValue: forCharacteristic: type: return null error and value

ios objective-c core-bluetooth
1个回答
3
投票

以下是一些示例代码:https://code.tutsplus.com/tutorials/ios-7-sdk-core-bluetooth-practical-lesson--mobile-20741

要来回发送数据,这是一个多步骤的过程

  1. 订阅适当的外围设备,服务和特性。链接演练应该有所帮助。保持对外围设备和特性的参考。
  2. 调用函数[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]对于数据,您可以使用十六进制字符串创建NSData方法(https://opensource.apple.com/source/Security/Security-55471.14.18/libsecurity_transform/NSData+HexString.m)。
  3. 等待回调- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error一旦你得到回调你可以用[_peripheral readValueForCharacteristic:characteristic]读取外围设备
  4. 然后等待回调- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error,你可以看看characteristic.value来看看价值。
© www.soinside.com 2019 - 2024. All rights reserved.