简体   繁体   English

如何使用Android蓝牙API定时BLE GATT交换?

[英]How to time a BLE GATT exchange with Android Bluetooth APIs?

I am writing an Android application that communicates with a custom made device BLE via GATT services. 我正在编写一个通过GATT服务与自定义设备BLE通信的Android应用程序。 Said device provides a service with 2 characteristics for reading and writing data. 所述设备提供用于读取和写入数据的具有两个特征的服务。 When some data is written to the WRITE characteristic, the BLE device will then send it through a wired UART interface to some other device. 当一些数据写入WRITE特性时,BLE设备将通过有线UART接口将其发送到其他设备。 That other device will then respond to the BLE device through that same UART interface. 然后,其他设备将通过相同的UART接口响应BLE设备。 Upon reception, the BLE device will send a notification that new data is available on the READ characteristic of its service so my Android application can retrieve it. 收到消息后,BLE设备将在其服务的READ特性上发送新数据可用的通知,以便我的Android应用程序可以检索它。

What I would like to do is measure the time elapsed between when I send a request from my Android application to when I receive the notification that new data is available. 我想做的是测量从我的Android应用程序发送请求到收到新数据可用通知的时间。

I have implemented a "stopwatch" as a long . 我已经使用了long的“秒表”。 I set it to System.currentTimeMillis(); 我将其设置为System.currentTimeMillis(); when I write a data and I compare its value to another call to System.currentTimeMillis(); 当我写入数据并将其值与对System.currentTimeMillis();另一个调用进行比较时System.currentTimeMillis(); upon notification reception giving something like : 在收到通知后,给出如下信息:

long stopwatch = System.currentTimeMillis();
// ...

// ...
long elapsed = System.currentTimeMillis() - stopwatch;

I have set 2 stopwatches to compare 2 measured times. 我设置了2个秒表来比较2个测量时间。

The first stopwatch is reset when I call gatt.writeCharacteristic(myCharacteristic) and the second one is reset when BluetoothGattCallback.onCharacteristicWrite() is called. 第一秒表复位当我打电话gatt.writeCharacteristic(myCharacteristic)并且当第二个被复位BluetoothGattCallback.onCharacteristicWrite()被调用。

I have registered my application for the notifications from the READ characteristic so I stop both stopwatches when BluetoothGattCallback.onCharacteristicChanged() is called. 我已经通过READ特性注册了用于通知的应用程序,因此在调用BluetoothGattCallback.onCharacteristicChanged()时我停止了两个秒表。

The thing is, I have on average 100ms between those measurements ! 问题是,这些测量之间我平均有100毫秒! I think it is quite a lot. 我认为很多。 Average time starting when I call gatt.writeCharacteristic(myCharacteristic) is 140ms Average time starting when BluetoothGattCallback.onCharacteristicWrite() is called is 40ms. 调用gatt.writeCharacteristic(myCharacteristic)的平均开始时间为gatt.writeCharacteristic(myCharacteristic)毫秒。调用BluetoothGattCallback.onCharacteristicWrite()平均开始时间为40毫秒。

So I was wondering what was the proper way to time such an exchange and when should I reset my stopwatch in order to get the most accurate time measurement. 因此,我想知道如何安排这种交流时间的正确方法,以及何时应该重置秒表以获取最准确的时间测量。

Well if you use the default connection interval of 50 ms this is not strange at all. 好吧,如果您使用默认的连接间隔50毫秒,这一点都不奇怪。 Assuming the write is issued at the next connection event (which might happen up to 50 ms in the future), and the result is available and notified at the next connection event 50 ms later, you get 100 ms. 假设在下一个连接事件(将来可能会发生最多50毫秒)发出写操作,并且结果可用并在50毫秒后的下一个连接事件通知,则得到100毫秒。 You can issue a Connection Parameter Update Request to get a faster connection interval. 您可以发出连接参数更新请求以获得更快的连接间隔。

If you want to get low overhead, why do you notify that data is available and then read, rather than just embedding the data directly in the notification payload? 如果要降低开销,为什么不通知数据可用然后再读取,而不是直接将数据嵌入到通知有效负载中呢?

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

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