12#include <freertos/FreeRTOSConfig.h>
13#include <esp_bt_main.h>
14#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
17#include <freertos/task.h>
18#include <esp_gap_ble_api.h>
21namespace esp32_ble_server {
23static const char *
const TAG =
"esp32_ble_server";
26 if (this->
parent_->is_failed()) {
28 ESP_LOGE(TAG,
"BLE Server was marked failed by ESP32BLE");
35 if (!this->
parent_->is_active()) {
38 switch (this->state_) {
43 if (service->is_created()) {
48 this->services_to_start_.erase(
49 std::remove_if(this->services_to_start_.begin(), this->services_to_start_.end(),
50 [](
BLEService *service) { return service->is_starting() || service->is_running(); }),
51 this->services_to_start_.end());
56 esp_err_t err = esp_ble_gatts_app_register(0);
58 ESP_LOGE(TAG,
"esp_ble_gatts_app_register failed: %d", err);
72 if (entry.service == this->device_information_service_) {
75 entry.service->do_create(
this);
85 ESP_LOGD(TAG,
"BLE server setup successfully");
105#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
106 char uuid_buf[esp32_ble::UUID_STR_LEN];
108 ESP_LOGV(TAG,
"Creating BLE service - %s", uuid_buf);
112 for (; inst_id < 0xFF; inst_id++) {
117 if (inst_id == 0xFF) {
118 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
119 uuid.
to_str(warn_uuid_buf);
120 ESP_LOGW(TAG,
"Could not create BLE service %s, too many instances", warn_uuid_buf);
124 new BLEService(uuid, num_handles, inst_id, advertise);
125 this->
services_.push_back({uuid, inst_id, service});
126 if (this->
parent_->is_active() && this->registered_) {
133#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
134 char uuid_buf[esp32_ble::UUID_STR_LEN];
136 ESP_LOGV(TAG,
"Removing BLE service - %s %d", uuid_buf, inst_id);
138 for (
auto it = this->
services_.begin(); it != this->services_.end(); ++it) {
139 if (it->uuid == uuid && it->inst_id == inst_id) {
140 it->service->do_delete();
146 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
147 uuid.
to_str(warn_uuid_buf);
148 ESP_LOGW(TAG,
"BLE service %s %d does not exist", warn_uuid_buf, inst_id);
153 if (entry.uuid == uuid && entry.inst_id == inst_id) {
154 return entry.service;
162 if (entry.type ==
type) {
163 entry.callback(conn_id);
169 esp_ble_gatts_cb_param_t *param) {
171 case ESP_GATTS_CONNECT_EVT: {
172 ESP_LOGD(TAG,
"BLE Client connected");
176 this->
parent_->advertising_start();
181 case ESP_GATTS_DISCONNECT_EVT: {
182 ESP_LOGD(TAG,
"BLE Client disconnected");
184 this->
parent_->advertising_start();
188 case ESP_GATTS_REG_EVT: {
198 entry.service->gatts_event_handler(event, gatts_if, param);
219 ESP_LOGE(TAG,
"Client array full");
236 entry.service->do_delete();
246 "ESP32 BLE Server:\n"
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use to_str() instead. Removed in 2026.8.0", "2026.2.0") std const char * to_str(std::span< char, UUID_STR_LEN > output) const
std::vector< CallbackEntry > callbacks_
void ble_before_disabled_event_handler() override
std::vector< uint8_t > manufacturer_data_
BLEService * device_information_service_
void remove_client_(uint16_t conn_id)
float get_setup_priority() const override
enum esphome::esp32_ble_server::BLEServer::State INIT
void dump_config() override
void dispatch_callbacks_(CallbackType type, uint16_t conn_id)
bool can_proceed() override
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
std::vector< BLEService * > services_to_start_
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) override
void remove_service(ESPBTUUID uuid, uint8_t inst_id=0)
void add_client_(uint16_t conn_id)
std::vector< ServiceEntry > services_
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
void restart_advertising_()
int8_t find_client_index_(uint16_t conn_id) const
void do_create(BLEServer *server)
BLEServer * global_ble_server
constexpr float AFTER_BLUETOOTH
Providing packet encoding functions for exchanging data with a remote host.