I am new to Android development and I can't understand how to properly turn all ble notifications and get all of them.
I've tried to loop through all services
for(BluetoothGattService service : gatt.getServices()){
if( service.getUuid().equals(Step_UUID)) {
BluetoothGattCharacteristic characteristicData = service.getCharacteristic(Step_UUID);
for (BluetoothGattDescriptor descriptor : characteristicData.getDescriptors()) {
descriptor.setValue( BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
gatt.setCharacteristicNotification(characteristicData, true);
}
}
but I don't get any notifications back. Please, help I do not understand what I am doing wrong..
EDIT
Right now I use this method to enable notifications after services are discovered:
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
// Setting default write type according to CDT 222486
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
String serviceUUID = characteristic.getService().getUuid().toString();
String serviceName = GattAttributes.lookupUUID(characteristic.getService().getUuid(), serviceUUID);
String characteristicUUID = characteristic.getUuid().toString();
String characteristicName = GattAttributes.lookupUUID(characteristic.getUuid(), characteristicUUID);
String descriptorUUID = GattAttributes.CLIENT_CHARACTERISTIC_CONFIG;
String descriptorName = GattAttributes.lookupUUID(UUIDDatabase.UUID_CLIENT_CHARACTERISTIC_CONFIG, descriptorUUID);
if (characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG)) != null) {
if (enabled == true) {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
boolean aaa = mBluetoothGatt.writeDescriptor(descriptor);
aaa = mBluetoothGatt.writeDescriptor(descriptor);
aaa = false;
} else {
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
boolean aaaa = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
aaaa = false;
}
The problem is that first characteristic notifies well, but all others I am trying to enable fails on the line
mBluetoothGatt.writeDescriptor(descriptor);
Don't know what to do...