ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ble_characteristic.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_descriptor.h"
6
7#include <vector>
8#include <span>
9#include <functional>
10#include <memory>
11
12#ifdef USE_ESP32
13
14#include <esp_gap_ble_api.h>
15#include <esp_gatt_defs.h>
16#include <esp_gattc_api.h>
17#include <esp_gatts_api.h>
18#include <esp_bt_defs.h>
19
21
22using namespace esp32_ble;
23using namespace bytebuffer;
24
25class BLEService;
26
28 public:
29 BLECharacteristic(ESPBTUUID uuid, uint32_t properties);
31
32 void set_value(ByteBuffer buffer);
33 void set_value(std::vector<uint8_t> &&buffer);
34 void set_value(std::initializer_list<uint8_t> data);
35 void set_value(const std::string &buffer);
36
37 void set_broadcast_property(bool value);
38 void set_indicate_property(bool value);
39 void set_notify_property(bool value);
40 void set_read_property(bool value);
41 void set_write_property(bool value);
42 void set_write_no_response_property(bool value);
43
44 void notify();
45
46 void do_create(BLEService *service);
47 void do_delete() { this->clients_to_notify_.clear(); }
48 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
49
50 void add_descriptor(BLEDescriptor *descriptor);
51 void remove_descriptor(BLEDescriptor *descriptor);
52
53 BLEService *get_service() { return this->service_; }
54 ESPBTUUID get_uuid() { return this->uuid_; }
55 std::vector<uint8_t> &get_value() { return this->value_; }
56
57 static constexpr uint32_t PROPERTY_READ = 1 << 0;
58 static constexpr uint32_t PROPERTY_WRITE = 1 << 1;
59 static constexpr uint32_t PROPERTY_NOTIFY = 1 << 2;
60 static constexpr uint32_t PROPERTY_BROADCAST = 1 << 3;
61 static constexpr uint32_t PROPERTY_INDICATE = 1 << 4;
62 static constexpr uint32_t PROPERTY_WRITE_NR = 1 << 5;
63
64 bool is_created();
65 bool is_failed();
66
67 // Direct callback registration - only allocates when callback is set
68 void on_write(std::function<void(std::span<const uint8_t>, uint16_t)> &&callback) {
69 this->on_write_callback_ =
70 std::make_unique<std::function<void(std::span<const uint8_t>, uint16_t)>>(std::move(callback));
71 }
72 void on_read(std::function<void(uint16_t)> &&callback) {
73 this->on_read_callback_ = std::make_unique<std::function<void(uint16_t)>>(std::move(callback));
74 }
75
76 protected:
79 esp_gatt_char_prop_t properties_;
80 uint16_t handle_{0xFFFF};
81
82 uint16_t value_read_offset_{0};
83 std::vector<uint8_t> value_;
84 std::vector<BLEDescriptor *> descriptors_;
85
87 uint16_t conn_id;
88 bool indicate; // true = indicate, false = notify
89 };
90 std::vector<ClientNotificationEntry> clients_to_notify_;
91
92 void remove_client_from_notify_list_(uint16_t conn_id);
94
95 void set_property_bit_(esp_gatt_char_prop_t bit, bool value);
96
97 std::unique_ptr<std::function<void(std::span<const uint8_t>, uint16_t)>> on_write_callback_;
98 std::unique_ptr<std::function<void(uint16_t)>> on_read_callback_;
99
100 esp_gatt_perm_t permissions_ = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
101
109};
110
111} // namespace esphome::esp32_ble_server
112
113#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:37
ClientNotificationEntry * find_client_in_notify_list_(uint16_t conn_id)
void remove_descriptor(BLEDescriptor *descriptor)
std::unique_ptr< std::function< void(std::span< const uint8_t >, uint16_t)> > on_write_callback_
enum esphome::esp32_ble_server::BLECharacteristic::State INIT
void set_property_bit_(esp_gatt_char_prop_t bit, bool value)
BLECharacteristic(ESPBTUUID uuid, uint32_t properties)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
void on_write(std::function< void(std::span< const uint8_t >, uint16_t)> &&callback)
std::vector< BLEDescriptor * > descriptors_
void add_descriptor(BLEDescriptor *descriptor)
void on_read(std::function< void(uint16_t)> &&callback)
std::vector< ClientNotificationEntry > clients_to_notify_
std::unique_ptr< std::function< void(uint16_t)> > on_read_callback_
static void uint32_t
bool indicate
uint16_t conn_id