简体   繁体   中英

CBPeripheral setNotifyValue WithResponse do not call didWriteValueForCharacteristic

I have a BLE fund to write data to a Peripheral service:

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)
}

I have already init test_peripheral and set its delegate to self. But test_peripheral.writeValue never called func peripheral(peripheral: CBPeripheral, didWriteValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) . I can't receive even a error message!

So does test_peripheral.setNotifyValue . It never invokes func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?)

What's wrong with my code?

CBCharacteristic in

test_peripheral.writeValue(NSData(bytes: data, length: data.count), forCharacteristic: character, type: .WithResponse)

should not be init by your own code. You should find your CBCharacteristic in

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)

and then use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM