3

I am working on an application which needs to communicate with a Bluetooth LE device.

This is the code I use 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 that's the reason I am not able to get any notifications from BluetoothGatt. I am not sure how to fix this issues.

This problem happened only on LG G2 with OS 5.02 before that it has 4.4 The problem was not that frequent but after the update I was getting "false" every time except the first time. If we try to set the Notifications first time after the connection it works and once I disconnect and try to connect then It will always return's False. I need to kill and restart the app for it to work again. Does anyone have any idea why this is not working? Thanks in advance

1

1 Answer 1

4

In Lollypop BluetoothGatt.java was updated to include "device is busy" blocks that prevent multiple asynchronous operations from being requested at a time by an application. See BluetoothGatt.java:1029 for the one returned from writeDescriptor.

I've had to setup a command queue to get around this. Basically what my logic does is enqueue any failed asynchronous calls and perform retries in my BluetoothGattCallback.

1
  • Hi. Can you further explain your solution?
    – aclowkay
    Nov 26, 2016 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.