Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added retries for connect and autoconnect #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions blessed/src/main/java/com/welie/blessed/BluetoothPeripheral.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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, delayBeforeConnectRetryInMs: Long = DELAY_BEFORE_CONNECT_RETRY_IN_MS) {
if (state == BluetoothProfile.STATE_DISCONNECTED) {
scope.launch {
delay(DIRECT_CONNECTION_DELAY_IN_MS)
Expand All @@ -455,14 +455,23 @@ 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 ${delayBeforeConnectRetryInMs}ms...")
delay(delayBeforeConnectRetryInMs)
connect(tryCount - 1)
}
} else {
Logger.e(TAG, "No more tries to connect to '%s'", name)
}
}
}

/**
* 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, 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) {
Expand All @@ -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 ${delayBeforeConnectRetryInMs}ms...")
delay(delayBeforeConnectRetryInMs)
autoConnect(tryCount - 1)
}
} else {
Logger.e(TAG, "No more tries to reconnect to '%s'", name)
}
}
}

Expand Down Expand Up @@ -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"
Expand Down