diff --git a/esphome/components/esp32_ble/ble.cpp b/esphome/components/esp32_ble/ble.cpp index edf6f3254b..3b561883f8 100644 --- a/esphome/components/esp32_ble/ble.cpp +++ b/esphome/components/esp32_ble/ble.cpp @@ -338,11 +338,12 @@ void ESP32BLE::loop() { gap_event == ESP_GAP_BLE_SCAN_START_COMPLETE_EVT || gap_event == ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT) { // All three scan complete events have the same structure with just status - // Cast is safe because all three ESP-IDF event structures are identical with just status field - // The scan_complete struct already contains our copy of the status (copied in BLEEvent constructor) + // The scan_complete struct matches ESP-IDF's layout exactly, so this reinterpret_cast is safe + // The struct already contains our copy of the status (copied in BLEEvent constructor) ESP_LOGV(TAG, "gap_event_handler - %d", gap_event); for (auto *gap_handler : this->gap_event_handlers_) { - gap_handler->gap_event_handler(gap_event, (esp_ble_gap_cb_param_t *) &ble_event->event_.gap.scan_complete); + gap_handler->gap_event_handler( + gap_event, reinterpret_cast(&ble_event->event_.gap.scan_complete)); } } break; diff --git a/esphome/components/esp32_ble/ble_event.h b/esphome/components/esp32_ble/ble_event.h index eb6453801f..e0178c41db 100644 --- a/esphome/components/esp32_ble/ble_event.h +++ b/esphome/components/esp32_ble/ble_event.h @@ -157,7 +157,9 @@ class BLEEvent { esp_gap_ble_cb_event_t gap_event; union { BLEScanResult scan_result; // 73 bytes - struct { + // This struct matches ESP-IDF's scan complete event structures + // All three (scan_param_cmpl, scan_start_cmpl, scan_stop_cmpl) have identical layout + struct ble_scan_complete_evt_param { esp_bt_status_t status; } scan_complete; // 1 byte }; diff --git a/esphome/components/esp32_ble/ble_scan_result.h b/esphome/components/esp32_ble/ble_scan_result.h index 42e1789437..49b0d5523d 100644 --- a/esphome/components/esp32_ble/ble_scan_result.h +++ b/esphome/components/esp32_ble/ble_scan_result.h @@ -8,7 +8,7 @@ namespace esphome { namespace esp32_ble { // Structure for BLE scan results - only fields we actually use -struct BLEScanResult { +struct __attribute__((packed)) BLEScanResult { esp_bd_addr_t bda; uint8_t ble_addr_type; int8_t rssi;