From 2b4e94901f84b9f7f83e547aa47bd456ce10948d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 10 Sep 2026 09:51:47 -0500 Subject: [PATCH] [esp32_ble_client] Keep INIT when a connect is rejected before registration The ble_client.connect action can reach connect() while the client is still in INIT; dropping it to IDLE there skipped the only registration path until the next disable cycle. --- .../bluetooth_connection/bluetooth_connection_bluedroid.cpp | 2 +- esphome/components/esp32_ble_client/ble_client_base.cpp | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp index 65964f2ec2..986a67c7a8 100644 --- a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp +++ b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp @@ -57,7 +57,7 @@ void BluedroidGattClient::loop() { ESP_LOGE(TAG, "gattc app register failed: app_id=%d code=%d", this->app_id, ret); this->mark_failed(); } - // Do not wait for REG_EVT; a dropped event must not wedge the slot. + // Do not wait for REG_EVT; connect() rejects until it lands. this->set_idle_(); } else if (st == ClientState::DISCONNECTING || this->disconnect_pending()) { // The one teardown safety net: a lost CLOSE_EVT, or a scheduled diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index a2b19c16b8..88454f7bdb 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -139,7 +139,10 @@ void BLEClientBase::connect() { if (this->gattc_if_ == ESP_GATT_IF_NONE) { // Bluedroid drops an open on an unknown interface without any event. this->log_warning_("Connect rejected, GATT app not registered"); - this->set_state(espbt::ClientState::IDLE); + // INIT stays so loop() still registers; only a promoted client goes back. + if (this->state() == espbt::ClientState::DISCOVERED) { + this->set_state(espbt::ClientState::IDLE); + } return; } ESP_LOGI(TAG, "[%d] [%s] 0x%02x Connecting", this->connection_index_, this->address_str_, this->remote_addr_type_);