Which Correct Flag of Autoconnect in Connectgatt of Ble

Which correct flag of autoConnect in connectGatt of BLE?

"Direct connect" is the opposite to "auto connect", so if you set the autoConnect parameter to false you get a "direct connect". Note that doing a "mBluetoothGatt.connect()" will also use auto connect.

Beware of https://code.google.com/p/android/issues/detail?id=69834 which is a bug affecting older versions of Android which might make your auto connections to be direct connections instead. This can be worked around with reflection.

There are a few non-documented differences between direct and auto connect:

Direct connect is a connection attempt with a 30 seconds timeout. It will suspend all current auto connects while the direct connect is in progress. If there already is a direct connect pending, the last direct connect will not be executed immediately but rather be queued up and start when the previous has finished.

With auto connect you can have multiple pending connections at the same time and they will never time out (until explicitly aborted or until Bluetooth is turned off).

If the connection was established through an auto connect, Android will automatically try to reconnect to the remote device when it gets disconnected until you manually call disconnect() or close(). Once a connection established through direct connect disconnects, no attempt is made to reconnect to the remote device.

Direct connect has a different scan interval and scan window at a higher duty than auto connect, meaning it will dedicate more radio time to listen for connectable advertisements for the remote device, i.e. the connection will be established faster.

NEW CHANGE IN ANDROID 10

Since Android 10, the direct connect queue is removed and will not temporarily pause auto connects any more. This is because direct connect now uses the white list just like auto connects. The scan window/interval is improved while a direct connect is ongoing.

Weird BLE behaviour Android

Android should be documented better. Anyway, here is the explanation:

When you call connectGatt (https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#connectGatt(android.content.Context, boolean, android.bluetooth.BluetoothGattCallback)) with autoConnect set to false, that will only make a one-shot connection to the device. When it disconnects, it doesn't reconnect.

When you instead connect using the .connect() method on the BluetoothGatt object, that is the same thing as closing the current BluetoothGatt object and then connect again with connectGatt and the autoConnect parameter set to true.

When the autoConnect parameter is set to true, that means that you would like to keep the device connected forever, i.e. for whatever reason it disconnects, Android will always try to attempt to reconnect to the device.

You can check out my list of differences for autoConnect here: https://stackoverflow.com/a/40187086/556495.

I'm trying to implement autoConnect between android app and Arduino ble

After reading more articles, I found what's the issue.

Not sure if anyone reaching out to this question but in my case, I want app to reconnect to ble automatically even after turning off and on the ble.

But I found turning off/on the phone, turning off/on ble clear the cache in android internally. so Cannot automatically reconnect in this scenario.

Please have a look. this is really helpful for me

https://medium.com/@martijn.van.welie/making-android-ble-work-part-2-47a3cdaade07

How to receive a notification on reconnect when using Android BLE Gatt

What you should do is to simply call connectGatt once with the autoConnect parameter set to true. When the device connects for the first time, you will get the onConnectionStateChange callback indicating that it's now connected. When the connection drops for some reason, you will get onConnectionStateChange indicating it's now disconnected. But you don't have to, and shouldn't, call connectGatt again. As soon as the peripheral advertises again, your Android device will automatically reconnect and you will get the onConnectionStateChange.

This will work until Bluetooth is turned off (as all Bluetooth objects are silently destroyed at that time), or your app process is terminated. After Bluetooth is turned on, you need to call connectGatt again.



Related Topics



Leave a reply



Submit