cordova-plugin-bluetoothle
cordova-plugin-bluetoothle copied to clipboard
Certain errors not comming back from android to callback function
Hello again,
So I am noticing that there are some bad status errors that are not comming back to any of my call back function such as GATT_INSUFFICIENT_AUTHENTICATION
I see in the log
11-11 10:26:56.213: D/BluetoothGatt(5582): writeCharacteristic() - uuid: 33152b22-555a-48cd-8cf4-32cf6bc60730
11-11 10:26:56.293: D/BluetoothGatt(5582): onCharacteristicWrite() - Device=20:73:7A:10:05:0B UUID=33152b22-555a-48cd-8cf4-32cf6bc60730 Status=5
11-11 10:27:02.059: D/BluetoothGatt(5582): onCharacteristicWrite() - Device=20:73:7A:10:05:0B UUID=00000000-0000-0000-0000-000000000000 Status=137
And my writeFailed callback function never gets any message back from the BLE plugin. Even if the plugin could just send me back a "Something Failed" message that would be good enough for me.
You aren't calling this with no write response, right? If the status doesn't equal BluetoothGatt.GATT_SUCCESS, it should be sending an error message.
//If write was successful, return the written value
if (status == BluetoothGatt.GATT_SUCCESS)
{
addProperty(returnObj, keyStatus, statusWritten);
addPropertyBytes(returnObj, keyValue, characteristic.getValue());
callbackContext.success(returnObj);
}
//Else it failed
else
{
addProperty(returnObj, keyError, errorWrite);
addProperty(returnObj, keyMessage, logWriteFailReturn);
callbackContext.error(returnObj);
}
I am not my gatt characteristic is a standard write request with authorization required. I am not seeing the failed callback coming back to any of the callback functions registered with the plugin. I have a debug function that all callback function I register call to let me know what the BLE stack returned to me. Also I am seeing the debug messages from onCharacteristicWrite telling me that I am getting a status 5, now the second call is really odd going to UUID=00000000-0000-0000-0000-000000000000 not sure why that is getting called.
Only thing I can think of is that callback instance is not getting retrieved by the plugin correctly. I haven't done much debugging on the problem yet.
This is on a Nexus 7 which in the past pairing has been a difficult pain. I tried on a different android device I am not getting this error.
So I guess I'll walk through the cycle connecting to a device, so for note these steps, except for the differences between how iOS and Android handle discovering are the same.
1.) Scan for devices 2.) Connect to target device 3.) Discover Services* 4.) Discover Characteristics* 5.) Subscribe to a notification 6.) Write to a characteristic 7.) Wait for write success 8.) Read back from characteristic 9.) Close connection
So when I try write to a characteristic right after subscribing to the notification that I get the Error described above and that I am waiting forever for some reply back from the BLE plugin about the status of the write sent.
It appears to revolve around the way Android handles pairing and that you are suppose to do some weird song and dance when trying to write or read to a paired device for the first time,
Googled and found a thread
http://forums.xamarin.com/discussion/9469/bluetooth-low-energy-ble-on-android
@DavidPuplava A couple of things I've found to be helpful.
Call CreateBond after a device has been connected and services discovered. When the OnBonded callback is received, you should be good to go.
Let Android do the pairing for you -- when you read the first encrypted characteristic, OnCharacteristicRead will return status 5 and immediately kick off bonding. Then OnCharacteristicRead will return status 137. At that point, you can re-issue the Read command, and the device is bonded.
So would adding a function for pairing resolve the issue, you think?
Well I think when ever I had this issue with android devices while I was writing native BLE apps I would simply take the status 137 as a "Darn it Android..." and simply send the BLE command again and see if it works. It appears that this all has to do with the poorly implemented pairing system of android. Now I do think that having functions to expose the pairing system would be helpful so that as a user I can take control of how I wish to pair to a device.
i get the same status error with some devices, when i check my log it's showing that the bluetoothGatt try to write on a null (all digit on 0) UUID characteristic. did any one found a solution for that ??
I've also experienced this issue, if i'm not mistaken it's because the encryption keys are being exchanged AFTER write operation is sent to device which then results in GATT_INSUFFICIENT_AUTHENTICATION. As a workaround i perform a read operation on a encrypted characteristic before doing the write operation.