CBPeripheral setNotifyValue WithResponse不会调用didWriteValueForCharacteristic

时间:2015-12-17 14:25:30

标签: ios swift core-bluetooth bluetooth-lowenergy

我有一个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并将其委托设置为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?)

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

中的CBC特征
test_peripheral.writeValue(NSData(bytes: data, length: data.count), forCharacteristic: character, type: .WithResponse)

不应该由您自己的代码初始化。你应该在

中找到你的CBC特色
func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)

然后使用它。