【问题标题】:BluetoothGattDescriptor is always NULLBluetoothGattDescriptor 始终为 NULL
【发布时间】:2015-12-04 14:06:13
【问题描述】:

我的 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 之后,onCharacteristicWriteBluetoothGattCallback 中被调用

@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 对吗?

【问题讨论】:

  • 您可以分享有关此问题的任何其他信息,或者您是否以某种方式解决了它?
  • @Harpreet,请参阅下面的答案

标签: android bluetooth bluetooth-lowenergy descriptor gatt


【解决方案1】:

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

要查看设备中所有支持者的描述符,请调用:

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

[更新] 部分BLE设备不支持Writed characteristics,如iBeacon

【讨论】:

    猜你喜欢
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 2022-01-20
    • 2016-01-14
    相关资源
    最近更新 更多