From f33ae9cc3ede1b08e089c7a6210b9823bbc821c6 Mon Sep 17 00:00:00 2001 From: Steve Grosbois Date: Wed, 1 Jun 2022 10:00:12 +0200 Subject: [PATCH 1/2] fix: Added retries for connect and autoconnect --- .../com/welie/blessed/BluetoothPeripheral.kt | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt b/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt index a4d9570..e52d322 100644 --- a/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt +++ b/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt @@ -434,7 +434,7 @@ class BluetoothPeripheral internal constructor( /** * Connect directly with the bluetooth device. This call will timeout in max 30 seconds (5 seconds on Samsung phones) */ - fun connect() { + fun connect(tryCount: Int = 10) { if (state == BluetoothProfile.STATE_DISCONNECTED) { scope.launch { delay(DIRECT_CONNECTION_DELAY_IN_MS) @@ -455,6 +455,15 @@ class BluetoothPeripheral internal constructor( } } else { Logger.e(TAG, "peripheral '%s' not yet disconnected, will not connect", name) + if (tryCount > 0) { + scope.launch { + Logger.d(TAG, "Retrying in ${DELAY_BEFORE_CONNECT_RETRY_IN_MS}ms...") + delay(DELAY_BEFORE_CONNECT_RETRY_IN_MS) + connect(tryCount - 1) + } + } else { + Logger.e(TAG, "No more tries to connect to '%s'", name) + } } } @@ -462,7 +471,7 @@ class BluetoothPeripheral internal constructor( * Try to connect to a device whenever it is found by the OS. This call never times out. * Connecting to a device will take longer than when using connect() */ - fun autoConnect() { + fun autoConnect(tryCount: Int = 10) { // Note that this will only work for devices that are known! After turning BT on/off Android doesn't know the device anymore! // https://stackoverflow.com/questions/43476369/android-save-ble-device-to-reconnect-after-app-close if (state == BluetoothProfile.STATE_DISCONNECTED) { @@ -482,7 +491,16 @@ class BluetoothPeripheral internal constructor( } } } else { - Logger.e(TAG, "peripheral '%s' not yet disconnected, will not connect", name) + Logger.e(TAG, "peripheral '%s' not yet disconnected, will not reconnect", name) + if (tryCount > 0) { + scope.launch { + Logger.d(TAG, "Retrying in ${DELAY_BEFORE_CONNECT_RETRY_IN_MS}ms...") + delay(DELAY_BEFORE_CONNECT_RETRY_IN_MS) + autoConnect(tryCount - 1) + } + } else { + Logger.e(TAG, "No more tries to reconnect to '%s'", name) + } } } @@ -1589,6 +1607,9 @@ class BluetoothPeripheral internal constructor( // The average time it takes to complete requestConnectionPriority private const val AVG_REQUEST_CONNECTION_PRIORITY_DURATION = 500L + // Delay to use before retrying a connection + private const val DELAY_BEFORE_CONNECT_RETRY_IN_MS = 1000L + // Error message constants private const val PERIPHERAL_NOT_CONNECTED = "peripheral not connected" private const val VALUE_BYTE_ARRAY_IS_EMPTY = "value byte array is empty" From 152039e904cb8181b5fce8a6cf5a989e7c1f819e Mon Sep 17 00:00:00 2001 From: Steve Grosbois Date: Mon, 22 May 2023 11:53:42 +0200 Subject: [PATCH 2/2] fix: Added parameter for delay before retry to connect and autoConnect methods --- .../java/com/welie/blessed/BluetoothPeripheral.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt b/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt index e52d322..89feee5 100644 --- a/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt +++ b/blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt @@ -434,7 +434,7 @@ class BluetoothPeripheral internal constructor( /** * Connect directly with the bluetooth device. This call will timeout in max 30 seconds (5 seconds on Samsung phones) */ - fun connect(tryCount: Int = 10) { + fun connect(tryCount: Int = 10, delayBeforeConnectRetryInMs: Long = DELAY_BEFORE_CONNECT_RETRY_IN_MS) { if (state == BluetoothProfile.STATE_DISCONNECTED) { scope.launch { delay(DIRECT_CONNECTION_DELAY_IN_MS) @@ -457,8 +457,8 @@ class BluetoothPeripheral internal constructor( Logger.e(TAG, "peripheral '%s' not yet disconnected, will not connect", name) if (tryCount > 0) { scope.launch { - Logger.d(TAG, "Retrying in ${DELAY_BEFORE_CONNECT_RETRY_IN_MS}ms...") - delay(DELAY_BEFORE_CONNECT_RETRY_IN_MS) + Logger.d(TAG, "Retrying in ${delayBeforeConnectRetryInMs}ms...") + delay(delayBeforeConnectRetryInMs) connect(tryCount - 1) } } else { @@ -471,7 +471,7 @@ class BluetoothPeripheral internal constructor( * Try to connect to a device whenever it is found by the OS. This call never times out. * Connecting to a device will take longer than when using connect() */ - fun autoConnect(tryCount: Int = 10) { + fun autoConnect(tryCount: Int = 10, delayBeforeConnectRetryInMs: Long = DELAY_BEFORE_CONNECT_RETRY_IN_MS) { // Note that this will only work for devices that are known! After turning BT on/off Android doesn't know the device anymore! // https://stackoverflow.com/questions/43476369/android-save-ble-device-to-reconnect-after-app-close if (state == BluetoothProfile.STATE_DISCONNECTED) { @@ -494,8 +494,8 @@ class BluetoothPeripheral internal constructor( Logger.e(TAG, "peripheral '%s' not yet disconnected, will not reconnect", name) if (tryCount > 0) { scope.launch { - Logger.d(TAG, "Retrying in ${DELAY_BEFORE_CONNECT_RETRY_IN_MS}ms...") - delay(DELAY_BEFORE_CONNECT_RETRY_IN_MS) + Logger.d(TAG, "Retrying in ${delayBeforeConnectRetryInMs}ms...") + delay(delayBeforeConnectRetryInMs) autoConnect(tryCount - 1) } } else {