From 2a9d11f14bde9a5597994db77e99d927916623c1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 10 Sep 2026 08:50:34 -0500 Subject: [PATCH] [esp32_ble] Honour an enable made during the disable callbacks The before-disabled callbacks can run user automations (on_scan_end fires when the tracker settles the scanner), so an enable issued there was flipped to ACTIVE and then overwritten by the DISABLED write after the teardown. Turn it into a bring-up instead. ble_client also skips advertisements until its app is registered, so the tracker does not stop the scan for a connect that would be rejected. --- esphome/components/esp32_ble/ble.cpp | 5 ++++- esphome/components/esp32_ble_client/ble_client_base.cpp | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index b08aa55ba2..81fa328c16 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -586,7 +586,10 @@ void ESP32BLE::loop_handle_state_transition_not_active_() { return; } this->drain_ble_events_(); - this->state_ = BLE_COMPONENT_STATE_DISABLED; + // A status callback may have asked for BLE back; the stack is down now, so + // that request becomes a bring-up. + this->state_ = + this->state_ == BLE_COMPONENT_STATE_ACTIVE ? BLE_COMPONENT_STATE_ENABLE : BLE_COMPONENT_STATE_DISABLED; } else if (this->state_ == BLE_COMPONENT_STATE_ENABLE) { ESP_LOGD(TAG, "Enabling"); this->state_ = BLE_COMPONENT_STATE_OFF; diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index d209dd22ce..a2b19c16b8 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -108,6 +108,10 @@ bool BLEClientBase::parse_device(const espbt::ESPBTDevice &device) { return false; if (this->state() != espbt::ClientState::IDLE) return false; + // Not registered on this stack yet; promoting now would stop the scan for a + // connect that connect() rejects anyway. + if (this->gattc_if_ == ESP_GATT_IF_NONE) + return false; this->log_event_("Found device"); if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG)