ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
ble_client.h
Go to the documentation of this file.
1#pragma once
2
7
8#ifdef USE_ESP32
9
10#include <esp_bt_defs.h>
11#include <esp_gap_ble_api.h>
12#include <esp_gatt_common_api.h>
13#include <esp_gattc_api.h>
14#include <array>
15#include <string>
16#include <vector>
17
18namespace esphome::ble_client {
19
21
22using namespace esp32_ble_client;
23
24class BLEClient;
25
27 public:
28 virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
29 esp_ble_gattc_cb_param_t *param){};
30 virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {}
31 virtual void loop() {}
32 void set_address(uint64_t address) { address_ = address; }
34 // This should be transitioned to Established once the node no longer needs
35 // the services/descriptors/characteristics of the parent client. This will
36 // allow some memory to be freed.
37 // The parent frees the peer's GATT cache once every node reports Established.
38 // Never report Established while an operation that reads that cache is outstanding.
39 // - esp_ble_gattc_register_for_notify() completes asynchronously.
40 // - Register from ESP_GATTC_SEARCH_CMPL_EVT, then set this from ESP_GATTC_REG_FOR_NOTIFY_EVT.
41 // - BLEClientBase::register_for_notify() holds the release until the registration completes.
42 espbt::ClientState node_state;
43
44 BLEClient *parent() { return this->parent_; }
46
47 protected:
49 uint64_t address_;
50};
51
52class BLEClient final : public BLEClientBase {
53 public:
54 void setup() override;
55 void dump_config() override;
56 void loop() override;
57
58 bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
59 esp_ble_gattc_cb_param_t *param) override;
60
61 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
62 bool parse_device(const espbt::ESPBTDevice &device) override;
63
64 void set_enabled(bool enabled);
65
67 node->client = this;
68 node->set_ble_client_parent(this);
69 this->nodes_.push_back(node);
70 }
71
72 bool enabled;
73
74 void set_state(espbt::ClientState state) override;
75
76 protected:
78
79 std::vector<BLEClientNode *> nodes_;
80};
81
82} // namespace esphome::ble_client
83
84#endif
uint8_t address
Definition bl0906.h:4
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
void register_ble_node(BLEClientNode *node)
Definition ble_client.h:66
bool parse_device(const espbt::ESPBTDevice &device) override
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
void set_state(espbt::ClientState state) override
std::vector< BLEClientNode * > nodes_
Definition ble_client.h:79
void set_enabled(bool enabled)
espbt::ESPBTClient * client
Definition ble_client.h:33
void set_ble_client_parent(BLEClient *parent)
Definition ble_client.h:45
virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
Definition ble_client.h:28
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
Definition ble_client.h:30
void set_address(uint64_t address)
Definition ble_client.h:32
Base class for BLE GATT clients that connect to remote devices.