Skip to content

Commit 3249646

Browse files
authored
Merge e730a88 into 84ee115
2 parents 84ee115 + e730a88 commit 3249646

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/platform/android/java/chip/platform/AndroidBleManager.java

+38-9
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private static class BleMtuDenylist {
8989
private static final int MSG_BLE_SCAN = 0;
9090
private static final int MSG_BLE_CONNECT = 1;
9191
private static final int MSG_BLE_CONNECT_SUCCESS = 2;
92+
private static final int MSG_BLE_CONNECT_RETRY = 3;
9293
private static final int MSG_BLE_FAIL = 99;
9394

9495
private static final int BLE_TIMEOUT_MS = 10000;
@@ -501,7 +502,10 @@ public void handleMessage(Message msg) {
501502
break;
502503
case MSG_BLE_CONNECT:
503504
stopBleScan();
504-
connectBLE(msg.obj);
505+
connectBLE(new ConnectionGattCallback((BluetoothDevice) msg.obj));
506+
break;
507+
case MSG_BLE_CONNECT_RETRY:
508+
connectBLE((ConnectionGattCallback) msg.obj);
505509
break;
506510
case MSG_BLE_CONNECT_SUCCESS:
507511
bleConnectSuccess(msg.obj);
@@ -567,20 +571,16 @@ private void stopBleScan() {
567571
}
568572
}
569573

570-
private void connectBLE(Object bluetoothDeviceObj) {
571-
if (bluetoothDeviceObj == null) {
572-
return;
573-
}
574-
574+
private void connectBLE(ConnectionGattCallback callback) {
575575
// Fail Timer reset.
576576
mConnectionHandler.removeMessages(MSG_BLE_FAIL);
577577
mConnectionHandler.sendEmptyMessageDelayed(MSG_BLE_FAIL, BLE_TIMEOUT_MS);
578578

579579
@SuppressWarnings("unchecked")
580-
BluetoothDevice device = (BluetoothDevice) bluetoothDeviceObj;
580+
BluetoothDevice device = callback.getTargetDevice();
581581

582582
Log.i(TAG, "Connecting");
583-
BluetoothGatt gatt = device.connectGatt(mContext, false, new ConnectionGattCallback());
583+
device.connectGatt(mContext, false, callback, BluetoothDevice.TRANSPORT_LE);
584584
}
585585

586586
class ConnectionGattCallback extends AndroidBluetoothGattCallback {
@@ -589,6 +589,16 @@ class ConnectionGattCallback extends AndroidBluetoothGattCallback {
589589
private static final int STATE_REQUEST_MTU = 3;
590590

591591
private int mState = STATE_INIT;
592+
private BluetoothDevice mDevice;
593+
private int connectRetriesLeft = 3;
594+
595+
public ConnectionGattCallback(BluetoothDevice device) {
596+
mDevice = device;
597+
}
598+
599+
public BluetoothDevice getTargetDevice() {
600+
return mDevice;
601+
}
592602

593603
@Override
594604
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
@@ -599,9 +609,28 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
599609
mState = STATE_DISCOVER_SERVICE;
600610
gatt.discoverServices();
601611
return;
602-
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
612+
}
613+
614+
if (newState == BluetoothProfile.STATE_DISCONNECTED
615+
&& status == 133
616+
&& connectRetriesLeft-- > 0) {
617+
Log.i(TAG, "GATT_ERROR 133. Retrying connect...");
618+
Log.i(TAG, "Retries left: " + connectRetriesLeft);
619+
620+
gatt.close();
621+
622+
Message msg = mConnectionHandler.obtainMessage();
623+
msg.what = MSG_BLE_CONNECT_RETRY;
624+
msg.obj = (Object) this;
625+
mConnectionHandler.sendMessage(msg);
626+
627+
return;
628+
}
629+
630+
if (newState == BluetoothProfile.STATE_DISCONNECTED) {
603631
Log.i(TAG, "Services Disconnected");
604632
}
633+
605634
mConnectionHandler.sendEmptyMessage(MSG_BLE_FAIL);
606635
}
607636

0 commit comments

Comments
 (0)