当我在5.0.1
Android版本上测试我的代码时,方法writeDescriptor
始终返回false
。我在4.3
和4.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
中,正如我所说的
答案 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");
}
}
}