@@ -89,6 +89,7 @@ private static class BleMtuDenylist {
89
89
private static final int MSG_BLE_SCAN = 0 ;
90
90
private static final int MSG_BLE_CONNECT = 1 ;
91
91
private static final int MSG_BLE_CONNECT_SUCCESS = 2 ;
92
+ private static final int MSG_BLE_CONNECT_RETRY = 3 ;
92
93
private static final int MSG_BLE_FAIL = 99 ;
93
94
94
95
private static final int BLE_TIMEOUT_MS = 10000 ;
@@ -501,7 +502,10 @@ public void handleMessage(Message msg) {
501
502
break ;
502
503
case MSG_BLE_CONNECT :
503
504
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 );
505
509
break ;
506
510
case MSG_BLE_CONNECT_SUCCESS :
507
511
bleConnectSuccess (msg .obj );
@@ -567,20 +571,16 @@ private void stopBleScan() {
567
571
}
568
572
}
569
573
570
- private void connectBLE (Object bluetoothDeviceObj ) {
571
- if (bluetoothDeviceObj == null ) {
572
- return ;
573
- }
574
-
574
+ private void connectBLE (ConnectionGattCallback callback ) {
575
575
// Fail Timer reset.
576
576
mConnectionHandler .removeMessages (MSG_BLE_FAIL );
577
577
mConnectionHandler .sendEmptyMessageDelayed (MSG_BLE_FAIL , BLE_TIMEOUT_MS );
578
578
579
579
@ SuppressWarnings ("unchecked" )
580
- BluetoothDevice device = ( BluetoothDevice ) bluetoothDeviceObj ;
580
+ BluetoothDevice device = callback . getTargetDevice () ;
581
581
582
582
Log .i (TAG , "Connecting" );
583
- BluetoothGatt gatt = device .connectGatt (mContext , false , new ConnectionGattCallback () );
583
+ device .connectGatt (mContext , false , callback , BluetoothDevice . TRANSPORT_LE );
584
584
}
585
585
586
586
class ConnectionGattCallback extends AndroidBluetoothGattCallback {
@@ -589,6 +589,16 @@ class ConnectionGattCallback extends AndroidBluetoothGattCallback {
589
589
private static final int STATE_REQUEST_MTU = 3 ;
590
590
591
591
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
+ }
592
602
593
603
@ Override
594
604
public void onConnectionStateChange (BluetoothGatt gatt , int status , int newState ) {
@@ -599,9 +609,28 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState
599
609
mState = STATE_DISCOVER_SERVICE ;
600
610
gatt .discoverServices ();
601
611
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 ) {
603
631
Log .i (TAG , "Services Disconnected" );
604
632
}
633
+
605
634
mConnectionHandler .sendEmptyMessage (MSG_BLE_FAIL );
606
635
}
607
636
0 commit comments