Android每个连接间隔接收多个BLE数据包 [英] Android receiving multiple BLE packets per connection interval

查看:827
本文介绍了Android每个连接间隔接收多个BLE数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台通过BLE GATT特性将数据传输到我的Android(4.4.2)的设备.该设备在每个连接间隔发送多个20字节的数据包,其中每个数据包都有一个序列号. 假设设备在每个连接间隔发送4个数据包,如图所示... | sq1,sq2,sq3,sq4 | sq5,sq6,sq7,sq8 | ..

I have a device that transmits data to my Android (4.4.2) over a BLE GATT characteristic. The device sends multiple 20byte packets per connection interval, where each packet has a sequence number. Lets say the device is sending 4 packets per connection interval, as shown... |sq1,sq2,sq3,sq4|sq5,sq6,sq7,sq8|..

我注意到在Android端,可能会出现对onCharacteristicChanged()的调用,例如... | sq4,sq4,sq4,sq4 | sq8,sq8,sq8,sq8 | ...

I notice at the Android end, may calls to onCharacteristicChanged() are coming in like... |sq4,sq4,sq4,sq4|sq8,sq8,sq8,sq8|...

Android是否支持在每个连接间隔发送多个数据包?

Does Android support sending multiple packets per connection interval?

迈克尔

推荐答案

是的,它在每个连接间隔支持多个数据包,但是API设计不良,并且特征值是共享对象,可能会被多个线程更新.每个通知可能在不同的线程中处理,然后在一个线程(在connectGatt(...)中设置,或在较旧的Android版本中使用一些未指定的线程)中调用onCharacteristicChanged.如果连接间隔很小,或者设备发送了很多数据包在单个时间间隔内,可能会发生,在您收到带有第一个通知的回调之前,该回调将被另一个覆盖.

Yes, it supports multiple packets per connection interval, but the API is badly designed, and the characteristic value is a shared object, which may be updated by multiple threads. Each notification may be handled in a different thread and they then call onCharacteristicChanged on one thread (either set in connectGatt(...), or some unspecified thread for older Android versions. If the connection interval is very small, or the device sends many packets in a single interval, it may happen that before you receive the callback with the first notification it will be overwritten by another.

我可能会提供2条建议:

There are 2 advices I may give:

  1. 应该在onCharacteristicChanged方法开始时尽快获取对该值的引用,因为它可能很快就会被更改(通过另一个通知或写入操作).参考: https://github.com/NordicSemiconductor/Android-BLE-Library/Issues/54
  2. 不使用connectGatt(..., Handler)方法,而是依赖默认处理程序.如果设置了处理程序,这将在收到回调之前增加额外的时间,因此有更多机会覆盖数据.参考: https://github.com/NordicSemiconductor/Android-BLE-Library/Issues/54
  1. The reference to the value should be obtained as soon as possible, at the beginning of onCharacteristicChanged method, as it may soon be changed (by another notification, or write operation). Ref: https://github.com/NordicSemiconductor/Android-BLE-Library/issues/54
  2. Don't use connectGatt(..., Handler) method, but rather rely on the default handler. If you set a handler, this will add additional time before you receive the callback, so more opportunity for the data to be overwritten. Ref: https://github.com/NordicSemiconductor/Android-BLE-Library/issues/54

以上建议不能保证成功的百分百,但是会降低丢失数据的机会.

The above advices do not guarantee 100% of success, however, but lower the chance of loosing data.

此外,在编写和侦听通知时,我建议将其拆分为2个以上的特征,因为然后在写和通知操作之间共享该特征的值.价值可能会在发送之前更新,最终您将发送已收到的内容.参考: https://github.com/NordicSemiconductor/Android-BLE-Library/Issues/60

Also, when you write and listen to notification, I recommend splitting that into 2+ characteristics as the characteristic's value is then shared between write and notify operations. Value may be updated before it's sent, and you'll end up sending what was received. Ref: https://github.com/NordicSemiconductor/Android-BLE-Library/issues/60

iOS API更好,因为数据被设置为 peripheralDidUpdateValueFor(...) .他们没有共享.

The iOS API is much better, as the data are set as a parameter to the writeValue(...) and received as value in peripheralDidUpdateValueFor(...). They are not shared.

这篇关于Android每个连接间隔接收多个BLE数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆