android - 读取 Gatt 特性后会发生什么情况?

标签 android bluetooth-lowenergy gatt

我正在使用带有 Android 5.1(蓝牙 4.0)的 Android 设备和具有蓝牙 4.2 的 MCU 板。

在我的 MCU 端,我正在循环更新我的 Gatt 特性,以确保我知道我写入的数据是否一致。在我将其写入 gatt 数据库之前,我正在使用 crc 检查。

在我的 android 端,我只有一个线程,它从 gatt 数据库中读取特征,紧接着我有相同的 crc,但似乎 50% 的值已损坏(这对我来说没有意义边)。 我知道我在 gatt 数据库中写入的数据是正确的,所以我猜问题出在线程中多次读取特性。

我已经尝试在我的 android 端通过通知读取特征,但蓝牙服务永远不会跳入 OnCharacteristicChanged 回调。

我的特征更新看起来像这样

tmpGatt.readCharacteristic(characteristic);

特征被之前的uuid过滤

for(int i = 0; i<Services.size(); i++){
                Characteristics = Services.get(i).getCharacteristics();
                for(int c=0;c < Characteristics.size();c++){
                    UUID myUUID = Characteristics.get(c).getUuid();
                    if(myUUID.toString().equals("354a1b8e-7597-11e6-8b77-86f30ca893d3")){
                        characteristic = Characteristics.get(c);
                        //refExternalData.getRefBluetoothGatt().readCharacteristic(characteristic);
                        descriptor = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
                        Log.i("BLE", "Characteristic " + myUUID.toString() + " found");
                    }
                }
            }

所以我需要做任何特别的事情来重新读取 gatt 特性吗?

最佳答案

您是否遵循了 https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#notification 中的程序?为了启用通知?

当您发出 readCharacteristic 时,在您获得 onCharacteristicRead 之前,您不能发出新的 readCharacteristic。或者实际上您不允许发送任何新请求(readCharacteristicwriteCharacteristicreadDescriptorwriteDescriptor)直到先前的请求已完成。这是因为一次可能只有一个未完成的 GATT 请求,并且没有内部队列。

关于android - 读取 Gatt 特性后会发生什么情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39848310/

相关文章:

android - 使用多线程从 Internet 检索单个图像

java - 将数据从 BLED112 发送到 BLE 设备

iOS - 如何在 BLE 外围设备上发现未公布的服务

bluetooth - BLE 设备无法连续发送多个 GATT 通知

javascript - 如何获取访客设备信息

Android 模拟器 - 运行 android 应用程序时出现错误

android - 使用 else 和 if android sqlite 构建逻辑条件查询

ios - 外围 BLE 设备的唯一标识符

android - 如何设置 BLE 通告数据包的设备名称字段

bluetooth - GATT配置文件和UART服务