ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
ble_client.cpp
Go to the documentation of this file.
1#include "ble_client.h"
6#include "esphome/core/log.h"
7
8#ifdef USE_ESP32
9
10namespace esphome::ble_client {
11
12static const char *const TAG = "ble_client";
13
16 this->enabled = true;
17}
18
21 for (auto *node : this->nodes_)
22 node->loop();
23}
24
26 ESP_LOGCONFIG(TAG, "BLE Client:");
28}
29
31 if (!this->enabled)
32 return false;
33 return BLEClientBase::parse_device(device);
34}
35
36void BLEClient::set_enabled(bool enabled) {
37 if (enabled == this->enabled)
38 return;
39 this->enabled = enabled;
40 if (!enabled) {
41 ESP_LOGI(TAG, "[%s] Disabling BLE client.", this->address_str());
42 this->disconnect();
43 }
44}
45
46bool BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,
47 esp_ble_gattc_cb_param_t *param) {
48 if (!BLEClientBase::gattc_event_handler(event, esp_gattc_if, param))
49 return false;
50
51 for (auto *node : this->nodes_)
52 node->gattc_event_handler(event, esp_gattc_if, param);
53
54 // The release frees the GATT cache that BLEClientBase's CCCD lookup still needs.
55 // The last REG_FOR_NOTIFY event clears the counter before node dispatch, so the release still runs here.
56 if (!this->services_.empty() && !this->notify_registration_pending() && this->all_nodes_established_()) {
57 this->release_services();
58 ESP_LOGD(TAG, "All clients established, services released");
59 }
60 return true;
61}
62
63void BLEClient::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
65
66 for (auto *node : this->nodes_)
67 node->gap_event_handler(event, param);
68}
69
70void BLEClient::set_state(espbt::ClientState state) {
72 for (auto &node : nodes_)
73 node->node_state = state;
74}
75
77 if (this->state() != espbt::ClientState::ESTABLISHED)
78 return false;
79 for (auto &node : nodes_) {
80 if (node->node_state != espbt::ClientState::ESTABLISHED)
81 return false;
82 }
83 return true;
84}
85
86} // namespace esphome::ble_client
87
88#endif
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
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)
std::vector< BLEService * > services_
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
void set_state(espbt::ClientState st) override
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
bool parse_device(const espbt::ESPBTDevice &device) override
bool state
Definition fan.h:2