From 0b47a549565d309d7e4080f1c3726e5c33efffd7 Mon Sep 17 00:00:00 2001 From: George Tasioulis Date: Mon, 18 May 2026 18:59:16 +0300 Subject: [PATCH] Keep BLE UI state at CONNECTED while re-advertising After #18 (MAX_CONNECTIONS=2), onConnect sets need_advertise=true to fill the second slot. start_advertising() then unconditionally overwrote state to ADVERTISING, so with a single connected client the UI flipped CONNECTED -> ADVERTISING on the next tick and stayed there until a second central attached. Only flip the state to ADVERTISING when no clients are connected. Co-Authored-By: Claude Opus 4.7 (1M context) --- firmware/src/ble.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/firmware/src/ble.cpp b/firmware/src/ble.cpp index 1e03aee..1a2f942 100644 --- a/firmware/src/ble.cpp +++ b/firmware/src/ble.cpp @@ -87,8 +87,16 @@ static void start_advertising() { adv->setScanResponseData(scanResp); adv->enableScanResponse(true); bool ok = adv->start(); - state = BLE_STATE_ADVERTISING; - Serial.printf("BLE: advertising start=%s\n", ok ? "OK" : "FAILED"); + // Only reflect ADVERTISING in the UI state when no client is connected. + // With MAX_CONNECTIONS=2, onConnect re-advertises to fill the second slot; + // without this guard the UI would flip CONNECTED → ADVERTISING on every + // first connect and never come back until a second client arrived. + if (!server || server->getConnectedCount() == 0) { + state = BLE_STATE_ADVERTISING; + } + Serial.printf("BLE: advertising start=%s (connected=%u)\n", + ok ? "OK" : "FAILED", + server ? (unsigned)server->getConnectedCount() : 0); } class ServerCallbacks : public NimBLEServerCallbacks {