Beacon 设备的 BluetoothGattDescriptor 为 NULL

BluetoothGattDescriptor is NULL for Beacon device

Beacon device

的 BluetoothGattDescriptor 为 NULL

这是我的代码:

BluetoothGattCharacteristic characteristic = mBluetoothGatt.getService(UUID.fromString("00001800-0000-1000-8000-00805f9b34fb"))
                            .getCharacteristic(UUID.fromString("00002a00-0000-1000-8000-00805f9b34fb"));

        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); // HERE descriptor = NULL
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(desc);


        characteristic.setValue("newName".getBytes());
        mBluetoothGatt.writeCharacteristic(characteristic);

为什么描述符为 NULL?

似乎是因为某些设备(我的是三星 E5)支持 BLE 但不支持 BluetoothGattDescriptor 0x2902(参见 link)

要查看您设备中支持的所有描述符,请调用此命令:

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

根据您的代码,除非特征没有通知 属性,否则它不应为 null。您确定远程设备针对此特定特征向其 GATT table 添加了 Characteristic Client Config Descriptor 吗?只是为了仔细检查它,请执行以下操作:在发现服务的地方放置一个断点,并在调试器中找到该特征。查找特征属性,看特征属性是否有这个值:PROPERTY_NOTIFY

Constant Value: 16 (0x00000010)

或者,记录 属性 值:characteristic.getProperties ()