android - Bluetooth LE 监听多种特性通知

标签 android bluetooth notifications characteristics

我在与自定义 BLE 传感器板通信的 Android 手机上使用 BLE 应用程序。板提供了两个特性,加速度和心电图。在电话方面,我想从传感器板接收两个特性的通知。我设置通知的代码:

mGatt.setCharacteristicNotification(ecgChar, true);
            BluetoothGattDescriptor descriptor = ecgChar.getDescriptor(
                    UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mGatt.writeDescriptor(descriptor);
            mGatt.setCharacteristicNotification(accelChar, true);
            descriptor = ecgChar.getDescriptor(
                    UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            mGatt.writeDescriptor(descriptor);

但是,我只能收到第一个特征的通知。当我只为一个特征注册通知时,效果很好。 ECG 和加速度的采样频率均为 100Hz。那么我怎样才能收到来自这两个特征的通知呢?谢谢。

最佳答案

你一次只能有一个未完成的 gatt 操作。在这种情况下,您会执行两次 writeDescriptor 调用,然后等待第一个完成。您必须等待 https://developer.android.com/reference/android/bluetooth/BluetoothGattCallback.html#onDescriptorWrite(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattDescriptor, int)直到你可以发送下一个。

关于android - Bluetooth LE 监听多种特性通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42126016/

相关文章:

android - 随着最近的应用程序切换,从通知开始/恢复任务返回堆栈

ios - iOS 上的每日提醒

java - Android ListView 数组索引越界异常

android - API 23 中的 ContextCompat.getColor 崩溃(找不到 Context.getColor)

android - 将 Android 连接到具有未知 UUID 的设备

java - 在 Java 中为字节数组实现缓冲区

ios7 - 多点连接随机断开

php - org.json.JSONException : Value <br of type java. lang.String 无法转换为 JSONObject

ios - 外部附件框架蓝牙对。 connectedAccessories 每次计数 0

ios - 屏幕锁定时的用户通知