android ble中的方法writeDescriptor总是在lollipop中返回false

时间:2015-08-05 10:19:27

标签: android android-bluetooth android-ble

当我在5.0.1 Android版本上测试我的代码时,方法writeDescriptor始终返回false。我在4.34.2.2设备上对其进行了测试,并始终返回true 这是我的方法:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                              boolean enabled) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }

        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        // This is specific to Heart Rate Measurement.
        if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                    UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
            descriptor.setValue(new byte[]{0x02});
            boolean status = mBluetoothGatt.writeDescriptor(descriptor);
            if(status)
                Log.d(TAG,"Client Characteristic Config is changed to 2");
            else
                Log.e(TAG,"Cannot set Client Characteristic Config");
        }
    }

问题出在方法writeDescriptor中,正如我所说的

1 个答案:

答案 0 :(得分:-1)

请参阅我编辑的答案。

 public void setCharacteristicNotification(BluetoothGattCharacteristic  characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }

    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    // This is specific to Heart Rate Measurement.
    if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
        BluetoothGattDescriptor descriptor = characteristic
                .getDescriptor(GattInfo.CLIENT_CHARACTERISTIC_CONFIG);


      if(descriptor!=null)
                {
          descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        boolean status = mBluetoothGatt.writeDescriptor(descriptor);
        if(status)
            Log.d(TAG,"Client Characteristic Config is changed to 2");
        else
            Log.e(TAG,"Cannot set Client Characteristic Config");
    }
     }
}