11#include <freertos/FreeRTOSConfig.h>
12#include <esp_bt_main.h>
13#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID
16#include <freertos/task.h>
17#include <esp_gap_ble_api.h>
21static const char *
const TAG =
"esp32_ble_server";
24 if (this->
parent_->is_failed()) {
26 ESP_LOGE(TAG,
"BLE Server was marked failed by ESP32BLE");
33 if (!this->
parent_->is_active()) {
36 switch (this->state_) {
42 if (service->is_created()) {
46 if (!service->is_starting() && !service->is_running()) {
47 this->services_to_start_[write_idx++] = service;
50 this->services_to_start_.erase(this->services_to_start_.begin() + write_idx, this->services_to_start_.end());
55 esp_err_t err = esp_ble_gatts_app_register(0);
57 ESP_LOGE(TAG,
"esp_ble_gatts_app_register failed: %d", err);
71 if (entry.service == this->device_information_service_) {
74 entry.service->do_create(
this);
84 ESP_LOGD(TAG,
"BLE server setup successfully");
102#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
103 char uuid_buf[esp32_ble::UUID_STR_LEN];
105 ESP_LOGV(TAG,
"Creating BLE service - %s", uuid_buf);
109 for (; inst_id < 0xFF; inst_id++) {
114 if (inst_id == 0xFF) {
115 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
116 uuid.
to_str(warn_uuid_buf);
117 ESP_LOGW(TAG,
"Could not create BLE service %s, too many instances", warn_uuid_buf);
121 new BLEService(uuid, num_handles, inst_id, advertise);
122 this->
services_.push_back({uuid, inst_id, service});
123 if (this->
parent_->is_active() && this->registered_) {
130#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
131 char uuid_buf[esp32_ble::UUID_STR_LEN];
133 ESP_LOGV(TAG,
"Removing BLE service - %s %d", uuid_buf, inst_id);
135 for (
auto it = this->
services_.begin(); it != this->services_.end(); ++it) {
136 if (it->uuid == uuid && it->inst_id == inst_id) {
137 it->service->do_delete();
143 char warn_uuid_buf[esp32_ble::UUID_STR_LEN];
144 uuid.
to_str(warn_uuid_buf);
145 ESP_LOGW(TAG,
"BLE service %s %d does not exist", warn_uuid_buf, inst_id);
150 if (entry.uuid == uuid && entry.inst_id == inst_id) {
151 return entry.service;
159 if (entry.type ==
type) {
160 entry.callback(conn_id);
166 esp_ble_gatts_cb_param_t *param) {
168 case ESP_GATTS_CONNECT_EVT: {
169 ESP_LOGD(TAG,
"BLE Client connected");
173 this->
parent_->advertising_start();
178 case ESP_GATTS_DISCONNECT_EVT: {
179 ESP_LOGD(TAG,
"BLE Client disconnected");
181 this->
parent_->advertising_start();
185 case ESP_GATTS_REG_EVT: {
195 entry.service->gatts_event_handler(event, gatts_if, param);
216 ESP_LOGE(TAG,
"Client array full");
233 entry.service->do_delete();
243 "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_
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)
ESPHOME_ALWAYS_INLINE bool is_running()
bool can_proceed() override
BLEService * get_service(ESPBTUUID uuid, uint8_t inst_id=0)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
std::vector< BLEService * > services_to_start_
void ble_before_disabled_event_handler()
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
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