BluetoothGattDescriptor始终为NULL

时间:2015-12-04 14:06:13

标签: android bluetooth bluetooth-lowenergy descriptor gatt

我的CLIENT_CHARACTERISTIC_CONFIG是

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

我这样做是为了改变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);
            }

在传感器上更改characteristic后,onCharacteristicWrite

中会调用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);
                        }
                    }
                }

descriptor始终为NULL。永远不会调用onCharacteristicChanged为什么?是我的CLIENT_CHARACTERISTIC_CONFIG对吗?

1 个答案:

答案 0 :(得分:1)

这是因为有些设备(我的是三星E5 Android 4.3->更新到5.1.1)支持BLE但不支持BluetoothGattDescriptor 0x2902(见链接)

要查看设备中支持者的所有描述符,请执行以下操作:

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

<强> [UPDATE] 有些BLE设备不支持Writed characteristics,例如 iBeacon