I have several characteristics (all under one service) that I would like to be notified of in my Android app. When I set the notification descriptor for one of them, it works well. I understand that some sort of a queue or delay must be utilized for receiving notifications for multiple, but I do not understand how to implement it in my code. I also cannot find any examples on this site Android documentation, or otherwise that explain how to implement this.
Here is the code I have attempted to create for setting the service notification:
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
for (BluetoothGattCharacteristic characteristic: gatt.getService(UUID.fromString("00001826-0000-1000-8000-00805f9b34fb")).getCharacteristics()) {
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
}
Here is the function I have for setting the text in my app to match the value of the characteristic which has just changed:
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
TextView instructionText = getView().findViewById(R.id.instructionText);
if(showMeasurements) {
instructionText.setText(characteristic.getStringValue(0));
}
}
One other method that I have considered trying is creating a list List<BluetoothGattCharacteristic> chars = new ArrayList<>();
and adding each characteristic I find in this list. Then I could try to write the notification descriptor one by one, but I cannot seem to implement this either.
I am a college student who is not familiar with Android development and such functions. Any help on how to go about resolving this problem would be greatly appreciated. Thank you.