From 4f086b6b259f61ff4e0657dbf03ed06e20f80d1c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Aug 2026 21:23:54 -0500 Subject: [PATCH] Drive the push and poll halves off one define --- .../bluetooth_proxy/bluetooth_proxy.cpp | 17 ++++++++++++----- .../bluetooth_proxy/bluetooth_proxy.h | 4 ++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index b54003b70f..cb9e25762f 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -51,7 +51,7 @@ bool BluetoothProxy::send_bluetooth_scanner_state_(ble_device_base::ScannerState return this->api_connection_->send_message(resp); } -#ifndef USE_ESP32 +#ifndef USE_BLE_SCANNER_STATE_CALLBACK void BluetoothProxy::send_polled_scanner_state_() { // One read feeds both the frame and the change detector; the detector only // advances if the frame was accepted, so a dropped send (WOULD_BLOCK on a @@ -62,7 +62,7 @@ void BluetoothProxy::send_polled_scanner_state_() { this->last_scan_running_ = running; } } -#endif // !USE_ESP32 +#endif // !USE_BLE_SCANNER_STATE_CALLBACK void BluetoothProxy::setup() { // BLUETOOTH_PROXY_MAX_CONNECTIONS is 0 on an advertisement-only proxy. @@ -516,11 +516,13 @@ void BluetoothProxy::loop() { return; } +#ifndef USE_BLE_SCANNER_STATE_CALLBACK // This hub doesn't push scanner-state transitions; poll and report on - // change. A hub gaining push must also refresh last_scan_running_ here. + // change. A hub gaining push emits the define and drops this poll. if (this->hub_->scan_running() != this->last_scan_running_) { this->send_polled_scanner_state_(); } +#endif this->flush_pending_advertisements_(); } @@ -603,12 +605,15 @@ void BluetoothProxy::bluetooth_scanner_set_mode(bool active) { ESP_LOGW(TAG, "Scanner mode %s not supported by this tracker", active ? "active" : "passive"); } } +#ifndef USE_BLE_SCANNER_STATE_CALLBACK if (this->api_connection_ != nullptr) { // Reports the mode change; the sender also refreshes last_scan_running_, so // a failed restart (scan_running_ dropped by the tracker) is not reported - // again by loop() on the next tick. + // again by loop() on the next tick. A push hub reports the restart's + // transitions (mode rides along) instead. this->send_polled_scanner_state_(); } +#endif } #endif // USE_ESP32 @@ -627,7 +632,9 @@ void BluetoothProxy::subscribe_api_connection(api::APIConnection *api_connection this->api_connection_->get_peername_to(old_peername)); } this->api_connection_ = api_connection; -#ifdef USE_ESP32 +#ifdef USE_BLE_SCANNER_STATE_CALLBACK + // esp32 only: get_scanner_state() is tracker-native, beyond the neutral + // contract; the push define implies the concrete tracker alias. this->send_bluetooth_scanner_state_(this->hub_->get_scanner_state()); #else this->send_polled_scanner_state_(); diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.h b/esphome/components/bluetooth_proxy/bluetooth_proxy.h index 1ba1ef32f0..d7150617d3 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.h +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.h @@ -179,7 +179,7 @@ class BluetoothProxy final : public Component { protected: bool send_bluetooth_scanner_state_(ble_device_base::ScannerState state); -#ifndef USE_ESP32 +#ifndef USE_BLE_SCANNER_STATE_CALLBACK void send_polled_scanner_state_(); #endif void on_raw_advertisement_(const ble_device_base::RawAdvertisement &raw); @@ -263,7 +263,7 @@ class BluetoothProxy final : public Component { bool active_; uint8_t connection_count_{0}; bool configured_scan_active_{false}; // Configured scan mode from YAML -#ifndef USE_ESP32 +#ifndef USE_BLE_SCANNER_STATE_CALLBACK bool last_scan_running_{false}; // Last scanner state reported to the subscriber #endif };