1

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:

  1. Is there a better way to subscribe to all 9 characteristics?
  2. Is it because my device (Samsung A30s 2019) can only handle 5 subscriptions - if so how can nRF Connect do more?
  3. 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.

ninjared
  • 11
  • 2
  • 1
    If you can subscribe in nRF Connect you should be able to do it in your app too. Have you tried doing it while nRF Connect is killed (not running)? – Emil Jun 19 '21 at 00:49
  • Yes, when I run my app I make sure nRF is disconnected and not running. Would it be possible that as the descriptors are bring written one at a time the characteristics values are changing and taking priority? – ninjared Jun 19 '21 at 07:25
  • I added a button that successfully subscribes to a single notification for one of the sensors (tested). If there are already 5 notifications subscribed to and I click the button it seems that it writes to the descriptor but the onDescriptorWrite() callback is never called. – ninjared Jun 19 '21 at 08:34
  • 2
    You could try look at the hci log to get some more insight what happens, or use an air sniffer. Or look at the logs on the peripheral. – Emil Jun 19 '21 at 09:07

0 Answers0