我有一个 BLE 基金可以将数据写入外围服务:
func writeData(data: [Int8]) {
let character = CBMutableCharacteristic(type: cuivienenCharacterUUID, properties: [.Write, .Read, .Notify], value: nil, permissions: [.Readable, .Writeable])
test_peripheral.writeValue(NSData(bytes: data, length: data.count), forCharacteristic: character, type: .WithResponse)
test_peripheral.setNotifyValue(true, forCharacteristic: character)
}
我已经初始化了 test_peripheral 并将其委托(delegate)设置为 self。但是test_peripheral.writeValue
从未打过电话func peripheral(peripheral: CBPeripheral, didWriteValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)
。我连错误消息都收不到!
test_peripheral.setNotifyValue
也是如此。它从不调用func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?)
我的代码有什么问题吗?
最佳答案
CB特性
test_peripheral.writeValue(NSData(bytes: data, length: data.count), forCharacteristic: character, type: .WithResponse)
不应由您自己的代码初始化。您应该在
中找到您的 CBCharacteristicfunc peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)
然后使用它。
关于ios - CBPeripheral setNotifyValue WithResponse 不调用 didWriteValueForCharacteristic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34336949/