ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
ble_server.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_service.h"
5
11
12#include <memory>
13#include <vector>
14#include <unordered_map>
15#include <functional>
16
17#ifdef USE_ESP32
18
19#include <esp_gatts_api.h>
20
22
23using namespace esp32_ble;
24using namespace bytebuffer;
25
26class BLEServer final : public Component, public Parented<ESP32BLE> {
27 public:
28 void setup() override;
29 void loop() override;
30 void dump_config() override;
31 float get_setup_priority() const override;
32 bool can_proceed() override;
33
34 ESPHOME_ALWAYS_INLINE bool is_running() { return this->parent_->is_active() && this->state_ == RUNNING; }
35
36 void set_manufacturer_data(const std::vector<uint8_t> &data) {
37 this->manufacturer_data_ = data;
39 }
40
46 void set_advertising_required(bool required) { this->advertising_required_ = required; }
47
48 void set_max_clients(uint8_t max_clients) { this->max_clients_ = max_clients; }
49 uint8_t get_max_clients() const { return this->max_clients_; }
50
51 BLEService *create_service(ESPBTUUID uuid, bool advertise = false, uint16_t num_handles = 15);
52 void remove_service(ESPBTUUID uuid, uint8_t inst_id = 0);
53 BLEService *get_service(ESPBTUUID uuid, uint8_t inst_id = 0);
54 void enqueue_start_service(BLEService *service) { this->services_to_start_.push_back(service); }
56
57 esp_gatt_if_t get_gatts_if() { return this->gatts_if_; }
59 const uint16_t *get_clients() const { return this->clients_; }
60 uint8_t get_client_count() const { return this->client_count_; }
61
62 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
63
65
66 // Direct callback registration - supports multiple callbacks
67 void on_connect(std::function<void(uint16_t)> &&callback) {
68 this->callbacks_.push_back({CallbackType::ON_CONNECT, std::move(callback)});
69 }
70 void on_disconnect(std::function<void(uint16_t)> &&callback) {
71 this->callbacks_.push_back({CallbackType::ON_DISCONNECT, std::move(callback)});
72 }
73
74 protected:
75 enum class CallbackType : uint8_t {
78 };
79
82 std::function<void(uint16_t)> callback;
83 };
84
90
94
95 int8_t find_client_index_(uint16_t conn_id) const;
96 void add_client_(uint16_t conn_id);
97 void remove_client_(uint16_t conn_id);
98 void dispatch_callbacks_(CallbackType type, uint16_t conn_id);
99
100 std::vector<CallbackEntry> callbacks_;
101
102 std::vector<uint8_t> manufacturer_data_{};
103 esp_gatt_if_t gatts_if_{0};
104 bool registered_{false};
107
108 uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]{};
109 uint8_t client_count_{0};
110 uint8_t max_clients_{1};
111 std::vector<ServiceEntry> services_{};
112 std::vector<BLEService *> services_to_start_{};
114
121};
122
123// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
124extern BLEServer *global_ble_server;
125
126} // namespace esphome::esp32_ble_server
127
128#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:1910
void set_advertising_required(bool required)
Whether this server needs the device to advertise so clients can find and connect to it.
Definition ble_server.h:46
std::vector< CallbackEntry > callbacks_
Definition ble_server.h:100
const uint16_t * get_clients() const
Definition ble_server.h:59
std::vector< uint8_t > manufacturer_data_
Definition ble_server.h:102
void set_device_information_service(BLEService *service)
Definition ble_server.h:55
void set_manufacturer_data(const std::vector< uint8_t > &data)
Definition ble_server.h:36
void remove_client_(uint16_t conn_id)
float get_setup_priority() const override
enum esphome::esp32_ble_server::BLEServer::State INIT
void dispatch_callbacks_(CallbackType type, uint16_t conn_id)
ESPHOME_ALWAYS_INLINE bool is_running()
Definition ble_server.h:34
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_
Definition ble_server.h:112
void enqueue_start_service(BLEService *service)
Definition ble_server.h:54
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_
Definition ble_server.h:111
uint16_t clients_[USE_ESP32_BLE_MAX_CONNECTIONS]
Definition ble_server.h:108
int8_t find_client_index_(uint16_t conn_id) const
void on_connect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:67
void on_disconnect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:70
void set_max_clients(uint8_t max_clients)
Definition ble_server.h:48
uint16_t type
static void uint32_t
Definition ble_server.h:80
CallbackType type
Definition ble_server.h:81
std::function< void(uint16_t)> callback
Definition ble_server.h:82
Definition ble_server.h:85
ESPBTUUID uuid
Definition ble_server.h:86
BLEService * service
Definition ble_server.h:88
uint8_t inst_id
Definition ble_server.h:87