ESPHome 2026.11.0b6
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
22class BLEService final {
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 esp_gatt_perm_t permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
31 BLECharacteristic *create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties,
32 esp_gatt_perm_t permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
33 BLECharacteristic *create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties,
34 esp_gatt_perm_t permissions = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE);
35
36 ESPBTUUID get_uuid() { return this->uuid_; }
37 uint8_t get_inst_id() { return this->inst_id_; }
39 uint16_t get_handle() { return this->handle_; }
40
41 BLEServer *get_server() { return this->server_; }
42
43 void do_create(BLEServer *server);
44 void do_delete();
45 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
46
47 void start();
48 void stop();
49
50 bool is_failed();
51 bool is_created() { return this->state_ == CREATED; }
52 bool is_running() { return this->state_ == RUNNING; }
53 bool is_starting() { return this->state_ == STARTING; }
54 bool is_stopped() { return this->state_ == STOPPED; }
55 bool is_deleted() { return this->state_ == DELETED; }
56
57 protected:
58 std::vector<BLECharacteristic *> characteristics_;
61 BLEServer *server_ = nullptr;
62 ESPBTUUID uuid_;
63 uint16_t num_handles_;
64 uint16_t handle_{0xFFFF};
65 uint8_t inst_id_;
66 bool advertise_{false};
67 bool should_start_{false};
68
70 void stop_();
71
84};
85
86} // namespace esphome::esp32_ble_server
87
88#endif
BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise)
void do_create(BLEServer *server)
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties, esp_gatt_perm_t permissions=ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE)
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:59
enum esphome::esp32_ble_server::BLEService::State INIT
std::vector< BLECharacteristic * > characteristics_
Definition ble_service.h:58
BLECharacteristic * get_last_created_characteristic()
Definition ble_service.h:38
BLECharacteristic * get_characteristic(ESPBTUUID uuid)
static void uint32_t