From a393efe3ba271b34efbcb8d2238518b5404cec8a Mon Sep 17 00:00:00 2001 From: Kaiser Li Date: Tue, 16 Apr 2024 14:41:17 +0800 Subject: [PATCH] fix: reset connected if DISCONNECTED is received between connect and initialize for autoconnected --- .../no/nordicsemi/android/ble/BleManagerHandler.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java b/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java index fa5fca10..1c8929c5 100644 --- a/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java +++ b/ble/src/main/java/no/nordicsemi/android/ble/BleManagerHandler.java @@ -2217,7 +2217,15 @@ public void onConnectionStateChange(@NonNull final BluetoothGatt gatt, if (cr != null && cr.shouldAutoConnect() && initialConnection && gatt.getDevice().getBondState() == BluetoothDevice.BOND_BONDED) { - log(Log.DEBUG, () -> "autoConnect = false called failed; retrying with autoConnect = true"); + log(Log.DEBUG, () -> "autoConnect = false called failed; retrying with autoConnect = true" + (connected ? "; reset connected to false" : "")); + + // fix:https://github.com/NordicSemiconductor/Android-BLE-Library/issues/497 + // if DISCONNECTED is received between connect and initialize, need to reset connected to make internalConnect work + if (connected) { + connected = false; + connectionState = BluetoothGatt.STATE_DISCONNECTED; + } + post(() -> internalConnect(gatt.getDevice(), cr)); return; }