在Android上同时强大地与多个BLE设备通信

在Android上同时强大地与多个BLE设备通信

本文介绍了在Android上同时强大地与多个BLE设备通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管未记录,但使用Android BLE API的传统观点是某些操作(例如读/写特征&描述符应一次完成(尽管某些设备比其他设备更宽松).但是,我不清楚该策略是仅适用于单个连接还是适用于所有活动连接.

Although undocumented, conventional wisdom using the Android BLE apis is that certain operations like reading / writing Characteristics & Descriptors should be done one at a time (although some devices are more lenient than others). However, I am not clear on whether this policy should apply only to a single connection, or across all active connections.

我听说最好一次启动一个到设备的连接.那可能是应该在所有设备之间串行执行的操作(connect/connectGatt)的示例.

I've heard that its best to initiate connections to devices one at a time. That might be an example of operations (connect / connectGatt) which should be executed serially among all devices.

但是对于其他操作,例如读写特征,如果每个连接串行执行操作是否足够好,还是我需要在所有设备之间共享一些全局操作队列,以便在所有设备之间仅执行一个操作?/p>

But for other operations, like reading and writing Characteristics, is it good enough if each connection executes operations serially, or do I need some global operation queue shared among all devices so that between all devices, only one operation is executing?

推荐答案

在Android上,每个BluetoothGatt对象您一次只能执行一项操作(请求mtu,发现服务,读取/写入特征/descriptor),否则事情将会出错.您必须等到相应的回调被调用后,才能执行下一个操作.

On Android, per BluetoothGatt object you should only execute one operation at a time (request mtu, discover services, read/write characteristic/descriptor) otherwise things will go wrong. You have to wait until the corresponding callback gets called until you can execute the next operation.

关于同时具有到多个设备的挂起连接,如果您使用autoConnect = true,则没有问题,但是,如果您使用autoConnect = false,则Android的蓝牙堆栈将仅尝试一次连接到一台设备.如果有多个未完成,它将排队连接请求.有一个特殊的错误,即它无法取消仍在队列中的挂起连接(当您调用.disconnect()或.close()时),但是最近已在Android中修复.

Regarding having pending connections to multiple devices at the same time, if you use autoConnect=true then there is no problem but if you use autoConnect=false then Android's bluetooth stack will only attempt to connect to one device at a time, meaning it will enqueue the connection requests if there are more than one outstanding. There is one particular bug where it fails to cancel a pending connection that is still in the queue (when you call .disconnect() or .close()) however, that was recently fixed in Android.

请注意,还有一个最大数量的连接/待处理的连接/gatt对象,对于这些行为,完全没有记录,超过这些限制时会发生什么.在最佳情况下,您只会收到带有错误状态的回调,但在某些情况下,我已经看到android蓝牙堆栈陷入了一个无限循环,在该循环中,每次迭代都告诉蓝牙控制器连接到设备,但控制器回传错误代码已达到最大连接数.

Note that there is also a maximum number of connections/pending connections/gatt objects for which the behaviour is completely undocumented what happens when you exceed these limits. In the best cases you simply get a callback with error status but in some cases I've seen that the android bluetooth stack gets stuck in an endless loop where it in each iteration tells the bluetooth controller to connect to a device but the controller sends back the error code maximum connections reached.

这篇关于在Android上同时强大地与多个BLE设备通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:14