1

我希望在我的 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开发等功能。任何有关如何解决此问题的帮助将不胜感激。谢谢你。

4

1 回答 1

1

感谢这篇文章,我能够将我的通知描述符异步应用于我的每个特征。

尽管大部分代码是相同的,但需要进行一些细微的修改。正是出于这个原因,我想对这段代码中发生的事情进行更深入的解释,以便像我这样的未来新手开发人员可以利用它。

在我的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 代码中使用适当的属性定义了我的服务和特性。如果我不确定这一点,那么可以使用此代码。

我希望这个(长)解释对任何未来的开发者都有价值!

于 2020-02-28T01:09:39.157 回答