Skip to content

fix(ble): Stop transport connect after failed bonding#5973

Merged
jamesarich merged 1 commit into
meshtastic:mainfrom
jeremiah-k:bugfix/ble-stop-connect-after-failed-bond
Jun 27, 2026
Merged

fix(ble): Stop transport connect after failed bonding#5973
jamesarich merged 1 commit into
meshtastic:mainfrom
jeremiah-k:bugfix/ble-stop-connect-after-failed-bond

Conversation

@jeremiah-k

Copy link
Copy Markdown
Contributor

Overview

This pull request stops the BLE transport from continuing into GATT setup after transport-side bonding fails and Android still reports the device as not bonded.

This is the third change in the related BLE bonding cleanup. #5967 made Android bonding waits finite and re-checked final bond state before failing. #5969 changed the user-facing pairing path so failed pairing waits for an explicit retry instead of immediately arming the transport. This PR handles the remaining transport-side case.

If bond() fails but Android now reports the device as bonded, the transport still continues. That preserves the late or flaky terminal-bond case where Android recorded the bond even though the app-side bond call reported an error.

If bond() fails and Android still reports the device as not bonded, the transport now stops that connection attempt before calling connectAndAwait(). The existing reconnect policy remains responsible for any later retry/backoff.

Key Changes

  • Re-checked isBonded(address) after transport-side bond() failure.
  • Continued into GATT setup when Android reports the device is bonded after a flaky bond result.
  • Stopped the current connection attempt when bonding fails and the device is still not bonded.
  • Avoided continuing into GATT setup after a failed bond that cannot produce an encrypted connection.
  • Preserved coroutine cancellation behavior.
  • Preserved the original bond failure as the cause on RadioNotConnectedException.
  • Added focused transport tests for the new bond-failure gate.

Testing

  • Added coverage proving successful bonding still proceeds to connectAndAwait().
  • Added coverage proving a flaky bond result still attempts bonding and proceeds when Android reports the device bonded.
  • Added coverage proving a failed bond does not call connectAndAwait() when Android still reports not bonded.
  • Added coverage proving CancellationException from bonding cancels rather than becoming a retryable generic failure.

Migration Notes

  • No user data migration is required.
  • Existing BLE addresses and OS bonds are unchanged.
  • Bluetooth permission behavior is unchanged.
  • This only changes the transport-side handling of failed bonding before GATT setup.

When BleRadioTransport bonds before connecting and bond() throws, re-check the final bonded state before continuing into GATT setup.

Successful bonding continues unchanged. A thrown bond call with a final bonded state continues so late or flaky terminal bond signals do not strand the connection. A thrown bond call with the device still unbonded now fails fast with RadioNotConnectedException so BleReconnectPolicy owns retry/backoff instead of entering GATT setup and surfacing cryptic status 5 or 133 failures later.

CancellationException is rethrown before the generic failure path, and the bond preflight is kept in a helper so attemptConnection does not need a broader ThrowsCount suppression.

Tests cover the successful bond path, late bonded-after-error path, failed-unbonded path, and cancellation propagation. The cancellation test advances far enough to detect an unintended reconnect retry.
@github-actions github-actions Bot added the bugfix PR tag label Jun 26, 2026
@jeremiah-k jeremiah-k marked this pull request as ready for review June 26, 2026 20:19
@jamesarich jamesarich added this pull request to the merge queue Jun 27, 2026
Merged via the queue into meshtastic:main with commit 86580db Jun 27, 2026
22 checks passed
@jeremiah-k jeremiah-k deleted the bugfix/ble-stop-connect-after-failed-bond branch June 27, 2026 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix PR tag

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants