From 073c64f3b6325711bd00a9d5748cdd14f5c4d339 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 3 Sep 2026 10:53:36 +1200 Subject: [PATCH] Keep advertising start/stop decisions in one place advertising_stop() now just drops its request and lets advertising_refresh() decide, so both sides are symmetric and only refresh knows when the radio should be advertising. --- esphome/components/esp32_ble/ble.cpp | 18 +++++++++--------- esphome/components/esp32_ble/ble.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index 82d588475d..fc95760cf8 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -107,19 +107,19 @@ void ESP32BLE::advertising_start() { void ESP32BLE::advertising_stop() { if (this->advertising_ref_count_ == 0) return; - // Keep advertising while another component still needs it - if (--this->advertising_ref_count_ > 0) - return; - if (this->advertising_ == nullptr || !this->is_active()) - return; - this->advertising_->stop(); + this->advertising_ref_count_--; + this->advertising_refresh(); } void ESP32BLE::advertising_refresh() { - if (this->advertising_ref_count_ == 0 || !this->is_active()) + if (this->advertising_ == nullptr || !this->is_active()) return; - this->advertising_init_(); - this->advertising_->start(); + // Advertise while any component still needs it, otherwise stop + if (this->advertising_ref_count_ == 0) { + this->advertising_->stop(); + } else { + this->advertising_->start(); + } } void ESP32BLE::advertising_set_service_data(const std::vector &data) { diff --git a/esphome/components/esp32_ble/ble.h b/esphome/components/esp32_ble/ble.h index b958cc5c2e..7d2d0438a4 100644 --- a/esphome/components/esp32_ble/ble.h +++ b/esphome/components/esp32_ble/ble.h @@ -123,7 +123,7 @@ class ESP32BLE final : public Component { void advertising_start(); /// Release a request made with advertising_start(); advertising stops at the last release. void advertising_stop(); - /// Re-apply the advertising payload, restarting advertising only if it is still requested. + /// Apply the current payload and request count: advertise while requested, otherwise stop. void advertising_refresh(); void advertising_set_service_data(const std::vector &data); void advertising_set_manufacturer_data(const std::vector &data);