简体   繁体   English

如何在Android中为BLE编写快速稳定的特性?

[英]How to write characteristic fast and stable for BLE in Android?

I am developing for BLE in Android. 我正在开发Android中的BLE。 The Android can send data to BLE device by Gatt.writeCharacteristic . Android可以通过Gatt.writeCharacteristic将数据发送到BLE设备。

But When I write consecutive Characteristic to BLE device (5 times in 1 Second) , I will get GATT_DISCONNECTED and it return null when I try to getService and GetCharacteristic . 但是当我连续写入BLE设备的特性 (1秒内5次)时,我会得到GATT_DISCONNECTED ,当我尝试getServiceGetCharacteristic时它返回null

I have seen some BLE LED product in market. 我在市场上看到了一些BLE LED产品 It use a color picker in Android like the following picture: 它在Android中使用颜色选择器,如下图所示:

在此输入图像描述

When user change the color fast on the color picker , the BLE LED also change the color immediately. 当用户在颜色选择器上快速更改颜色时, BLE LED也会立即改变颜色。

How to write characteristic fast and stable for BLE in Android like the BLE LED product in market ? 如何像市场上的BLE LED产品一样在Android中为BLE写出快速稳定的特性?

When you call gatt.writeCharacteristic you need to do 2 things: 当你打电话给gatt.writeCharacteristic你需要做两件事:

  1. You need to queue up the gatt read and write requests so that when you make a read or write request, you must wait for the corresponding callback in BluetoothGattCallback before requesting the next read or write. 您需要排队gatt读取和写入请求,以便在发出读取或写入请求时,必须等待BluetoothGattCallback中的相应回调,然后才能请求下一次读取或写入。 This is an Android BluetoothGatt behavior that is not well documented. 这是Android BluetoothGatt行为,没有详细记录。
  2. Somewhat related to 1. above, you should check the boolean return value of the gatt read and write requests. 与上面的1.有些相关,你应该检查gatt读写请求的布尔返回值。 If it does not return true, then your request failed. 如果它不返回true,那么您的请求失败。 This occurs when some other gatt operation is in still in progress at the time you make the next request. 当您进行下一个请求时,某些其他gatt操作仍处于进行中时会发生这种情况。

I have found that the best way to read (or write) BLE characteristics fast is to make the request, wait for the callback and make the next request from the callback. 我发现快速读取(或写入)BLE特性的最佳方法是发出请求,等待回调并从回调中发出下一个请求。 If there is a faster or more reliable way, I am unaware of it. 如果有更快或更可靠的方式,我不知道它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM