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

how to enable notification

 Hello.

I just found here: https://nordicsemiconductor.github.io/Nordic-Thingy52-FW/documentation/firmware_architecture.html

something like this:

"To turn on sensor reading, enable notifications from the desired characteristic by writing 0x0001 to the Client Characteristic Configuration Descriptor (CCCD) for that characteristic."

but I don know how to do that.

Here is my code:

  @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);

           
            UUID SERVICE_UUID=UUID.fromString           ("EF680200-9B35-4933-9B10-52FFA9740042");
            UUID CHARACTERISTIC_UUID=UUID.fromString    ("EF680201-9B35-4933-9B10-52FFA9740042");


            BluetoothGattService service = gatt.getService(SERVICE_UUID);
            BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID);
            gatt.readCharacteristic(characteristic);
            gatt.setCharacteristicNotification(characteristic,true);
            bluetoothLeScanner.stopScan(scanCallback);

        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);

           
            byte[] moja_tablica = new byte[3];
            gatt.setCharacteristicNotification(characteristic,true);
            moja_tablica= characteristic.getValue();

        }

How to writie 0x0001 to the Client Characteristic Configuration Descriptor (CCCD) for that characteristic?

  • I found this:

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
            UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    bluetoothGatt.writeDescriptor(descriptor);
    

    but I still dont know what is in my case:

    SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG

    and

    BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE

     

  •   @Override
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
    
                
    
                UUID SERVICE_UUID=UUID.fromString           ("EF680200-9B35-4933-9B10-52FFA9740042");
                UUID CHARACTERISTIC_UUID=UUID.fromString    ("EF680201-9B35-4933-9B10-52FFA9740042");
    
                BluetoothGattService service = gatt.getService(SERVICE_UUID);
                BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID);
    
                BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                gatt.writeDescriptor(descriptor);
                bluetoothLeScanner.stopScan(scanCallback);
    
                gatt.readCharacteristic(characteristic);
                gatt.setCharacteristicNotification(characteristic,true);
    
    
            }
    
            @Override
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
    
            }
    
            @Override
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);
            }
    
            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
                
                //here we read changes for example:
                    byte[] moja_tablica = new byte[2];
                    moja_tablica= characteristic.getValue();
                
            }
    

    thats all :-)

  • Hi

    Do you mean to say you found the solution?

    The last code you sent appears to be correct. 

    Best regards
    Torbjørn

Related