From dc9718b1679fa2cebfb5403bb48fe283fb2f2a09 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 29 Mar 2026 21:32:38 -1000 Subject: [PATCH] [esp32_ble_tracker] Restart BLE scan after OTA failure When OTA starts, stop_scan() is called which clears the scan_continuous_ flag. If OTA fails or aborts, the scan was never restarted because scan_continuous_ was already false. This left the device without BLE scanning until reboot. Save the scan_continuous_ state before stopping and restore it on OTA_ERROR or OTA_ABORT to resume scanning. --- esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp | 7 +++++++ esphome/components/esp32_ble_tracker/esp32_ble_tracker.h | 1 + 2 files changed, 8 insertions(+) diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index 6dce70f839..60490584cf 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -88,12 +88,19 @@ void ESP32BLETracker::setup() { #ifdef USE_OTA_STATE_LISTENER void ESP32BLETracker::on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) { if (state == ota::OTA_STARTED) { + this->scan_continuous_before_ota_ = this->scan_continuous_; this->stop_scan(); #ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT for (auto *client : this->clients_) { client->disconnect(); } #endif + } else if (state == ota::OTA_ERROR || state == ota::OTA_ABORT) { + if (this->scan_continuous_before_ota_) { + this->scan_continuous_before_ota_ = false; + this->scan_continuous_ = true; + this->start_scan(); + } } } #endif diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h index f50ed107b6..8a57c7194a 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h @@ -436,6 +436,7 @@ class ESP32BLETracker : public Component, ScannerState scanner_state_{ScannerState::IDLE}; bool scan_continuous_; bool scan_active_; + bool scan_continuous_before_ota_{false}; bool ble_was_disabled_{true}; bool raw_advertisements_{false}; bool parse_advertisements_{false};