Change the timeout of a low-energy Bluetooth battery or clear the read stream for faster detection of disconnect events

I'm looking for an android to clear the characteristics that the application receives from the Ble device, or at least learn from the data that the connection was lost as soon as it actually happens, except for about 15 seconds after disconnecting it. If there is a way to change the timeout of the gatt connection, it will be much better.

To repeat in a different form, I would like the solution (or a link that might explain) to detect the BLE device disconnecting faster than any timeout value currently, by viewing if the value I accept is fresher by resetting the flag or changing the disconnect timeout on the gatt side, so I see that it disconnects within a second to call another code.

+6
source share
5 answers

Other answers here may be better than this, but that was my solution to the problem. Be sure to try Emil's answer before using this.

What I have been doing since then was too slow to wait for it to check rssi, as it always changes. If there is a period of time, say, 3 seconds, when the value remains unchanged, it is disconnected from the device. It lasts 15 seconds and adds its own timeout.

This would be necessary to check the signal strength. It was written a couple of years ago, so some things may need to be changed.

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status){ //check for signal strength changes. It will stay the same if we //are getting no updates if(mLastRssi == rssi){ disconnectCounter++; if(disconnectCounter> 140) { //disconnect logic as if we are done with connection //maybe start listening for device again to reconnect disconnectCounter = 0; } } else{ //we are connected. reset counter disconnectCounter = 0; } //store last value as global for comparison mLastRssi= rssi; } } 

Somewhere in a loop, call

 mBluetoothGatt.readRemoteRssi() 
+1
source

I donโ€™t know if this will help, but you can use (if any) the periodic data that is passed to gatt. So, if, for example, you measure 1 second of periodicity, you can do something like:

 // runnable to detect the lack of activity: private final Runnable watchDog = new Runnable() { @Override public void run() { measurement_timeout--; if(measurement_timeout==0) { Log.d("BLE_CONTROLLER", "PROBE WITH NO ACTIVITY"); } } }; // this should be in the reception of the periodic data: measurement_timeout++; mHandler.postDelayed(watchDog, 3000); 

So, "measure_timeout" will work as an actual timeout, when upon reaching 0 it means that you do not have data received in a period of 3000 ms. Please note that you must have an observation time> 2 *.

0
source

The only way I was able to get gatt disconnected quickly was to ensure that the peripheral device sent a disconnect BLE command before disconnecting or disconnecting the connection.

After the android receives the disconnect command, gatt immediately restores, and does not take 15 seconds to implement the peripheral device.

It would seem that most peripheral devices do not bother and simply disappear.

Obviously, this approach is only possible if you can change the peripheral device.

0
source

The correct way is to use the request to update the connection parameters from the peripheral side to change the timeout to a lower value.

0
source

Android has a callback:

 BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange( BluetoothGatt gatt,int status,int newState){ if(newState == BluetoothProfile.STATE_DISCONNECTED){ //your code here } } } 
-1
source

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


All Articles