Curnelious Curnelious - 1 year ago 72
iOS Question

Unable to send data to characteristic

I am able to get notified on the app when a remote device change the characteristic value, but when the app update the value back, the remote device won't see it.

So ,I know my characteristic is valid, i also know that other apps are able to write back to that characteristic (The actual device prints it)

So problem is of course in the app.

Whats more interesting is that every characteristic has handles , which have each its own UUID.

iOS will only let me see my characteristic UUID, no access to its handles, although there are 2 handles for each characteristic with different UUID'S

Anyway , here I get the callback and try to write back :

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
print("UPDATE FROM:" , characteristic) //good!
sendData(data: "check") //not good
if characteristic.uuid.uuidString == characteristicUUID {
if let str = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue)
print("BLE:GOT:",str ) Notification.Name(rawValue: "Bluetooth"), object: str)

and writing with :

func sendData(data:String)
if(peripheral != nil)
//happens for sure
var bytesData = [UInt8](data.utf8)
let writeData = NSData (bytes: &bytesData, length: bytesData.count)
peripheral.writeValue(writeData as Data, for: characteristic, type: CBCharacteristicWriteType.withResponse)

and listing my characteristics ( which prints 1 UUID - thats true we have only 1, but it has 2 handles with different UUID)

func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
for charateristic in service.characteristics!
print("Found CHARACTERISTIC:",charateristic as CBCharacteristic)

Answer Source

You should check what properities are set for your characteristic. If CBCharacteristicPropertyWrite is set then write with a response can be executed. If only CBCharacteristicPropertyWriteWithoutResponse is set then you can not execute writeValue with type: CBCharacteristicWriteType.withResponse set.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download