在我的应用程序中,我使用READ和WRITE来处理特定的BluetoothGattCharacteristic
对象。CCD_ 2、CCD_ 3和CCD_。然后,我尝试设置NOTIFY选项,以便我的Android应用程序在设备上的特定特性发生更改时收到通知。我已经通过以下代码设置了这个:
// Local notifications
mGatt.setCharacteristicNotification(statusTypeCharacteristic, notify);
// Remote notifications
BluetoothGattDescriptor desc = statusTypeCharacteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
Log.d("Descriptor", desc.toString());
boolean test;
test = desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); // return value = true
test = mGatt.writeDescriptor(desc); // return value = true
当特性发生变化时,回调:onCharacteristicChanged
将按预期调用
但是,现在所有READ和WRITE操作都不再工作。当我对处理描述符的行进行注释时,READ和WRITE将再次工作。
我非常不清楚的一部分是关于用于获取描述符的UUID
。这是正确的吗?我应该扫描特征中的所有描述符,并在其中一个描述符上启用通知吗?我怎么知道该设置哪一个,因为我有多个要返回?
好的,所以我已经解决了这个问题。在我的应用程序开始时,我正在配置(即编写)许多描述符。它有两个问题:1-描述符一次只能写入一个2-当描述符被写入时,不会发生读/写操作
修复方法是创建一个写入描述符操作队列,并在onDescriptorWrite回调中执行下一次描述符写入。
private void writeGattDescriptor(BluetoothGattDescriptor d) {
//put the descriptor into the write queue
descriptorWriteQueue.add(d);
//if there is only 1 item in the queue, then write it. If more than 1, we handle asynchronously in the
// callback
if(descriptorWriteQueue.size() == 1) {
mGatt.writeDescriptor(d);
}
}
然后在回调中:
@Override
public void onDescriptorWrite (BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
// Called when the descriptor is updated for notification
Log.d("onDescriptorWrite", descriptor.getUuid().toString());
// pop the item that we just finishing writing
descriptorWriteQueue.remove();
// if there is more to write, do it!
if(descriptorWriteQueue.size() > 0) {
mGatt.writeDescriptor(descriptorWriteQueue.element());
}
// Inform the framework that the scope has connected, configured and ready to process commands
if(descriptorWriteQueue.size() == 0) {
// Do something else, such as reads and writes
}
}