我有几个特征(全部在一项服务下),我希望在我的 Android 应用程序中收到通知。当我为其中之一设置通知描述符时,效果很好。我知道必须利用某种队列或延迟来接收多个通知,但我不明白如何在我的代码中实现它。我也无法在这个网站的 Android 文档中找到任何示例,或者以其他方式解释如何实现这一点。
这是我尝试创建的用于设置服务通知的代码:
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
for (BluetoothGattCharacteristic characteristic: gatt.getService(UUID.fromString("00001826-0000-1000-8000-00805f9b34fb")).getCharacteristics()) {
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
}
这是我在应用程序中设置文本以匹配刚刚更改的特征值的函数:
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
TextView instructionText = getView().findViewById(R.id.instructionText);
if(showMeasurements) {
instructionText.setText(characteristic.getStringValue(0));
}
}
我考虑尝试的另一种方法是创建列表 List<BluetoothGattCharacteristic> chars = new ArrayList<>();
并添加我在此列表中找到的每个特征。然后我可以尝试一一写入通知描述符,但我似乎也无法实现这一点。
我是一名大学生,对Android开发及此类功能不太熟悉。任何有关如何解决此问题的帮助将不胜感激。谢谢。
最佳答案
感谢this post ,我能够将我的通知描述符异步应用到我的每个特征。
尽管大部分代码是相同的,但仍需要进行一些细微的修改。正是出于这个原因,我想对这段代码中发生的事情提供更深入的解释,以便像我这样的 future 新手开发人员可以利用它。
在我的gatCallback
内,我创建了一个类似于我在问题中提到的列表。我还定义了一些 UUID,稍后我将在代码中使用它们:
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
List<BluetoothGattCharacteristic> chars = new ArrayList<>();
UUID pitchUUID = UUID.fromString("78c5307a-6715-4040-bd50-d64db33e2e9e");
UUID rollUUID = UUID.fromString("78c5307b-6715-4040-bd50-d64db33e2e9e");
一旦我发现了服务,我就会从特定服务中迭代我需要的特征,并将它们添加到我的列表中。然后我跳转到我创建的名为 subscribeToCharacteristics
的函数,传入我的BluetoothGatt对象:
for (BluetoothGattCharacteristic characteristic: gatt.getService(UUID.fromString("00001826-0000-1000-8000-00805f9b34fb")).getCharacteristics()) {
chars.add(characteristic);
}
subscribeToCharacteristics(gatt);
正是在这个函数中我设置了特征通知。我仅在列表中有项目时才这样做;如果存在项目,则意味着仍然有描述符需要写入!
private void subscribeToCharacteristics(BluetoothGatt gatt) {
if(chars.size() == 0) return;
BluetoothGattCharacteristic characteristic = chars.get(0);
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
if(descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
当我覆盖 onDescriptorWrite
时函数,我只需删除列表中的第一个元素,然后调用我的 subscribeToCharacteristics
再次发挥作用。
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
Log.i("DESCRIPTOR", "WROTE DESCRIPTOR FOR CHARACTERISTIC");
super.onDescriptorWrite(gatt, descriptor, status);
chars.remove(0);
subscribeToCharacteristics(gatt);
}
请注意,我没有使用 GetCharacteristicsWithNotifications(gatt);
因为这不是标准功能,并且在另一篇文章中没有得到很好的解释。
我也没有使用notifyCharacteristics.get(0);
因为这之前没有解释过或包含在帖子的代码中。
最后,我没有使用characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
因为我已经在 Arduino 代码中使用适当的属性定义了我的服务和特征。如果我不确定这一点,那么可以使用这段代码。
我希望这个(长)解释对 future 的开发者有值(value)!
关于android - 接收 Android 中多个特征的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60424495/