ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ble_service.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <vector>
7
8#ifdef USE_ESP32
9
10#include <esp_bt_defs.h>
11#include <esp_gap_ble_api.h>
12#include <esp_gatt_defs.h>
13#include <esp_gattc_api.h>
14#include <esp_gatts_api.h>
15
17
18class BLEServer;
19
20using namespace esp32_ble;
21
23 public:
24 BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise);
28
29 BLECharacteristic *create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties);
30 BLECharacteristic *create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties);
31 BLECharacteristic *create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties);
32
33 ESPBTUUID get_uuid() { return this->uuid_; }
34 uint8_t get_inst_id() { return this->inst_id_; }
36 uint16_t get_handle() { return this->handle_; }
37
38 BLEServer *get_server() { return this->server_; }
39
40 void do_create(BLEServer *server);
41 void do_delete();
42 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
43
44 void start();
45 void stop();
46
47 bool is_failed();
48 bool is_created() { return this->state_ == CREATED; }
49 bool is_running() { return this->state_ == RUNNING; }
50 bool is_starting() { return this->state_ == STARTING; }
51 bool is_deleted() { return this->state_ == DELETED; }
52
53 protected:
54 std::vector<BLECharacteristic *> characteristics_;
57 BLEServer *server_ = nullptr;
59 uint16_t num_handles_;
60 uint16_t handle_{0xFFFF};
61 uint8_t inst_id_;
62 bool advertise_{false};
63 bool should_start_{false};
64
66 void stop_();
67
80};
81
82} // namespace esphome::esp32_ble_server
83
84#endif
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties)
BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise)
void do_create(BLEServer *server)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
BLECharacteristic * last_created_characteristic_
Definition ble_service.h:55
enum esphome::esp32_ble_server::BLEService::State INIT
std::vector< BLECharacteristic * > characteristics_
Definition ble_service.h:54
BLECharacteristic * get_last_created_characteristic()
Definition ble_service.h:35
BLECharacteristic * get_characteristic(ESPBTUUID uuid)
static void uint32_t