From fd7510d6e5abf3e43d740c435053dabfc8ddb455 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Aug 2026 01:54:36 -0500 Subject: [PATCH] Use one stack query for the adapter MAC --- esphome/components/esp32_ble/ble.cpp | 19 ++++++++----------- esphome/components/esp32_ble/ble.h | 3 +-- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index 15b9322dbe..456f02de9d 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -8,7 +8,6 @@ #ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID #include -#include #else #include "esphome/components/watchdog/watchdog.h" #include @@ -676,27 +675,25 @@ void ESP32BLE::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gat #endif void ESP32BLE::get_mac_msb_first(uint8_t out[6]) { -#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID - // The adapter MAC lives in the remote chip's efuse, so only the running - // stack knows it. + // The running stack owns the address (on hosted controllers it lives in + // the remote chip's efuse); null before init becomes all-zero. const uint8_t *mac = esp_bt_dev_get_address(); if (mac != nullptr) { memcpy(out, mac, 6); } else { memset(out, 0, 6); } -#else - // IDF owns the BT-offset derivation (base + 2 with four universal MACs, - // base + 1 with two); works before the BT stack is up. - esp_read_mac(out, ESP_MAC_BT); -#endif } float ESP32BLE::get_setup_priority() const { return setup_priority::BLUETOOTH; } void ESP32BLE::dump_config() { - const uint8_t *mac_address = esp_bt_dev_get_address(); - if (mac_address) { + uint8_t mac_address[6]; + this->get_mac_msb_first(mac_address); + bool mac_known = false; + for (uint8_t b : mac_address) + mac_known |= b != 0; + if (mac_known) { const char *io_capability_s; switch (this->io_cap_) { case ESP_IO_CAP_OUT: diff --git a/esphome/components/esp32_ble/ble.h b/esphome/components/esp32_ble/ble.h index 326bdc291f..43c9584c3c 100644 --- a/esphome/components/esp32_ble/ble.h +++ b/esphome/components/esp32_ble/ble.h @@ -108,8 +108,7 @@ class ESP32BLE final : public Component { void setup() override; void loop() override; void dump_config() override; - /// Adapter MAC in printable (MSB-first) order. Hosted controllers only - /// know it once the stack is up; all-zero means unavailable. + /// Adapter MAC in printable (MSB-first) order; all-zero until the stack is up. void get_mac_msb_first(uint8_t out[6]); float get_setup_priority() const override; void set_name(const char *name) { this->name_ = name; }