Skip to content

Commit

Permalink
[android] Feat: retry GATT_ERROR 133 on device.connectGatt fail (#34697)
Browse files Browse the repository at this point in the history
* feat: retry GATT_ERROR 133 on device.connectGatt fail

* chore: add constant for error code 133
  • Loading branch information
j0tunn authored and pull[bot] committed Nov 19, 2024
1 parent 73fb48c commit 1553585
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions src/platform/android/java/chip/platform/AndroidBleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private static class BleMtuDenylist {
private static final int MSG_BLE_SCAN = 0;
private static final int MSG_BLE_CONNECT = 1;
private static final int MSG_BLE_CONNECT_SUCCESS = 2;
private static final int MSG_BLE_CONNECT_RETRY = 3;
private static final int MSG_BLE_FAIL = 99;

private static final int BLE_TIMEOUT_MS = 10000;
Expand Down Expand Up @@ -501,7 +502,10 @@ public void handleMessage(Message msg) {
break;
case MSG_BLE_CONNECT:
stopBleScan();
connectBLE(msg.obj);
connectBLE(new ConnectionGattCallback((BluetoothDevice) msg.obj));
break;
case MSG_BLE_CONNECT_RETRY:
connectBLE((ConnectionGattCallback) msg.obj);
break;
case MSG_BLE_CONNECT_SUCCESS:
bleConnectSuccess(msg.obj);
Expand Down Expand Up @@ -567,28 +571,39 @@ private void stopBleScan() {
}
}

private void connectBLE(Object bluetoothDeviceObj) {
if (bluetoothDeviceObj == null) {
return;
}

private void connectBLE(ConnectionGattCallback callback) {
// Fail Timer reset.
mConnectionHandler.removeMessages(MSG_BLE_FAIL);
mConnectionHandler.sendEmptyMessageDelayed(MSG_BLE_FAIL, BLE_TIMEOUT_MS);

@SuppressWarnings("unchecked")
BluetoothDevice device = (BluetoothDevice) bluetoothDeviceObj;
BluetoothDevice device = callback.getTargetDevice();

Log.i(TAG, "Connecting");
BluetoothGatt gatt = device.connectGatt(mContext, false, new ConnectionGattCallback());
device.connectGatt(mContext, false, callback, BluetoothDevice.TRANSPORT_LE);
}

class ConnectionGattCallback extends AndroidBluetoothGattCallback {
private static final int STATE_INIT = 1;
private static final int STATE_DISCOVER_SERVICE = 2;
private static final int STATE_REQUEST_MTU = 3;

/// Unfortunately GATT 133 is very generic, however among other things
/// it may also show up on device out of range or BLE being closed due to
/// background task termination, hence it is a candidate to retry connectivity.
private static final int kGattRetryableErrorStatus = 133;

private int mState = STATE_INIT;
private BluetoothDevice mDevice;
private int connectRetriesLeft = 3;

public ConnectionGattCallback(BluetoothDevice device) {
mDevice = device;
}

public BluetoothDevice getTargetDevice() {
return mDevice;
}

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
Expand All @@ -599,9 +614,27 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
mState = STATE_DISCOVER_SERVICE;
gatt.discoverServices();
return;
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
}

if (newState == BluetoothProfile.STATE_DISCONNECTED
&& status == kGattRetryableErrorStatus
&& connectRetriesLeft-- > 0) {
Log.i(TAG, "Retries left: " + connectRetriesLeft + ". Retrying connect...");

gatt.close();

Message msg = mConnectionHandler.obtainMessage();
msg.what = MSG_BLE_CONNECT_RETRY;
msg.obj = (Object) this;
mConnectionHandler.sendMessage(msg);

return;
}

if (newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.i(TAG, "Services Disconnected");
}

mConnectionHandler.sendEmptyMessage(MSG_BLE_FAIL);
}

Expand Down

0 comments on commit 1553585

Please sign in to comment.