Why does `ACTION_GATT_DISCONNECTED` take so long to update the status?

I use Bluetooth Low Energy to connect to my Galaxy S4. After connecting, the connection status will be updated in the function

private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); String intentAction; if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { Log.d(TAG,"Connected"); } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { Log.d(TAG,"Disconnected"); } } } 

In which status is obtained from

 // Implements callback methods for GATT events that the app cares about. For example, // connection change and services discovered. private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { String intentAction; if (newState == BluetoothProfile.STATE_CONNECTED) { intentAction = ACTION_GATT_CONNECTED; mConnectionState = STATE_CONNECTED; broadcastUpdate(intentAction); Log.i(TAG, "Connected to GATT server."); // Attempts to discover services after successful connection. Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices()); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { intentAction = ACTION_GATT_DISCONNECTED; mConnectionState = STATE_DISCONNECTED; Log.i(TAG, "Disconnected from GATT server."); broadcastUpdate(intentAction); } } 

However, ACTION_GATT_DISCONNECTED takes 10 seconds to update when the connection between the BLE device and the phone is lost. In the case of ACTION_GATT_CONNECTED it updates so fast for about 1 second. Is it possible to reduce the shutdown status in BLE? Thank you all

+6
source share
2 answers

There is a “communication control timeout”, this is defined as the time during which communication should be maintained until failure. That's why it takes so long when your peripheral device stops communicating.

 // Link supervision timeout is measured in N * 10ms int timeout = 2000; // 20s 

You can find the corresponding code starting at line 1646 in com.android.bluetooth.gatt.GattService

https://android.googlesource.com/platform/packages/apps/Bluetooth/+/2b42a4906be952d3761d8b47134fba5a277a7765/src/com/android/bluetooth/gatt/GattService.java

---- Edit ----

The big problem seems to be a known bug with lollipop and kitcat, in which bluedroid caches devices during discovery when it should not, see question https://code.google.com/p/android/issues/detail?id= 81130 & q = BLE & sort = -stars & colspec = ID% 20Status% 20Priority% 20Owner% 20Summary% 20Stars% 20Reporter% 20Opened

It is not possible to edit the timeout if you cannot compile your own rom system and use a flash device.

It is possible to crack this problem, but it is not recommended because it refers to a hidden internal API that may not exist starting with marshmallows (since bluedroid fell for bluez).

Breaking:

After calling the disconnect and close function, the discovery cache is updated by reflexively invoking the update method.

 BluetoothGatt localBluetoothGatt = gatt; Method localMethod = localBluetoothGatt.getClass().getMethod("refresh", new Class[0]); if (localMethod != null) { localMethod.invoke(localBluetoothGatt, new Object[0])).booleanValue(); } 
+3
source

What BLE device is used to test the connection ?. It is possible that the device does close the connection after a while. In some cases, the BLE device will close the connection after a while, because it may be necessary to write some of the characteristics that you requested. In some cases, it really depends on the device’s embedded code to determine when to close the connection.

In my situation, my BLE device will close the connection after 5 seconds. This 5 seconds looks like a buffer for the device to send me all the pending records, read the characteristics that I requested.

We set our BLE device to broadcast completion notification, which I used to notify the user that the connection was closed, but in the background the BLE device is still open for 5 seconds so that it can write some characteristics or the user may want to reconnect.

+1
source

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


All Articles