I am trying to subscribe to 9 different simultaneous characteristic notifications. My target and min SDK version is 23
My app can currently only subscribes to a maximum of 5 notifications where after it stops. However, when using the nRF Connect app I can manually subscribe to all 9.
The 5 characteristics subscribed correctly trigger the onCharacteristicChanged()
callback.
Here is my code to set the notifications:
numNotifications = 8;
public void setNotify(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
boolean registered = gatt.setCharacteristicNotification(characteristic, true);
if(registered){
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
numNotifications--;
}
After calling setNotify()
for the 9th characteristic, I asynchronously write to the descriptor one characteristic at a time. (yes, I know I could use a loop instead of a switch and did try it with the same result).
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
// Called when the descriptor is updated for notification
if (status == BluetoothGatt.GATT_SUCCESS) {
switch (numNotifications){
case 7: setNotify(gatt ,chars.get(numNotifications));
break;
case 6: setNotify(gatt ,chars.get(numNotifications));
break;
case 5: setNotify(gatt ,chars.get(numNotifications));
break;
case 4: setNotify(gatt ,chars.get(numNotifications));
break;
case 3: setNotify(gatt ,chars.get(numNotifications));
break;
case 2: setNotify(gatt ,chars.get(numNotifications));
break;
case 1: setNotify(gatt ,chars.get(numNotifications));
break;
case 0: setNotify(gatt ,chars.get(numNotifications));
break;
}
}
}
Questions:
- Is there a better way to subscribe to all 9 characteristics?
- Is it because my device (Samsung A30s 2019) can only handle 5 subscriptions - if so how can nRF Connect do more?
- If it is my device perhaps I can create 2 BluetoothGatt objects in the same app as @Emil mentioned here: Android Bluetooth Low Energy Characteristic notification count limit: does this vary by device? How would I do this?
Thanks.