我的 ble 外设要求我将十六进制值 0x0001 写入 CCCD 描述符以启用通知。当我尝试使用 .notify 属性将值写入特征时,出现以下错误:
Cannot invoke 'writeValue' with an argument list of type '(Data:String, for: CBCharacteristic)'
如何将值写入我的特征以便接收通知?
我尝试删除 .writeValue 行并仅保留 .setNotifyValue 行,但是当我从设备发送数据时,我没有收到通知。当我收到通知时,我是否必须读取该值?通知是否应该将值写入我的输出?
if characteristic.properties.contains(.notify) {
print("\(characteristic.uuid): properties contains .notify")
NUB5.writeValue(Data: "0x0001", for: characteristic)
NUB5.setNotifyValue(true, for: characteristic)
}
//BL652需要将0x0001的值写入TX CCCD来通知我们有数据。
最佳答案
setNotifyValue(true, for:characteristic)
应该足够了。 您是否实现了peripheral(_:didUpdateValueFor:error:)
方法?你能显示一下代码吗?writeValue
接受Data
输入,但您尝试传递String
。您需要对其进行编码,例如:
let encodedString = "Lorem ipsum".data(using: .utf8)
let number = 1
let encodedNumber = withUnsafeBytes(of: number) { Data($0) }
关于swift - .writeValue 不会让我将十六进制字符串写入特征以启用通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54543048/