diff --git a/esphome/components/esp32_ble_server/ble_server.cpp b/esphome/components/esp32_ble_server/ble_server.cpp index 8f7931d40e..6b7d1af5ea 100644 --- a/esphome/components/esp32_ble_server/ble_server.cpp +++ b/esphome/components/esp32_ble_server/ble_server.cpp @@ -153,8 +153,8 @@ void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga case ESP_GATTS_CONNECT_EVT: { ESP_LOGD(TAG, "BLE Client connected"); this->add_client_(param->connect.conn_id); - if (this->on_connect_callback_) { - (*this->on_connect_callback_)(param->connect.conn_id); + for (auto &callback : this->on_connect_callbacks_) { + callback(param->connect.conn_id); } break; } @@ -162,8 +162,8 @@ void BLEServer::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t ga ESP_LOGD(TAG, "BLE Client disconnected"); this->remove_client_(param->disconnect.conn_id); this->parent_->advertising_start(); - if (this->on_disconnect_callback_) { - (*this->on_disconnect_callback_)(param->disconnect.conn_id); + for (auto &callback : this->on_disconnect_callbacks_) { + callback(param->disconnect.conn_id); } break; } diff --git a/esphome/components/esp32_ble_server/ble_server.h b/esphome/components/esp32_ble_server/ble_server.h index 51e9bc0814..53579614d2 100644 --- a/esphome/components/esp32_ble_server/ble_server.h +++ b/esphome/components/esp32_ble_server/ble_server.h @@ -55,12 +55,12 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv void ble_before_disabled_event_handler() override; - // Direct callback registration - only allocates when callback is set + // Direct callback registration - supports multiple callbacks void on_connect(std::function &&callback) { - this->on_connect_callback_ = std::make_unique>(std::move(callback)); + this->on_connect_callbacks_.push_back(std::move(callback)); } void on_disconnect(std::function &&callback) { - this->on_disconnect_callback_ = std::make_unique>(std::move(callback)); + this->on_disconnect_callbacks_.push_back(std::move(callback)); } protected: @@ -75,8 +75,8 @@ class BLEServer : public Component, public GATTsEventHandler, public BLEStatusEv void add_client_(uint16_t conn_id) { this->clients_.insert(conn_id); } void remove_client_(uint16_t conn_id) { this->clients_.erase(conn_id); } - std::unique_ptr> on_connect_callback_; - std::unique_ptr> on_disconnect_callback_; + std::vector> on_connect_callbacks_; + std::vector> on_disconnect_callbacks_; std::vector manufacturer_data_{}; esp_gatt_if_t gatts_if_{0};