我CLIENT_CHARACTERISTIC_CONFIG是BluetoothGattDescriptor始終是NULL
public final static UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
我這樣做是爲了改變特點:
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權利?
您可以共享這個問題或者您是否以某種方式解決了這個問題? – Harpreet
@Harpreet,請參閱下面的回答 – NickUnuchek