Getting Unknown Error while writing data on BLE in iOS
When you write the data you're requesting a response (CBCharacteristicWriteWithResponse
).
The write will fail if the characteristic you're writing to does not have the property CBCharacteristicPropertyWrite
set, but only supports writes without response (CBCharacteristicPropertyWriteWithoutResponse
).
if ((self.positionCharacteristic.properties & CBCharacteristicPropertyWrite) == CBCharacteristicPropertyWrite) {
// Responses are available, so write with response.
[self.peripheral writeValue:dataToWrite forCharacteristic:self.positionCharacteristic type:CBCharacteristicWriteWithResponse];
}
else if ((self.positionCharacteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) == CBCharacteristicPropertyWriteWithoutResponse) {
// Responses are not available.
// Write with response is going to fail, so write without response.
[self.peripheral writeValue:dataToWrite forCharacteristic:self.positionCharacteristic type:CBCharacteristicWriteWithoutResponse];
}
I have seen this error while developing both the iOS App (as Central) and the BLE Device firmware (as Peripheral). This would occur when I changed/modified the characteristics on the BLE peripheral.
iOS seems to cache information about the services and characteristics after a connection. I was able to solve this by updating the Bluetooth Address of the peripheral whenever I would build/download new firmware to the Device.