From decbeb6a80553ff9ae439cbcda3f6cc54e853ea0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 10 Sep 2026 09:34:08 -0500 Subject: [PATCH] [esp32_ble_tracker] Quiet the teardown of a scan that is still starting stop_scan_() logs an error for STARTING, which the settle right after it makes moot; only ask the controller to stop a scan that is running. Fold the two adjacent client-count guards and fix the services_released_ comment. --- esphome/components/esp32_ble_client/ble_client_base.h | 2 +- .../components/esp32_ble_tracker/esp32_ble_tracker.cpp | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/esphome/components/esp32_ble_client/ble_client_base.h b/esphome/components/esp32_ble_client/ble_client_base.h index c189fd4d26..3e7b010fbb 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.h +++ b/esphome/components/esp32_ble_client/ble_client_base.h @@ -140,7 +140,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component { uint8_t pending_notify_regs_{0}; bool auto_connect_{false}; bool paired_{false}; - // Set only when release_services() cleans the stack's GATT cache, which no API may then walk + // Always set by release_services(); the stack's GATT database must not be walked after it bool services_released_{false}; // 8 bytes used, no padding diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index 26296eadf0..59b65352b1 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -219,17 +219,18 @@ void ESP32BLETracker::stop_scan() { } void ESP32BLETracker::ble_before_disabled_event_handler() { - this->stop_scan_(); + // Tell the controller to stop; a scan still starting has nothing to stop yet. + if (this->scanner_state_ == ScannerState::RUNNING || this->scanner_state_ == ScannerState::FAILED) { + this->stop_scan_(); + } #ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT for (auto *client : this->clients_) { client->ble_before_disabled_event_handler(); } + this->skip_next_scan_end_ = false; #endif // The stop above never completes (stack torn down, events dropped); settle // here so start_scan_() sees IDLE once the stack is back. -#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT - this->skip_next_scan_end_ = false; -#endif if (this->scanner_state_ != ScannerState::IDLE) { this->cleanup_scan_state_(true); }