From fb379bbb88d99758ff89161865a881c68bb6bfbb Mon Sep 17 00:00:00 2001 From: GilDev Date: Thu, 31 Jul 2025 05:34:49 +0200 Subject: [PATCH 1/3] [wifi] Allow fast_connect with multiple networks (#9947) --- esphome/components/wifi/__init__.py | 2 - esphome/components/wifi/wifi_component.cpp | 57 ++++++++++++++++------ esphome/components/wifi/wifi_component.h | 6 ++- 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/esphome/components/wifi/__init__.py b/esphome/components/wifi/__init__.py index 26195694e7..b74237ad2e 100644 --- a/esphome/components/wifi/__init__.py +++ b/esphome/components/wifi/__init__.py @@ -265,8 +265,6 @@ def _validate(config): networks = config.get(CONF_NETWORKS, []) if not networks: raise cv.Invalid("At least one network required for fast_connect!") - if len(networks) != 1: - raise cv.Invalid("Fast connect can only be used with one network!") if CONF_USE_ADDRESS not in config: use_address = CORE.name + config[CONF_DOMAIN] diff --git a/esphome/components/wifi/wifi_component.cpp b/esphome/components/wifi/wifi_component.cpp index 2731dc1f3f..98f75894f4 100644 --- a/esphome/components/wifi/wifi_component.cpp +++ b/esphome/components/wifi/wifi_component.cpp @@ -91,8 +91,11 @@ void WiFiComponent::start() { } if (this->fast_connect_) { - this->selected_ap_ = this->sta_[0]; - this->load_fast_connect_settings_(); + this->trying_loaded_ap_ = this->load_fast_connect_settings_(); + if (!this->trying_loaded_ap_) { + this->ap_index_ = 0; + this->selected_ap_ = this->sta_[this->ap_index_]; + } this->start_connecting(this->selected_ap_, false); } else { this->start_scanning(); @@ -121,6 +124,14 @@ void WiFiComponent::start() { this->wifi_apply_hostname_(); } +void WiFiComponent::restart_adapter() { + ESP_LOGW(TAG, "Restarting adapter"); + this->wifi_mode_(false, {}); + delay(100); // NOLINT + this->num_retried_ = 0; + this->retry_hidden_ = false; +} + void WiFiComponent::loop() { this->wifi_loop_(); const uint32_t now = App.get_loop_component_start_time(); @@ -140,7 +151,7 @@ void WiFiComponent::loop() { this->status_set_warning("waiting to reconnect"); if (millis() - this->action_started_ > 5000) { if (this->fast_connect_ || this->retry_hidden_) { - this->start_connecting(this->sta_[0], false); + this->start_connecting(this->selected_ap_, false); } else { this->start_scanning(); } @@ -703,18 +714,30 @@ void WiFiComponent::retry_connect() { delay(10); if (!this->is_captive_portal_active_() && !this->is_esp32_improv_active_() && (this->num_retried_ > 3 || this->error_from_callback_)) { - if (this->num_retried_ > 5) { - // If retry failed for more than 5 times, let's restart STA - ESP_LOGW(TAG, "Restarting adapter"); - this->wifi_mode_(false, {}); - delay(100); // NOLINT + if (this->fast_connect_) { + if (this->trying_loaded_ap_) { + this->trying_loaded_ap_ = false; + this->ap_index_ = 0; // Retry from the first configured AP + } else if (this->ap_index_ >= this->sta_.size() - 1) { + ESP_LOGW(TAG, "No more APs to try"); + this->ap_index_ = 0; + this->restart_adapter(); + } else { + // Try next AP + this->ap_index_++; + } this->num_retried_ = 0; - this->retry_hidden_ = false; + this->selected_ap_ = this->sta_[this->ap_index_]; } else { - // Try hidden networks after 3 failed retries - ESP_LOGD(TAG, "Retrying with hidden networks"); - this->retry_hidden_ = true; - this->num_retried_++; + if (this->num_retried_ > 5) { + // If retry failed for more than 5 times, let's restart STA + this->restart_adapter(); + } else { + // Try hidden networks after 3 failed retries + ESP_LOGD(TAG, "Retrying with hidden networks"); + this->retry_hidden_ = true; + this->num_retried_++; + } } } else { this->num_retried_++; @@ -761,17 +784,22 @@ bool WiFiComponent::is_esp32_improv_active_() { #endif } -void WiFiComponent::load_fast_connect_settings_() { +bool WiFiComponent::load_fast_connect_settings_() { SavedWifiFastConnectSettings fast_connect_save{}; if (this->fast_connect_pref_.load(&fast_connect_save)) { bssid_t bssid{}; std::copy(fast_connect_save.bssid, fast_connect_save.bssid + 6, bssid.begin()); + this->ap_index_ = fast_connect_save.ap_index; + this->selected_ap_ = this->sta_[this->ap_index_]; this->selected_ap_.set_bssid(bssid); this->selected_ap_.set_channel(fast_connect_save.channel); ESP_LOGD(TAG, "Loaded fast_connect settings"); + return true; } + + return false; } void WiFiComponent::save_fast_connect_settings_() { @@ -783,6 +811,7 @@ void WiFiComponent::save_fast_connect_settings_() { memcpy(fast_connect_save.bssid, bssid.data(), 6); fast_connect_save.channel = channel; + fast_connect_save.ap_index = this->ap_index_; this->fast_connect_pref_.save(&fast_connect_save); diff --git a/esphome/components/wifi/wifi_component.h b/esphome/components/wifi/wifi_component.h index 64797a5801..bbe1bbb874 100644 --- a/esphome/components/wifi/wifi_component.h +++ b/esphome/components/wifi/wifi_component.h @@ -60,6 +60,7 @@ struct SavedWifiSettings { struct SavedWifiFastConnectSettings { uint8_t bssid[6]; uint8_t channel; + int8_t ap_index; } PACKED; // NOLINT enum WiFiComponentState : uint8_t { @@ -256,6 +257,7 @@ class WiFiComponent : public Component { void setup() override; void start(); void dump_config() override; + void restart_adapter(); /// WIFI setup_priority. float get_setup_priority() const override; float get_loop_priority() const override; @@ -353,7 +355,7 @@ class WiFiComponent : public Component { bool is_captive_portal_active_(); bool is_esp32_improv_active_(); - void load_fast_connect_settings_(); + bool load_fast_connect_settings_(); void save_fast_connect_settings_(); #ifdef USE_ESP8266 @@ -400,12 +402,14 @@ class WiFiComponent : public Component { WiFiComponentState state_{WIFI_COMPONENT_STATE_OFF}; WiFiPowerSaveMode power_save_{WIFI_POWER_SAVE_NONE}; uint8_t num_retried_{0}; + uint8_t ap_index_{0}; #if USE_NETWORK_IPV6 uint8_t num_ipv6_addresses_{0}; #endif /* USE_NETWORK_IPV6 */ // Group all boolean values together bool fast_connect_{false}; + bool trying_loaded_ap_{false}; bool retry_hidden_{false}; bool has_ap_{false}; bool handled_connected_state_{false}; From 88cfcc1967ac58e68165e33b1c5e5d9bd0a94aff Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 30 Jul 2025 17:36:33 -1000 Subject: [PATCH 2/3] [esp32_ble_client] Fix BLE connection stability for WiFi-based proxies (#9993) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- .../esp32_ble_client/ble_client_base.cpp | 65 ++++++++++++------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index d3416641d9..94f2a6073c 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -13,10 +13,15 @@ namespace esp32_ble_client { static const char *const TAG = "esp32_ble_client"; -// Connection interval defaults matching ESP-IDF's BTM_BLE_CONN_INT_*_DEF -static const uint16_t DEFAULT_MIN_CONN_INTERVAL = 0x0A; // 10 * 1.25ms = 12.5ms -static const uint16_t DEFAULT_MAX_CONN_INTERVAL = 0x0C; // 12 * 1.25ms = 15ms -static const uint16_t DEFAULT_CONN_TIMEOUT = 600; // 600 * 10ms = 6s +// Intermediate connection parameters for standard operation +// ESP-IDF defaults (12.5-15ms) are too slow for stable connections through WiFi-based BLE proxies, +// causing disconnections. These medium parameters balance responsiveness with bandwidth usage. +static const uint16_t MEDIUM_MIN_CONN_INTERVAL = 0x08; // 8 * 1.25ms = 10ms +static const uint16_t MEDIUM_MAX_CONN_INTERVAL = 0x0A; // 10 * 1.25ms = 12.5ms +// The timeout value was increased from 6s to 8s to address stability issues observed +// in certain BLE devices when operating through WiFi-based BLE proxies. The longer +// timeout reduces the likelihood of disconnections during periods of high latency. +static const uint16_t MEDIUM_CONN_TIMEOUT = 800; // 800 * 10ms = 8s // Fastest connection parameters for devices with short discovery timeouts static const uint16_t FAST_MIN_CONN_INTERVAL = 0x06; // 6 * 1.25ms = 7.5ms (BLE minimum) @@ -151,20 +156,32 @@ void BLEClientBase::connect() { } else { this->set_state(espbt::ClientState::CONNECTING); - // For connections without cache, set fast connection parameters after initiating connection - // This ensures service discovery completes within the 10-second timeout that - // some devices like HomeKit BLE sensors enforce + // Always set connection parameters to ensure stable operation + // Use FAST for V3_WITHOUT_CACHE (devices that need lowest latency) + // Use MEDIUM for all other connections (balanced performance) + uint16_t min_interval, max_interval, timeout; + const char *param_type; + if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) { - auto param_ret = - esp_ble_gap_set_prefer_conn_params(this->remote_bda_, FAST_MIN_CONN_INTERVAL, FAST_MAX_CONN_INTERVAL, - 0, // latency: 0 - FAST_CONN_TIMEOUT); - if (param_ret != ESP_OK) { - ESP_LOGW(TAG, "[%d] [%s] esp_ble_gap_set_prefer_conn_params failed: %d", this->connection_index_, - this->address_str_.c_str(), param_ret); - } else { - ESP_LOGD(TAG, "[%d] [%s] Set fast conn params", this->connection_index_, this->address_str_.c_str()); - } + min_interval = FAST_MIN_CONN_INTERVAL; + max_interval = FAST_MAX_CONN_INTERVAL; + timeout = FAST_CONN_TIMEOUT; + param_type = "fast"; + } else { + min_interval = MEDIUM_MIN_CONN_INTERVAL; + max_interval = MEDIUM_MAX_CONN_INTERVAL; + timeout = MEDIUM_CONN_TIMEOUT; + param_type = "medium"; + } + + auto param_ret = esp_ble_gap_set_prefer_conn_params(this->remote_bda_, min_interval, max_interval, + 0, // latency: 0 + timeout); + if (param_ret != ESP_OK) { + ESP_LOGW(TAG, "[%d] [%s] esp_ble_gap_set_prefer_conn_params failed: %d", this->connection_index_, + this->address_str_.c_str(), param_ret); + } else { + ESP_LOGD(TAG, "[%d] [%s] Set %s conn params", this->connection_index_, this->address_str_.c_str(), param_type); } } } @@ -394,17 +411,17 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ } ESP_LOGI(TAG, "[%d] [%s] Service discovery complete", this->connection_index_, this->address_str_.c_str()); - // For non-cached connections, restore default connection parameters after service discovery - // Now that we've discovered all services, we can use more balanced parameters - // that save power and reduce interference + // For non-cached connections, restore to medium connection parameters after service discovery + // This balances performance with bandwidth usage after the critical discovery phase if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) { esp_ble_conn_update_params_t conn_params = {{0}}; memcpy(conn_params.bda, this->remote_bda_, sizeof(esp_bd_addr_t)); - conn_params.min_int = DEFAULT_MIN_CONN_INTERVAL; - conn_params.max_int = DEFAULT_MAX_CONN_INTERVAL; + conn_params.min_int = MEDIUM_MIN_CONN_INTERVAL; + conn_params.max_int = MEDIUM_MAX_CONN_INTERVAL; conn_params.latency = 0; - conn_params.timeout = DEFAULT_CONN_TIMEOUT; - ESP_LOGD(TAG, "[%d] [%s] Restored default conn params", this->connection_index_, this->address_str_.c_str()); + conn_params.timeout = MEDIUM_CONN_TIMEOUT; + ESP_LOGD(TAG, "[%d] [%s] Restored medium conn params after service discovery", this->connection_index_, + this->address_str_.c_str()); esp_ble_gap_update_conn_params(&conn_params); } From 71557c9f58acf754fa80ce5836c51066ec46ce12 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 30 Jul 2025 18:11:11 -1000 Subject: [PATCH 3/3] [bluetooth_proxy] Batch BLE service discovery messages for 67% reduction in API traffic (#9992) --- esphome/components/api/api.proto | 2 +- esphome/components/api/api_pb2.cpp | 6 +- esphome/components/api/api_pb2.h | 4 +- .../bluetooth_proxy/bluetooth_connection.cpp | 188 ++++++++++-------- .../bluetooth_proxy/bluetooth_proxy.h | 1 + 5 files changed, 109 insertions(+), 92 deletions(-) diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 32bbc5ec0d..27edf4680f 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -1505,7 +1505,7 @@ message BluetoothGATTGetServicesResponse { option (ifdef) = "USE_BLUETOOTH_PROXY"; uint64 address = 1; - repeated BluetoothGATTService services = 2 [(fixed_array_size) = 1]; + repeated BluetoothGATTService services = 2; } message BluetoothGATTGetServicesDoneResponse { diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 2e8adeaf5c..ef02a5a774 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -1929,11 +1929,13 @@ void BluetoothGATTService::calculate_size(ProtoSize &size) const { } void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); - buffer.encode_message(2, this->services[0], true); + for (auto &it : this->services) { + buffer.encode_message(2, it, true); + } } void BluetoothGATTGetServicesResponse::calculate_size(ProtoSize &size) const { size.add_uint64(1, this->address); - size.add_message_object_force(1, this->services[0]); + size.add_repeated_message(1, this->services); } void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint64(1, this->address); diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 7f2299f77c..6c2ca60e00 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -1895,12 +1895,12 @@ class BluetoothGATTService : public ProtoMessage { class BluetoothGATTGetServicesResponse : public ProtoMessage { public: static constexpr uint8_t MESSAGE_TYPE = 71; - static constexpr uint8_t ESTIMATED_SIZE = 21; + static constexpr uint8_t ESTIMATED_SIZE = 38; #ifdef HAS_PROTO_MESSAGE_DUMP const char *message_name() const override { return "bluetooth_gatt_get_services_response"; } #endif uint64_t address{0}; - std::array services{}; + std::vector services{}; void encode(ProtoWriteBuffer buffer) const override; void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp index b3484032b2..1295c18985 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp @@ -57,7 +57,7 @@ void BluetoothConnection::reset_connection_(esp_err_t reason) { } void BluetoothConnection::send_service_for_discovery_() { - if (this->send_service_ == this->service_count_) { + if (this->send_service_ >= this->service_count_) { this->send_service_ = DONE_SENDING_SERVICES; this->proxy_->send_gatt_services_done(this->address_); if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE || @@ -70,119 +70,133 @@ void BluetoothConnection::send_service_for_discovery_() { // Early return if no API connection auto *api_conn = this->proxy_->get_api_connection(); if (api_conn == nullptr) { + this->send_service_ = DONE_SENDING_SERVICES; return; } - // Send next service - esp_gattc_service_elem_t service_result; - uint16_t service_count = 1; - esp_gatt_status_t service_status = esp_ble_gattc_get_service(this->gattc_if_, this->conn_id_, nullptr, - &service_result, &service_count, this->send_service_); - this->send_service_++; - - if (service_status != ESP_GATT_OK || service_count == 0) { - ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_service %s, status=%d, service_count=%d, offset=%d", - this->connection_index_, this->address_str().c_str(), service_status != ESP_GATT_OK ? "error" : "missing", - service_status, service_count, this->send_service_ - 1); - return; - } - + // Prepare response for up to 3 services api::BluetoothGATTGetServicesResponse resp; resp.address = this->address_; - auto &service_resp = resp.services[0]; - fill_128bit_uuid_array(service_resp.uuid, service_result.uuid); - service_resp.handle = service_result.start_handle; - // Get the number of characteristics directly with one call - uint16_t total_char_count = 0; - esp_gatt_status_t char_count_status = - esp_ble_gattc_get_attr_count(this->gattc_if_, this->conn_id_, ESP_GATT_DB_CHARACTERISTIC, - service_result.start_handle, service_result.end_handle, 0, &total_char_count); + // Process up to 3 services in this iteration + uint8_t services_to_process = + std::min(MAX_SERVICES_PER_BATCH, static_cast(this->service_count_ - this->send_service_)); + resp.services.reserve(services_to_process); - if (char_count_status != ESP_GATT_OK) { - ESP_LOGW(TAG, "[%d] [%s] Error getting characteristic count, status=%d", this->connection_index_, - this->address_str().c_str(), char_count_status); - return; - } + for (int service_idx = 0; service_idx < services_to_process; service_idx++) { + esp_gattc_service_elem_t service_result; + uint16_t service_count = 1; + esp_gatt_status_t service_status = esp_ble_gattc_get_service(this->gattc_if_, this->conn_id_, nullptr, + &service_result, &service_count, this->send_service_); - if (total_char_count == 0) { - // No characteristics, just send the service response - api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE); - return; - } - - // Reserve space and process characteristics - service_resp.characteristics.reserve(total_char_count); - uint16_t char_offset = 0; - esp_gattc_char_elem_t char_result; - while (true) { // characteristics - uint16_t char_count = 1; - esp_gatt_status_t char_status = - esp_ble_gattc_get_all_char(this->gattc_if_, this->conn_id_, service_result.start_handle, - service_result.end_handle, &char_result, &char_count, char_offset); - if (char_status == ESP_GATT_INVALID_OFFSET || char_status == ESP_GATT_NOT_FOUND) { - break; - } - if (char_status != ESP_GATT_OK) { - ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_, - this->address_str().c_str(), char_status); + if (service_status != ESP_GATT_OK || service_count == 0) { + ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_service %s, status=%d, service_count=%d, offset=%d", + this->connection_index_, this->address_str().c_str(), + service_status != ESP_GATT_OK ? "error" : "missing", service_status, service_count, this->send_service_); + this->send_service_ = DONE_SENDING_SERVICES; return; } - if (char_count == 0) { - break; - } - service_resp.characteristics.emplace_back(); - auto &characteristic_resp = service_resp.characteristics.back(); - fill_128bit_uuid_array(characteristic_resp.uuid, char_result.uuid); - characteristic_resp.handle = char_result.char_handle; - characteristic_resp.properties = char_result.properties; - char_offset++; + this->send_service_++; + resp.services.emplace_back(); + auto &service_resp = resp.services.back(); + fill_128bit_uuid_array(service_resp.uuid, service_result.uuid); + service_resp.handle = service_result.start_handle; - // Get the number of descriptors directly with one call - uint16_t total_desc_count = 0; - esp_gatt_status_t desc_count_status = esp_ble_gattc_get_attr_count( - this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, 0, 0, char_result.char_handle, &total_desc_count); + // Get the number of characteristics directly with one call + uint16_t total_char_count = 0; + esp_gatt_status_t char_count_status = + esp_ble_gattc_get_attr_count(this->gattc_if_, this->conn_id_, ESP_GATT_DB_CHARACTERISTIC, + service_result.start_handle, service_result.end_handle, 0, &total_char_count); - if (desc_count_status != ESP_GATT_OK) { - ESP_LOGW(TAG, "[%d] [%s] Error getting descriptor count for char handle %d, status=%d", this->connection_index_, - this->address_str().c_str(), char_result.char_handle, desc_count_status); + if (char_count_status != ESP_GATT_OK) { + ESP_LOGE(TAG, "[%d] [%s] Error getting characteristic count, status=%d", this->connection_index_, + this->address_str().c_str(), char_count_status); + this->send_service_ = DONE_SENDING_SERVICES; return; } - if (total_desc_count == 0) { - // No descriptors, continue to next characteristic + + if (total_char_count == 0) { + // No characteristics, continue to next service continue; } - // Reserve space and process descriptors - characteristic_resp.descriptors.reserve(total_desc_count); - uint16_t desc_offset = 0; - esp_gattc_descr_elem_t desc_result; - while (true) { // descriptors - uint16_t desc_count = 1; - esp_gatt_status_t desc_status = esp_ble_gattc_get_all_descr( - this->gattc_if_, this->conn_id_, char_result.char_handle, &desc_result, &desc_count, desc_offset); - if (desc_status == ESP_GATT_INVALID_OFFSET || desc_status == ESP_GATT_NOT_FOUND) { + // Reserve space and process characteristics + service_resp.characteristics.reserve(total_char_count); + uint16_t char_offset = 0; + esp_gattc_char_elem_t char_result; + while (true) { // characteristics + uint16_t char_count = 1; + esp_gatt_status_t char_status = + esp_ble_gattc_get_all_char(this->gattc_if_, this->conn_id_, service_result.start_handle, + service_result.end_handle, &char_result, &char_count, char_offset); + if (char_status == ESP_GATT_INVALID_OFFSET || char_status == ESP_GATT_NOT_FOUND) { break; } - if (desc_status != ESP_GATT_OK) { - ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d", this->connection_index_, - this->address_str().c_str(), desc_status); + if (char_status != ESP_GATT_OK) { + ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_, + this->address_str().c_str(), char_status); + this->send_service_ = DONE_SENDING_SERVICES; return; } - if (desc_count == 0) { - break; // No more descriptors + if (char_count == 0) { + break; } - characteristic_resp.descriptors.emplace_back(); - auto &descriptor_resp = characteristic_resp.descriptors.back(); - fill_128bit_uuid_array(descriptor_resp.uuid, desc_result.uuid); - descriptor_resp.handle = desc_result.handle; - desc_offset++; + service_resp.characteristics.emplace_back(); + auto &characteristic_resp = service_resp.characteristics.back(); + fill_128bit_uuid_array(characteristic_resp.uuid, char_result.uuid); + characteristic_resp.handle = char_result.char_handle; + characteristic_resp.properties = char_result.properties; + char_offset++; + + // Get the number of descriptors directly with one call + uint16_t total_desc_count = 0; + esp_gatt_status_t desc_count_status = esp_ble_gattc_get_attr_count( + this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, 0, 0, char_result.char_handle, &total_desc_count); + + if (desc_count_status != ESP_GATT_OK) { + ESP_LOGE(TAG, "[%d] [%s] Error getting descriptor count for char handle %d, status=%d", this->connection_index_, + this->address_str().c_str(), char_result.char_handle, desc_count_status); + this->send_service_ = DONE_SENDING_SERVICES; + return; + } + if (total_desc_count == 0) { + // No descriptors, continue to next characteristic + continue; + } + + // Reserve space and process descriptors + characteristic_resp.descriptors.reserve(total_desc_count); + uint16_t desc_offset = 0; + esp_gattc_descr_elem_t desc_result; + while (true) { // descriptors + uint16_t desc_count = 1; + esp_gatt_status_t desc_status = esp_ble_gattc_get_all_descr( + this->gattc_if_, this->conn_id_, char_result.char_handle, &desc_result, &desc_count, desc_offset); + if (desc_status == ESP_GATT_INVALID_OFFSET || desc_status == ESP_GATT_NOT_FOUND) { + break; + } + if (desc_status != ESP_GATT_OK) { + ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d", this->connection_index_, + this->address_str().c_str(), desc_status); + this->send_service_ = DONE_SENDING_SERVICES; + return; + } + if (desc_count == 0) { + break; // No more descriptors + } + + characteristic_resp.descriptors.emplace_back(); + auto &descriptor_resp = characteristic_resp.descriptors.back(); + fill_128bit_uuid_array(descriptor_resp.uuid, desc_result.uuid); + descriptor_resp.handle = desc_result.handle; + desc_offset++; + } } } - // Send the message (we already checked api_conn is not null at the beginning) + // Send the message with 1-3 services api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE); } diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.h b/esphome/components/bluetooth_proxy/bluetooth_proxy.h index d249515fdf..b33460339b 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.h +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.h @@ -22,6 +22,7 @@ namespace esphome::bluetooth_proxy { static const esp_err_t ESP_GATT_NOT_CONNECTED = -1; static const int DONE_SENDING_SERVICES = -2; +static const uint8_t MAX_SERVICES_PER_BATCH = 3; using namespace esp32_ble_client;