Android: automatically reconnect BLE devices

My goal is to connect an Android device to a BLE device that it previously connected to without user intervention , in the same way as a classic BT pairing device (even works through power cycles).

One of the ideas of BTLE devices is that one maintains a state of service, communication, and permission, so reconnecting is VERY fast and consumes very little power on the periphery.

What I did seems to work, but it works poorly.

The first step is to connect or pair and connect to a new device, setting the auto-connect option to true. When the device shuts down, do not call gatt.close (). Wherever I look, I see what needs to be called gatt.close (). But if I call gatt.close (), the central Android application will never reconnect. I have tested this many times.

If I did not call gatt.close () and did not turn on the power of Android, usually an automatic connection occurs. This can sometimes be time consuming, especially after version 5.0. However, it is unreliable and may be unreliable due to the very low scan cycle, and the device exits the advertisement before the scan cycle actually detects the advertisement. I'm not sure, because there is no way to detect a scan operation, as there is an advertisement! It is also possible that the scan will stop after a certain time, but there is no documentation on it.

So, it seems to me that I need to make sure that the background scan frequency used by Android is set to a higher duty cycle (maybe only in 5.0 and higher) when auto-connect is established, but I don’t know how to do it. I do not want to run my own scan, but somehow set the background scan frequency used by Android to reconnect. Does anyone know how to do this? Does anyone really know how autoconnect and gatt.close () work?

Maybe the auto-join was NOT intended to reconnect, as described above?

+5
source share
1 answer

Well, after many trials and tribulations, that’s how I’ll best connect Android to automatic connection with a single user action, to select the device first (if you use the settings menu and then the first pairing).

You must catch the paired event in BroadcastReceiver and set BluetoothDevice.connectGatt () so that the auto-connect matches true. Then, when the device disconnects, call gatt.connect ().

Update. Although the above approach works in general, it is sometimes painfully slow, probably because the pending connection uses extremely conservative scan speeds. Another disadvantage is that for each device that you want to automatically connect, you need to save a BluetoothGatt object that is performing a pending connection. In the embedded world, this is insanity. Instead, what you do is constantly scanned and connected to the device you want, checking its ads. One saves only the minimum amount of data about the device (services, its paired state and keys, etc.). When an ad is captured, you see if it is one of your famous devices and connects, if so. I tried the equivalent on Android. Check all the time (low power level) and connect to ads of interest and maintain a class representing a known device. There are some unpleasant details in this approach (for example, disabling scanning when connecting and restarting after connecting), but basically it works without the overhead of maintaining connections. BUT there is one exception that I do not understand. The scanner found that advertisements with a preinstalled device are never visible. However, if I call a pending connection to this device, I reconnect. I do not understand this. On my embedded platforms, it works as it should. If someone else tried this approach to automatically reconnect, please share your impressions!

+1
source

Source: https://habr.com/ru/post/1238991/


All Articles