diff --git a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp index f726793759..69355372e7 100644 --- a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp +++ b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.cpp @@ -54,7 +54,10 @@ void BluedroidGattClient::loop() { // frees its slot, then re-register the app on the next enable. auto down_st = this->state(); if (down_st != ClientState::IDLE && down_st != ClientState::INIT) { - this->release_services(); + // The dying stack invalidates its own cache; a cache_clean would just + // warn against a disabled stack. Reset the stream latches directly. + this->service_total_ = 0; + this->services_released_ = true; this->set_idle_(); this->listener_->on_connection_state(false, 0, ble_device_base::GATT_ERR_NOT_CONNECTED); } diff --git a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.h b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.h index 0c3e675935..0f92854690 100644 --- a/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.h +++ b/esphome/components/bluetooth_connection/bluetooth_connection_bluedroid.h @@ -153,6 +153,7 @@ class BluedroidGattClient final : public esp32_ble_tracker::ESPBTClient, public bool mtu_failed_ : 1 {false}; // Search issued at OPEN_EVT overlaps the MTU exchange; discover_services() // completes from it. Reset by set_idle_(). + static_assert(static_cast(SearchState::REPORT_PENDING) < (1 << 4), "search_state_ bitfield too narrow"); SearchState search_state_ : 4 {SearchState::NONE}; // esp_gatt_status_t of the completed search, held until claimed. uint8_t search_status_{0}; diff --git a/esphome/components/bluetooth_connection/bluetooth_connection_hub.h b/esphome/components/bluetooth_connection/bluetooth_connection_hub.h index d63300ab4c..ffa58ef9af 100644 --- a/esphome/components/bluetooth_connection/bluetooth_connection_hub.h +++ b/esphome/components/bluetooth_connection/bluetooth_connection_hub.h @@ -148,6 +148,9 @@ class BluetoothConnection final : public ble_device_base::GattClientListener { char address_str_[MAC_ADDRESS_PRETTY_BUFFER_SIZE]{}; // Group 5: bit-packed tail; within 2 bytes the 8-aligned object stays 48. + static_assert(static_cast(ClientState::ESTABLISHED) < (1 << 3), "state_ bitfield too narrow"); + static_assert(static_cast(ConnectionType::V3_WITHOUT_CACHE) < (1 << 2), + "connection_type_ bitfield too narrow"); ClientState state_ : 3 {ClientState::IDLE}; bool paired_ : 1 {false}; ConnectionType connection_type_ : 2 {ConnectionType::V1};