Reputation: 85
I would like to subscribe to multiple characteristics. Currently I'm doing this (mostly successfully) by using a timer between calling each writeDescriptor(descriptor). I would like to instead do this by utilizing the onDescriptorWrite() callback, but it is never being called.
I have the following call back.
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
Log.d("onDescWrite", "yes");
if(characteristics_with_notifications_or_indications.size() > 0) {
characteristics_with_notifications_or_indications.remove(0);
subscribeToCharacteristics();
}
}
};
It's initialized in my connect function.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
This is my descriptor write
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(CESGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
I would like to know when I'm done doing a writeDescriptor() via the callback so I can subscribe to another characteristic. However, even when I've successfully subscribed to a characteristic and am receiving valid data, onDescriptorWrite() still does not get called.How can I get onDescriptorWrite() to be called?
Upvotes: 2
Views: 2426
Reputation: 85
So I figured out the problem. The callback function wasn't being called because I had a list of all the characteristics which had notify/indicate properties. However, before I would write to my descriptor I would check for certain UUIDs. Well, I wasn't checking for one of the characteristics because I wasn't using it, and it happened to be one of the first ones on the list! So, I would never even write to the descriptor for the callback to be called.
Upvotes: 0