From b2ec2615bbc3eabbb67577701a845b729bf42505 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 18 Jul 2025 14:33:10 -1000 Subject: [PATCH] other way --- .../bluetooth_proxy/bluetooth_connection.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp index 6495cb4922..ea41396c5c 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp @@ -65,6 +65,12 @@ void BluetoothConnection::send_service_for_discovery_() { return; } + // Early return if no API connection + auto *api_conn = this->proxy_->get_api_connection(); + if (api_conn == nullptr) { + return; + } + // Send next service esp_gattc_service_elem_t service_result; uint16_t service_count = 1; @@ -174,10 +180,8 @@ void BluetoothConnection::send_service_for_discovery_() { } resp.services.push_back(std::move(service_resp)); - auto *api_conn = this->proxy_->get_api_connection(); - if (api_conn != nullptr) { - api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE); - } + // Send the message (we already checked api_conn is not null at the beginning) + api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE); } bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,