Android BLE bonding problems: pairing loops, stale bonds and how to recover
Why BLE pairing repeats, fails or breaks after a device reset on Android and iOS: bonding vs pairing, stale bonds and how to design recovery.
Most BLE bonding problems come from three things: the app doing GATT operations while bonding is still in progress, a device that pairs without storing keys so users are asked every time, and stale bonds where a reset device forgot its keys but the phone didn't. Wait for BOND_BONDED before continuing, agree the bonding rules with the firmware team, and design a recovery path for reset devices.
Key takeaways
- Pairing creates keys for one connection; bonding stores them. Prompts every time usually means no bonding.
- Accessing an encrypted characteristic triggers pairing on Android and iOS; wait for it to finish.
- Listen for ACTION_BOND_STATE_CHANGED and treat BOND_NONE after BOND_BONDING as a failure.
- A stale bond shows as status 0x3D, 22 or 5/15 errors right after connecting. Plan how users re-pair.
- Neither platform gives you a supported API to delete a bond; design the flow so users rarely need to.
Pairing bugs feel random to users and unfixable to developers, because half of the behavior lives in the phone's settings app and the other half in the firmware. They become manageable once you separate what pairing is, when it starts, and what happens when one side forgets.
Pairing vs bonding
Pairing is the key exchange that lets a connection be encrypted. Bonding is pairing plus storing the keys on both sides, so the next connection can be encrypted without asking again. If users get a pairing prompt every time they connect, the device is very likely pairing without bonding, which is a firmware setting.
Modern devices should use LE Secure Connections. The method (Just Works, passkey, numeric comparison) depends on what input and output each side has; Just Works shows a simple confirm prompt on Android and no protection against an attacker in the middle.
When does pairing start?
- Explicitly, when the app calls
createBond()on Android (needsBLUETOOTH_CONNECTon Android 12+). - Implicitly, when the app reads or writes a characteristic that requires encryption. The device answers with an “insufficient authentication/encryption” error and the phone starts pairing. iOS works only this way: there's no
createBond. - From the device, which can send a security request right after the connection.
The implicit path is where apps trip: the read that triggered pairing may fail with status 5 or 15, or wait silently while the user looks at the prompt, and the app meanwhile fires the next operations into a link that isn't ready.
Waiting for the bond on Android
Bonding progress arrives as a broadcast, not a GATT callback. Wrap it in a suspend function and don't send GATT operations until it settles:
@SuppressLint("MissingPermission")
suspend fun BluetoothDevice.awaitBond(context: Context, timeoutMs: Long = 30_000): Boolean {
if (bondState == BluetoothDevice.BOND_BONDED) return true
return withTimeoutOrNull(timeoutMs) {
callbackFlow {
val receiver = object : BroadcastReceiver() {
override fun onReceive(c: Context, i: Intent) {
val d = IntentCompat.getParcelableExtra(i, BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)
if (d?.address != address) return
trySend(i.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR))
}
}
ContextCompat.registerReceiver(context, receiver,
IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED), ContextCompat.RECEIVER_EXPORTED)
if (bondState == BluetoothDevice.BOND_NONE && !createBond()) trySend(BluetoothDevice.BOND_NONE)
awaitClose { context.unregisterReceiver(receiver) }
}.first { it == BluetoothDevice.BOND_BONDED || it == BluetoothDevice.BOND_NONE } ==
BluetoothDevice.BOND_BONDED
} ?: false
}Pause the GATT queue while this runs. A transition from BOND_BONDING back to BOND_NONE means pairing failed or the user cancelled; say so in the UI instead of retrying in a loop, which is how pairing prompts end up stacking.
A detail that confuses testers: if the app isn't in the foreground when pairing starts, many Android phones show the request as a notification instead of a dialog, and users miss it.
Stale bonds: when one side forgets
The device was factory reset, reflashed or swapped under warranty. It has no keys; the phone still has the old bond. The phone tries to encrypt with old keys and the connection fails, often with status 0x3D (MIC failure), 22, or authentication errors right after connecting. From the user's side: “it worked yesterday, now it won't connect”.
What you can and can't do:
- Detect it. A previously bonded device that now fails encryption right after connect is the signature. Log it explicitly.
- Guide the user. On both platforms the reliable fix is “forget this device” in Bluetooth settings, then pair again. Show exactly that, with the steps for their OS.
- Design it out with firmware. If a reset device advertises a flag or a new identity, the app can tell it's been reset and handle re-pairing as a normal flow instead of an error.
About removeBond(). Android has a hidden removeBond() that many apps call through reflection. It isn't public API, can break on any release and is blocked by non-SDK interface restrictions in newer versions. iOS has no equivalent at all: there, iOS reports CBError.peerRemovedPairingInformation when the device dropped its keys, and only the user can remove the bond in Settings.
Pairing loops
A prompt that keeps coming back usually has one of these causes:
- The device pairs but doesn't bond, so every connection pairs again.
- The app calls
createBond()on every connect, including when a bond already exists or is in progress. - The app retries the encrypted read in a loop while pairing is pending, starting a new pairing each time.
- The device rejects the stored keys (stale bond), pairing restarts and fails the same way.
Add a pairing bug you fixed on a real product and what the cause was.
Bonding questions are a big part of the conversation with firmware teams: decide early whether the device bonds, what a factory reset clears, and how a user re-pairs.
FAQ
Why does my BLE device ask to pair every time?
Most often the device pairs without bonding, so no keys are stored and every connection needs a new pairing. It's a firmware setting. Less often, the app calls createBond() on every connection.
How do I remove a BLE bond programmatically on Android?
There's no public API. The hidden removeBond() method is reached by reflection in many apps, but it's unsupported and may stop working. The reliable path is to guide users to forget the device in Bluetooth settings, and to design firmware so reset devices are recognized.
What is a stale bond?
A bond that only one side still remembers: typically the device was reset and lost its keys while the phone kept them. Connections then fail right after connecting, often with status 0x3D or 22, until the user removes the bond and pairs again.
Does iOS have createBond()?
No. On iOS, pairing starts when the app accesses a characteristic that requires encryption, or when the device requests security. The app can't start it directly or remove a bond.
