From de57f1ad6297ed73b21fd103cd3ad36490b4c807 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Wed, 8 Nov 2023 16:30:19 +0100 Subject: [PATCH] Fix bluetooth_proxy --- .../esp32_ble_client/ble_client_base.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index 40eff49266..cc6d3d7d4d 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -20,16 +20,21 @@ static const esp_bt_uuid_t NOTIFY_DESC_UUID = { void BLEClientBase::setup() { static uint8_t connection_index = 0; this->connection_index_ = connection_index++; - - auto ret = esp_ble_gattc_app_register(this->app_id); - if (ret) { - ESP_LOGE(TAG, "gattc app register failed. app_id=%d code=%d", this->app_id, ret); - this->mark_failed(); - } - this->set_state(espbt::ClientState::IDLE); } void BLEClientBase::loop() { + if (!esp32_ble::global_ble->is_active()) { + this->set_state(espbt::ClientState::INIT); + return; + } + if (this->state_ == espbt::ClientState::INIT) { + auto ret = esp_ble_gattc_app_register(this->app_id); + if (ret) { + ESP_LOGE(TAG, "gattc app register failed. app_id=%d code=%d", this->app_id, ret); + this->mark_failed(); + } + this->set_state(espbt::ClientState::IDLE); + } // READY_TO_CONNECT means we have discovered the device // and the scanner has been stopped by the tracker. if (this->state_ == espbt::ClientState::READY_TO_CONNECT) {