From bbef0e173e5042fe288d0908cf1889d718a27f77 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 11 Sep 2025 15:54:34 -0500 Subject: [PATCH] [esp32_ble_tracker] Simplify BLE client state machine by removing READY_TO_CONNECT (#10672) --- .../esp32_ble_client/ble_client_base.cpp | 26 ++++++++++--------- .../esp32_ble_tracker/esp32_ble_tracker.cpp | 4 +-- .../esp32_ble_tracker/esp32_ble_tracker.h | 3 --- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index af5162afb0..18321ef91c 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -43,13 +43,6 @@ void BLEClientBase::setup() { void BLEClientBase::set_state(espbt::ClientState st) { ESP_LOGV(TAG, "[%d] [%s] Set state %d", this->connection_index_, this->address_str_.c_str(), (int) st); ESPBTClient::set_state(st); - - if (st == espbt::ClientState::READY_TO_CONNECT) { - // Enable loop for state processing - this->enable_loop(); - // Connect immediately instead of waiting for next loop - this->connect(); - } } void BLEClientBase::loop() { @@ -65,8 +58,8 @@ void BLEClientBase::loop() { } this->set_state(espbt::ClientState::IDLE); } - // If its idle, we can disable the loop as set_state - // will enable it again when we need to connect. + // If idle, we can disable the loop as connect() + // will enable it again when a connection is needed. else if (this->state_ == espbt::ClientState::IDLE) { this->disable_loop(); } @@ -108,9 +101,20 @@ bool BLEClientBase::parse_device(const espbt::ESPBTDevice &device) { #endif void BLEClientBase::connect() { + // Prevent duplicate connection attempts + if (this->state_ == espbt::ClientState::CONNECTING || this->state_ == espbt::ClientState::CONNECTED || + this->state_ == espbt::ClientState::ESTABLISHED) { + ESP_LOGW(TAG, "[%d] [%s] Connection already in progress, state=%s", this->connection_index_, + this->address_str_.c_str(), espbt::client_state_to_string(this->state_)); + return; + } ESP_LOGI(TAG, "[%d] [%s] 0x%02x Connecting", this->connection_index_, this->address_str_.c_str(), this->remote_addr_type_); this->paired_ = false; + // Enable loop for state processing + this->enable_loop(); + // Immediately transition to CONNECTING to prevent duplicate connection attempts + this->set_state(espbt::ClientState::CONNECTING); // Determine connection parameters based on connection type if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) { @@ -168,7 +172,7 @@ void BLEClientBase::unconditional_disconnect() { this->log_gattc_warning_("esp_ble_gattc_close", err); } - if (this->state_ == espbt::ClientState::READY_TO_CONNECT || this->state_ == espbt::ClientState::DISCOVERED) { + if (this->state_ == espbt::ClientState::DISCOVERED) { this->set_address(0); this->set_state(espbt::ClientState::IDLE); } else { @@ -212,8 +216,6 @@ void BLEClientBase::handle_connection_result_(esp_err_t ret) { if (ret) { this->log_gattc_warning_("esp_ble_gattc_open", ret); this->set_state(espbt::ClientState::IDLE); - } else { - this->set_state(espbt::ClientState::CONNECTING); } } diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index 0edde169eb..bab1dd7c98 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -51,8 +51,6 @@ const char *client_state_to_string(ClientState state) { return "IDLE"; case ClientState::DISCOVERED: return "DISCOVERED"; - case ClientState::READY_TO_CONNECT: - return "READY_TO_CONNECT"; case ClientState::CONNECTING: return "CONNECTING"; case ClientState::CONNECTED: @@ -795,7 +793,7 @@ void ESP32BLETracker::try_promote_discovered_clients_() { #ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE this->update_coex_preference_(true); #endif - client->set_state(ClientState::READY_TO_CONNECT); + client->connect(); break; } } diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h index dd67156108..e53c2ac097 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h @@ -159,8 +159,6 @@ enum class ClientState : uint8_t { IDLE, // Device advertisement found. DISCOVERED, - // Device is discovered and the scanner is stopped - READY_TO_CONNECT, // Connection in progress. CONNECTING, // Initial connection established. @@ -313,7 +311,6 @@ class ESP32BLETracker : public Component, counts.discovered++; break; case ClientState::CONNECTING: - case ClientState::READY_TO_CONNECT: counts.connecting++; break; default: