NickUnuchek
NickUnuchek

Reputation: 12857

BluetoothGattDescriptor is always NULL

My CLIENT_CHARACTERISTIC_CONFIG is

public final static UUID CLIENT_CHARACTERISTIC_CONFIG =  UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");

I do this to change CHARACTERISTIC:

            BluetoothGatt mBluetoothGatt = device.connectGatt(this.context, false, mGattCallback);
            mBluetoothGatt.connect();


            BluetoothGattService mCustomService = bleScanner.getBluetoothGatt().getService(UUID.fromString(bleServiceUUID));
            if(mCustomService == null){
               return;
            }

            BluetoothGattCharacteristic mCharacteristic = mCustomService.getCharacteristic(UUID.fromString(bleCharacteristicUUID));
            if(mReadCharacteristic == null){
                return;
            }
            String message = "myMessage";
            mCharacteristic.setValue(message.getBytes());

            if (!mBluetoothGatt.writeCharacteristic(characteristic)) {
                Log.e(TAG, "Failed to write characteristic");
            } else {
                Log.e(TAG, "Writed characteristic " + message);
            }

After that characteristic is changed on sensor, onCharacteristicWrite is called in BluetoothGattCallback

@Override
                public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {

                    if (status == BluetoothGatt.GATT_SUCCESS) {

                        if (!mBluetoothGatt.setCharacteristicNotification(characteristic, true)) {
                            DLog.e(TAG, "Notification Setup failed: "+characteristic.getUuid());
                        }

                        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
                        if (descriptor!=null){
                            descriptor.setValue( BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                            mBluetoothGatt.writeDescriptor(descriptor1);
                        }
                    }
                }

BUT descriptor is always NULL. And onCharacteristicChanged is never called. Why? and is my CLIENT_CHARACTERISTIC_CONFIG right?

Upvotes: 2

Views: 2782

Answers (1)

NickUnuchek
NickUnuchek

Reputation: 12857

it was because some of devices (mine is Samsung E5 Android 4.3->updated to 5.1.1) are support BLE but doesn't support BluetoothGattDescriptor 0x2902 (see link)

to see all Descriptors which are supporter in device call this:

for (BluetoothGattDescriptor descriptor:characteristic.getDescriptors()){
                    Log.e(TAG, "BluetoothGattDescriptor: "+descriptor.getUuid().toString());
                }

[UPDATE] Some BLE devices doesn't support Writed characteristics, such as iBeacon

Upvotes: 3

Related Questions