Android:自动重新连接 BLE 设备 [英] Android: Auto-reconnect BLE devices

查看:165
本文介绍了Android:自动重新连接 BLE 设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是让 Android 设备重新连接到之前连接过的 BLE 设备,无需用户干预,就像经典 BT 配对设备所做的那样(甚至可以通过电源循环).

My goal is to get the Android device to reconnect to a BLE device that it has previously connected to without user intervention in the same way it does for a classic BT paired device does (even works through power cycles).

BTLE 设备的一个想法是保存服务、绑定和启用状态,这样重新连接非常快,并且在外围设备上消耗很少的电量.

One of the ideas of BTLE devices is that one saves service, bonding, and enabling states such that a reconnect is VERY fast and consumes very little power on the peripheral.

我所做的似乎有效,但效果不佳.

What I have done seems to work but it works poorly.

第一步是连接或配对并连接到新设备,将autoconnect"参数设置为true".当设备断开连接时,不要调用 gatt.close().我看到的每个地方都应该调用 gatt.close().但是如果我调用 gatt.close() ,Android 中央应用程序永远不会重新连接.我已经测试过很多次了.

The first step is to connect or pair and connect to a new device setting the 'autoconnect' parameter to 'true'. When the device disconnects, do not call gatt.close(). Everywhere I look I see that one should call gatt.close(). But if I do call gatt.close() the Android central app never reconnects. I have tested this many times.

如果我没有调用 gatt.close() 并且没有重启 Android,通常会发生自动连接.有时可能需要很长时间,尤其是在 5.0 版本之后.然而,它是不可靠的,并且由于非常低的占空比扫描周期和设备在扫描周期实际检测到广告之前退出广告而可能不可靠.我不确定,因为没有办法像有广告那样检测扫描操作!扫描也可能在一段时间后停止,但没有相关文档.

If I have not called gatt.close() and have not power cycled the Android, the auto-connection usually happens. Sometimes it can take a long time, especially after version 5.0. It is, however, unreliable and it may be unreliable due to a very low-duty scan cycle and the device quitting advertising before a scan cycle actually detects the advertisement. I am not sure because there is no way to detect the scanning operation like there is advertisements! It is also possible the scanning stops after a certain amount of time but there is no documentation on that.

所以我认为我需要做的是在设置自动连接时以某种方式将 Android 使用的后台扫描速率设置为更高的占空比(仅在 5.0 及更高版本中可能),但我不知道如何做这个.我不想开始自己的扫描,而是以某种方式设置 Android 用于重新连接的后台扫描速率.有谁知道如何做到这一点?有人真的知道 autoconnect 和 gatt.close() 是如何工作的吗?

So what I think I need to do is to somehow set the background scan rate used by the Android to a higher duty cycle (only possible in 5.0 and up) when auto-connect has been set but I do not know how to do this. I do not want to start my own scan but somehow set the background scanning rate used by Android for the reconnect. Does anyone know how to do this? Does anyone really know how autoconnect and gatt.close() are to work?

也许自动连接不是按照我上面所说的重新连接?

Maybe the auto-connect was NOT meant to re-connect as I indicated above?

推荐答案

经过多次试验和磨难,这就是我最好的让 Android 自动连接的方式,唯一的用户操作是首先选择设备(如果使用设置菜单然后第一次配对).

Well after many trials and tribulations this is how I best get the Android to auto connect with the only user action being to first select the device (if using the settings menu then first pairing).

您必须在 BroadcastReceiver 中捕获配对事件并执行 BluetoothDevice.connectGatt() 将自动连接设置为 true.然后当设备断开连接时,调用 gatt.connect().

You have to trap the paired event in a BroadcastReceiver and do a BluetoothDevice.connectGatt() setting the autoconnect to true. Then when the device disconnects, invoke a gatt.connect().

更新:虽然上述方法在一般情况下有效,但有时会非常缓慢,可能是因为待处理的连接使用极其保守的扫描速率.另一个缺点是,对于您想要自动重新连接的每个设备,您必须保持一个 BluetoothGatt 对象执行挂起的连接.在嵌入式世界中,这是疯狂的.相反,人们所做的是通过检查其广告连续扫描并连接到所需设备.一个只保存关于设备的最少量数据(服务、其配对状态和密钥等).当广告被捕获时,您会看到它是否是您已知的设备之一,如果是,则连接到它.我在 Android 上尝试过类似的方法.一直扫描(低功耗)并连接到感兴趣的广告,并维护一个代表已知设备的类.这种方法有一些烦人的细节(比如在连接时关闭扫描和连接后重新启动),但它基本上没有维护连接的开销.但是有一个我不明白的例外.扫描仪永远不会看到一个预先配对的设备的广告.但是,如果我调用与此设备的挂起连接,我将重新连接.我完全不明白这一点.在我的嵌入式平台上,它可以正常工作.如果有人尝试过这种自动重新连接的方法,请分享您的经验!

Update: While the above approach works in general, it is sometimes agonizingly slow probably because the pending connection uses extremely conservative scan rates. The other downside is that for each device you want to auto-reconnect to you have to keep a BluetoothGatt object performing a pending connection. In the embedded world this is insane. Instead what one does is continuously scan and connect to a desired device by inspecting its advertisement. One saves only the minimal amount of data about the device (the services, its paired state and keys, etc.). When an advertisement is captured you see if it is one of your known devices and connect to if it is. I tried the equivalent on Android. Scan all the time (low power rate) and connect to advertisements of interest, and maintain a class representing a known device. There are some annoying details in this approach (like turning off scanning while connecting and restarting after connected) but it basically works without the overhead of maintaining connections. BUT there is one exception I do not understand. One pre-paired device's advertisements are never seen by the scanner. However, if I invoke a pending connection to this device, I re-connect. I do not understand this at all. On my embedded platforms it works as it should. If anyone else has tried this approach for auto-reconnecting, please share your experiences!

我发现了 Android 看不到预配对设备的原因.Android 仅在设备响应扫描请求时报告扫描结果.配对后,此设备仅发出广告并忽略扫描请求,因此 Android 系统不会在 ScanCallback 中传递其广告.因此,为了使用扫描方法工作,我必须对那些特定设备使用挂起连接方法.好像赢不了一样!

I have discovered the reason the pre-paired device is not seen by Android. Android only reports scan results IF the device responds to a scan request. Once paired, this device only emits advertisements and ignores scan requests, so the Android system does not pass up its advertisements in the ScanCallback. Thus in order to work using the scan approach, I have to use the pending connect approach for those specific devices. It just seems like you can't win!

============ 2020 年更新

============= UPDATE 2020

许多年过去了,我对后台扫描方法有了更多的经验.如果保持支持的平台 5 及更高版本,则只能使用最新的扫描仪 API 并使用过滤器,无需自己解码原始广告.我还发现,如果您在连接时不关闭扫描,则连接和重新连接会更快捷.我知道它违反了所有文档,但它有效并且在某些平台上允许发生否则不会发生的连接.此外,迄今为止,我发现只有一个(健康)设备需要挂起连接.免责声明:我所使用的只是健康设备.

Many years have passed and I have a lot more experience with the background scan approach. If one keeps the supported platforms 5 and up, one can use only the newest scanner APIs and use filters, eliminating the need to decode the raw advertisements yourself. I have also found that connection and re-connection is snappier if you DONT turn off scanning while connecting. I know it goes against all documentation, but it works and on some platforms allowed connections to happen that otherwise did not. Also, to date, I have found only one (health) device that needs pending connects. Disclaimer: All I have ever worked with is health devices.

这篇关于Android:自动重新连接 BLE 设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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