【问题标题】:How to writeValue forCharacteristic with response AND read response BLE如何使用响应和读取响应 BLE 来写值 forCharacteristic
【发布时间】:2017-04-24 01:11:14
【问题描述】:

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

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

最好有代码示例。

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

【问题讨论】:

  • 嘿,克里斯,你找到解决方案了吗,因为我也陷入了需要向 BLE 发送“P”以获得价值的问题。

标签: ios objective-c core-bluetooth


【解决方案1】:

这里是一些示例代码: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 来查看值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 2013-11-25
    相关资源
    最近更新 更多