retryBleOperation

suspend fun <T> retryBleOperation(count: Int = 3, delayMs: Long = 250, tag: String = "BLE", retryWhile: () -> Boolean = { true }, block: suspend () -> T): T(source)

Retries a BLE operation with bounded exponential backoff and jitter.

Each retry waits delayMs * 2^(attempt-1), capped at MAX_RETRY_DELAY_MS, with a random ±25% jitter applied to avoid synchronised retry storms when multiple operations fail in lockstep (e.g. a TX/RX pair both failing the same STATUS_GATT_BUSY window).

Return

The result of the operation.

Parameters

count

Total attempt count (default 3).

delayMs

Initial delay before the first retry. Subsequent delays grow exponentially.

tag

Tag for log prefixes.

retryWhile

Checked after a failed attempt and again before each retry. Returning false rethrows the latest failure immediately without logging or waiting, which lets callers revoke retries when the operation's session is no longer current. The initial attempt always runs.

block

The operation to perform.

Throws

If the operation fails after all attempts. CancellationException is always re-thrown immediately.