Skip to content

NimBLE params overhaul and try-fix for incompatible bond cleanup - #10741

Merged
thebentern merged 4 commits into
developfrom
nimble-pairing-fix
Jun 19, 2026
Merged

NimBLE params overhaul and try-fix for incompatible bond cleanup#10741
thebentern merged 4 commits into
developfrom
nimble-pairing-fix

Conversation

@thebentern

Copy link
Copy Markdown
Contributor

Fixes Android BLE pairing for my Heltec V3.

This pull request introduces several important improvements to BLE bond management, connection handling, and memory optimization for ESP32 devices using NimBLE. The most significant changes involve automatically purging incompatible BLE bonds after a NimBLE upgrade, deferring advertising restarts to prevent crashes during host resets, and right-sizing BLE RAM usage for single-connection scenarios. These changes enhance robustness during upgrades and improve memory efficiency.

BLE bond management and upgrade robustness:

  • Added a function (purgeIncompatibleBleBonds) to automatically detect and wipe incompatible BLE bond records left after a NimBLE upgrade, preventing persistent pairing failures without requiring a factory reset. This is called during setup on ESP32 devices. [1] [2]
  • Updated clearBonds() and clearNVS() to also delete BLE_STORE_OBJ_TYPE_OUR_SEC entries, ensuring all relevant bond data is removed. [1] [2]

Connection and advertising handling improvements:

  • Introduced pendingStartAdvertising to defer advertising restarts after disconnects, avoiding crashes caused by attempting to restart advertising while the NimBLE host is resetting. The main thread now safely handles advertising restarts. [1] [2] [3]
  • Improved security callback logic to avoid marking a connection as authenticated if encryption fails (e.g., due to a stale bond), increasing connection state accuracy.

BLE RAM and configuration optimization (ESP32):

  • Tuned BLE-related configuration in esp32-common.ini to reduce RAM usage for single-connection scenarios: lowered stack size, reduced buffer counts, and limited maximum connections, freeing heap space and preventing out-of-memory errors during BLE initialization.

Dependency and platform-specific adjustments:

  • Added necessary includes for ESP32 and NimBLE host/ble_hs APIs, ensuring compatibility and access to required functions.

@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

⚡ Try this PR in the Web Flasher

Flash this PR in the Web Flasher

firmware commit boards expires

Warning

This is an automated, unreviewed CI test build. Back up your device configuration
before flashing, and only flash devices you are able to recover.

Supported boards built by this PR (24)
Device Board Platform
Crowpanel Adv 3.5 TFT elecrow-adv-35-tft esp32-s3
Heltec HT62 heltec-ht62-esp32c3-sx1262 esp32-c3
Heltec Mesh Node 096 heltec-mesh-node-t096 nrf52840
Heltec Mesh Node T1 heltec-mesh-node-t1 nrf52840
Heltec Mesh Node T114 heltec-mesh-node-t114 nrf52840
Heltec V3 heltec-v3 esp32-s3
Heltec V4 heltec-v4 esp32-s3
Raspberry Pi Pico pico rp2040
Raspberry Pi Pico W picow rp2040
RAK WisMesh Tag rak_wismeshtag nrf52840
RAK WisBlock 11200 rak11200 esp32
RAK WisBlock 11310 rak11310 rp2040
RAK3312 rak3312 esp32-s3
RAK WisBlock 4631 rak4631 nrf52840
Seeed Wio Tracker L1 seeed_wio_tracker_L1 nrf52840
Seeed Xiao NRF52840 Kit seeed_xiao_nrf52840_kit nrf52840
Seeed Xiao ESP32-S3 seeed-xiao-s3 esp32-s3
Station G2 station-g2 esp32-s3
Station G3 station-g3 esp32-s3
LILYGO T-Deck t-deck-tft esp32-s3
LILYGO T-Echo t-echo nrf52840
LILYGO T-Echo Plus t-echo-plus nrf52840
LilyGo T3-C6 tlora-c6 esp32-c6
Seeed SenseCAP T1000-E tracker-t1000-e nrf52840

Build artifacts expire on 2026-07-19. Updated for c122cbe.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR targets ESP32 NimBLE stability and Android pairing reliability by cleaning up incompatible bond data after upgrades, avoiding unsafe advertising restarts during NimBLE host resets, and reducing ESP32 BLE RAM usage for the firmware’s single-phone-peripheral model.

Changes:

  • Add ESP32-only boot-time detection and one-shot purge of incompatible on-disk NimBLE bond records, with an automatic reboot after cleanup.
  • Defer advertising restarts out of the NimBLE disconnect callback into the main thread (gated on ble_hs_synced()), avoiding crashes during host resets; tighten auth callback handling to avoid marking connections “connected” on failed encryption.
  • Tune ESP32 NimBLE/controller sdkconfig defaults to reduce RAM usage (single connection, fewer CCCDs/buffers, smaller host task stack).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
variants/esp32/esp32-common.ini Adjusts ESP32 NimBLE/controller sdkconfig defaults to reduce RAM usage for a single BLE connection scenario.
src/nimble/NimbleBluetooth.cpp Adds incompatible bond purge on ESP32, defers advertising restart to main thread, and improves security/auth state handling; extends bond-clearing to include OUR_SEC records.

Comment thread src/nimble/NimbleBluetooth.cpp Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Firmware Size Report

22 targets | vs develop: 13 increased, net +11,536 (+11.3 KB)

Target Size vs develop
rak3312 2,251,472 📈 +1,232 (+1.2 KB)
seeed-xiao-s3 2,255,408 📈 +1,232 (+1.2 KB)
t-deck-tft 3,790,608 📈 +1,216 (+1.2 KB)
heltec-vision-master-e213-inkhud 2,204,528 📈 +848
heltec-ht62-esp32c3-sx1262 2,115,584 📈 +816
Show 17 more target(s)
Target Size vs develop
rak11200 1,840,784 📈 +816
tlora-c6 2,348,992 📈 +800
heltec-v4 2,264,320 📈 +784
station-g3 2,245,712 📈 +784
elecrow-adv-35-tft 3,396,480 📈 +768
station-g2 2,254,976 📈 +768
t-eth-elite 2,470,448 📈 +752
heltec-v3 2,243,744 📈 +720
pico 765,568 0
pico2 753,568 0
pico2w 1,202,928 0
picow 1,226,316 0
rak11310 788,192 0
rak3172 181,028 0
seeed_xiao_rp2040 763,768 0
seeed_xiao_rp2350 751,728 0
wio-e5 233,220 0

Updated for 95a003a

Comment thread src/nimble/NimbleBluetooth.cpp
@cpatulea cpatulea mentioned this pull request Jun 19, 2026
8 tasks
Comment thread src/nimble/NimbleBluetooth.cpp Outdated

@cpatulea cpatulea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks reasonable.

Did not compile or test on device.

… init

- Delete unused clearNVS() (no callers; should have been removed in #10264).

- Move purgeIncompatibleBleBonds() after the "Init the NimBLE" log so bond-cleanup output doesn't appear to precede module init; it still runs before BLEDevice::init() reads the store.
@thebentern
thebentern merged commit f2f23f8 into develop Jun 19, 2026
89 checks passed
raghumad pushed a commit to raghumad/mezulla-firmware that referenced this pull request Jun 25, 2026
…htastic#10741)

* NimBLE params overhaul and try-fix for incompatible bond cleanup

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Address PR review: remove dead clearNVS(), defer bond-purge log below init

- Delete unused clearNVS() (no callers; should have been removed in meshtastic#10264).

- Move purgeIncompatibleBleBonds() after the "Init the NimBLE" log so bond-cleanup output doesn't appear to precede module init; it still runs before BLEDevice::init() reads the store.

* Update MAX_SATELLITE_NODES and WARM_NODE_COUNT definitions for ESP32-S3 PSRAM support

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
thebentern added a commit that referenced this pull request Jul 27, 2026
sendLog() notified once per firmware log line with no backpressure. With
debug_log_api_enabled and a subscribed client, a logging burst drains the
msys_1 mbuf pool that every GATT notification and ATT response allocates
from, so ble_gatts_notify_custom starts returning BLE_HS_ENOMEM (rc=6) --
for the fromNum doorbell too, not just the log line that exhausted it.
Each failure then prints an error over serial at ~12ms apiece, which is
itself enough to throttle the main loop during a burst.

Register a callback on the log characteristic so the host's own
ERROR_GATT verdict pauses log notifies for 250ms and lets the pool
refill. Keying off the failure rather than a free-block count keeps this
correct however the pools are sized.

Also restore CONFIG_BT_NIMBLE_MSYS_1_BLOCK_COUNT to the IDF default of
12. Pool selection is by block size and a notify request always resolves
to the smallest pool, so msys_1 is the only pool GATT draws from; 8 was
thin once anything chains or fragments. msys_2 and the ACL/EVT transport
pools are left trimmed -- neither is on the notify path, and restoring
the full defaults would re-spend the contiguous allocation that #10741
was fixing on heap-tight boards.

sendLog() also lacked the null check onNowHasData() already has, so a log
line arriving during BLE teardown dereferenced a freed characteristic.

Fixes #11245
vidplace7 pushed a commit that referenced this pull request Jul 27, 2026
sendLog() notified once per firmware log line with no backpressure. With
debug_log_api_enabled and a subscribed client, a logging burst drains the
msys_1 mbuf pool that every GATT notification and ATT response allocates
from, so ble_gatts_notify_custom starts returning BLE_HS_ENOMEM (rc=6) --
for the fromNum doorbell too, not just the log line that exhausted it.
Each failure then prints an error over serial at ~12ms apiece, which is
itself enough to throttle the main loop during a burst.

Register a callback on the log characteristic so the host's own
ERROR_GATT verdict pauses log notifies for 250ms and lets the pool
refill. Keying off the failure rather than a free-block count keeps this
correct however the pools are sized.

Also restore CONFIG_BT_NIMBLE_MSYS_1_BLOCK_COUNT to the IDF default of
12. Pool selection is by block size and a notify request always resolves
to the smallest pool, so msys_1 is the only pool GATT draws from; 8 was
thin once anything chains or fragments. msys_2 and the ACL/EVT transport
pools are left trimmed -- neither is on the notify path, and restoring
the full defaults would re-spend the contiguous allocation that #10741
was fixing on heap-tight boards.

sendLog() also lacked the null check onNowHasData() already has, so a log
line arriving during BLE teardown dereferenced a freed characteristic.

Fixes #11245
@caveman99
caveman99 deleted the nimble-pairing-fix branch August 26, 2026 19:40
Itzdavid01 pushed a commit to Itzdavid01/firmware that referenced this pull request Sep 5, 2026
…htastic#10741)

* NimBLE params overhaul and try-fix for incompatible bond cleanup

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Address PR review: remove dead clearNVS(), defer bond-purge log below init

- Delete unused clearNVS() (no callers; should have been removed in meshtastic#10264).

- Move purgeIncompatibleBleBonds() after the "Init the NimBLE" log so bond-cleanup output doesn't appear to precede module init; it still runs before BLEDevice::init() reads the store.

* Update MAX_SATELLITE_NODES and WARM_NODE_COUNT definitions for ESP32-S3 PSRAM support

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Pull request that fixes bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants