ESPHome 2025.5.0
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
16namespace esphome {
17namespace esp32_ble_server {
18
19class BLEServer;
20
21using namespace esp32_ble;
22
24 public:
25 BLEService(ESPBTUUID uuid, uint16_t num_handles, uint8_t inst_id, bool advertise);
29
30 BLECharacteristic *create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties);
31 BLECharacteristic *create_characteristic(uint16_t uuid, esp_gatt_char_prop_t properties);
32 BLECharacteristic *create_characteristic(ESPBTUUID uuid, esp_gatt_char_prop_t properties);
33
34 ESPBTUUID get_uuid() { return this->uuid_; }
35 uint8_t get_inst_id() { return this->inst_id_; }
37 uint16_t get_handle() { return this->handle_; }
38
39 BLEServer *get_server() { return this->server_; }
40
41 void do_create(BLEServer *server);
42 void do_delete();
43 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
44
45 void start();
46 void stop();
47
48 bool is_failed();
49 bool is_created() { return this->state_ == CREATED; }
50 bool is_running() { return this->state_ == RUNNING; }
51 bool is_starting() { return this->state_ == STARTING; }
52 bool is_deleted() { return this->state_ == DELETED; }
53
54 protected:
55 std::vector<BLECharacteristic *> characteristics_;
58 BLEServer *server_ = nullptr;
60 uint16_t num_handles_;
61 uint16_t handle_{0xFFFF};
62 uint8_t inst_id_;
63 bool advertise_{false};
64 bool should_start_{false};
65
67 void stop_();
68
81};
82
83} // namespace esp32_ble_server
84} // namespace esphome
85
86#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:56
enum esphome::esp32_ble_server::BLEService::State INIT
std::vector< BLECharacteristic * > characteristics_
Definition ble_service.h:55
BLECharacteristic * get_last_created_characteristic()
Definition ble_service.h:36
BLECharacteristic * get_characteristic(ESPBTUUID uuid)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7