Android BluetoothGatt not receiving specific BluetoothGatt notifications # writeDescriptor (desc) return false

I am working on an app that needs to communicate with a Bluetooth LE device.

This is the code I am using to set the CharacteristicNotification

public boolean setCharacteristicNotification(
    BluetoothGattCharacteristic characteristic, boolean enable) {

if(mBluetoothAdapter == null || mBluetoothGatt == null) {
    Log.w(TAG, "BluetoothAdapter not initialized");
    return false;
}

Log.v(TAG, "setCharacteristicNotification(): uuid=" + characteristic.getUuid() + " enabled=" + enable);

boolean notifications = mBluetoothGatt.setCharacteristicNotification(characteristic, enable);

if(notifications) {
    Log.v(TAG, "setCharacteristicNotification(): Notifications are enabled");
}
else {
    Log.w(TAG, "setCharacteristicNotification(): Notifications are not enabled for characteristic " + characteristic);
}

BluetoothGattDescriptor desc = characteristic.getDescriptor(
        UUID.fromString(FBGattAttributes.CHARACTERISTIC_CLIENT_CONFIG));

desc.setValue(enable ?
                BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :
                new byte[]{0x00, 0x00}
);

boolean ok = mBluetoothGatt.writeDescriptor(desc);

if(ok){
    Log.v(TAG, "wrote descriptor value for notification: ok=" + ok);
}else{
    Log.w(TAG, "writeDescriptor failed: we will not get notifications=" + ok);
}


return ok;
}

      

Here in this code "mBluetoothGatt.writeDescriptor (desc)"; returns false sometimes, which is the reason I can't get any notifications from BluetoothGatt. I am not sure how to resolve these issues.

This issue only happened on LG G2 with OS 5.02 before it had 4.4. The problem was not that frequent, but after the update I got "false" every time except the first time. If we try to set notifications the first time after connecting, and as soon as I disconnect and try to connect, it will always return False. I need to kill and restart the application for it to work again. Does anyone know why this is not working? thanks in advance

+3


source to share


1 answer


In Lollypop, BluetoothGatt.java has been updated to include device-in-use locking, which prevents multiple asynchronous operations from being processed at the same time by an application. See BluetoothGatt.java:1029 for one returned from writeDescriptor.



I had to set up a command queue to get around this. Basically what my logic is doing is not having any async calls and retries in my BluetoothGattCallback.

+4


source







All Articles