This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Android-enable notification client side, CCCD & UUID.

Hi All,

I have a problem to enable notification on my Android phone, i followed the tutorial on android web page (section bluetooth low energy) but my app crash, i have this error :

W/BluetoothGatt: Unhandled exception in callback
                                                                              java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothGattDescriptor.setValue(byte[])' on a null object reference
                                                                                  at com.selfiethings.yacire.selfwakeup2.ConnectionBleService$override.setCharacteristicNotification(ConnectionBleService.java:314)
                                                                                  at com.selfiethings.yacire.selfwakeup2.ConnectionBleService$override.static$access$300(ConnectionBleService.java:31)
                                                                                  at com.selfiethings.yacire.selfwakeup2.ConnectionBleService$override.access$dispatch(ConnectionBleService.java)
                                                                                  at com.selfiethings.yacire.selfwakeup2.ConnectionBleService.access$300(ConnectionBleService.java:0)
                                                                                  at com.selfiethings.yacire.selfwakeup2.ConnectionBleService$2.onServicesDiscovered(ConnectionBleService.java:151)
                                                                                  at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:305)
                                                                                  at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:217)
                                                                                  at android.os.Binder.execTransact(Binder.java:446)

My code to set up the notification on my client (Android app) is like this :

 private void setCharacteristicNotification() {

    BluetoothGattService bluetoothGattService = mBluetoothGatt.getService(UUID.fromString(SELF_WAKE_UP_SERVICE_UUID));
    BluetoothGattCharacteristic bluetoothGattCharacteristic = bluetoothGattService.
            getCharacteristic(UUID.fromString(BATTERY_LEVEL_CHARACTERISTIC_UUID));

    mBluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true);
    BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(
            UUID.fromString(BATTERY_LEVEL_CHARACTERISTIC_UUID));

    Log.i(TAG, "descriptor " + descriptor);
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);

}

So, i have an issue at the line : BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor( UUID.fromString(BATTERY_LEVEL_CHARACTERISTIC_UUID)); My descriptor reference is null. In this link text

I see, to write the CCCD to enable notification, i must have the CCCD_UUID, but what is this CCCD UUID, i have only UUID for charcteristics and services in my server (nrf51 DK). So i need to write this : BluetoothGattDescriptor desc = char.getDescriptor(CCCD_UUID); to enable notif. in Android.

Do you have any information about CCCD UUID please ? Thanks.

Parents
  • Not all characteristics have a CCCD by default. You have to make sure, that the GATT server provides a CCCD for the characteristic. If you have a ble sniffer, you could go and watch the service and characteristic discovery between client and server and see, if there is a CCCD and if the correct attribute bits are set within the characteristic descriptor.

    You can not address a specific CCCD by UUID, as all CCCD have the very same 16-bit UUID. You have to lookup the characteristic, that contains the CCCD and then search through all attributes of that characteristic. But that is something that should have been done by Android for you.

  • Hi Torsten,

    Thanks for your answer. There is already a CCCD in my server.The code in the server side is OK, i tested it with nrfConnect. But when i test with my Android App, it doesn't work.

    What is this 16-bit UUID for CCCD ? maybe this is this UUID which is needed in this function :

    bluetoothGattCharacteristic.getDescriptor( UUID.fromString(here_16bit_UUID_for_All_CCCD));
    

    Edit_1: Ok, in the excellent tutorial by Martin Bors-Lind, he speaks about a UUID for all CCCD : the 16_bit value is : 0x2902.

    I will test with this value int the getDescriptor() method.

Reply
  • Hi Torsten,

    Thanks for your answer. There is already a CCCD in my server.The code in the server side is OK, i tested it with nrfConnect. But when i test with my Android App, it doesn't work.

    What is this 16-bit UUID for CCCD ? maybe this is this UUID which is needed in this function :

    bluetoothGattCharacteristic.getDescriptor( UUID.fromString(here_16bit_UUID_for_All_CCCD));
    

    Edit_1: Ok, in the excellent tutorial by Martin Bors-Lind, he speaks about a UUID for all CCCD : the 16_bit value is : 0x2902.

    I will test with this value int the getDescriptor() method.

Children
No Data
Related