diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index fb75e8837f..15b9322dbe 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -8,6 +8,7 @@ #ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID #include +#include #else #include "esphome/components/watchdog/watchdog.h" #include @@ -674,6 +675,23 @@ 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. + 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() { diff --git a/esphome/components/esp32_ble/ble.h b/esphome/components/esp32_ble/ble.h index c85ddfc983..326bdc291f 100644 --- a/esphome/components/esp32_ble/ble.h +++ b/esphome/components/esp32_ble/ble.h @@ -108,6 +108,9 @@ 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. + void get_mac_msb_first(uint8_t out[6]); float get_setup_priority() const override; void set_name(const char *name) { this->name_ = name; } diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index 9a3c18269a..18b6cf022d 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -9,9 +9,7 @@ #ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID #include -#include #endif -#include #include #include #include @@ -20,7 +18,6 @@ #include #include #include -#include #ifdef USE_OTA #include "esphome/components/ota/ota_backend.h" @@ -288,24 +285,6 @@ void ESP32BLETracker::register_listener(ble_device_base::ESPBTDeviceListener *li #endif } -void ESP32BLETracker::get_adapter_mac(uint8_t out[6]) { -#ifdef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID - // Hosted controller (esp32-p4): the adapter MAC lives in the remote chip's - // efuse, so only the running stack knows it; ESP_MAC_BT does not exist - // here. Null before init becomes all-zero, the proxy's unavailable state. - 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 -} - void ESP32BLETracker::register_listener(ESPBTDeviceListener *listener) { #ifdef ESPHOME_ESP32_BLE_TRACKER_LISTENER_COUNT listener->set_parent(this); diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h index ed8082a14d..9031d86c97 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.h @@ -196,7 +196,7 @@ class ESP32BLETracker final : public Component, return {/* active_scan = */ true, /* merges_scan_response = */ true, /* gatt = */ true, /* scan_mode_switch = */ false}; } - void get_adapter_mac(uint8_t out[6]) override; + void get_adapter_mac(uint8_t out[6]) override { this->parent_->get_mac_msb_first(out); } bool scan_running() override { return this->scanner_state_ == ScannerState::RUNNING; } bool scan_active() override { return this->scan_active_; }