ESPHome  2025.2.0
ble_client.cpp
Go to the documentation of this file.
1 #include "ble_client.h"
5 #include "esphome/core/helpers.h"
6 #include "esphome/core/log.h"
7 
8 #ifdef USE_ESP32
9 
10 namespace esphome {
11 namespace ble_client {
12 
13 static const char *const TAG = "ble_client";
14 
17  this->enabled = true;
18 }
19 
22  for (auto *node : this->nodes_)
23  node->loop();
24 }
25 
27  ESP_LOGCONFIG(TAG, "BLE Client:");
29 }
30 
32  if (!this->enabled)
33  return false;
34  return BLEClientBase::parse_device(device);
35 }
36 
38  if (enabled == this->enabled)
39  return;
40  this->enabled = enabled;
41  if (!enabled) {
42  ESP_LOGI(TAG, "[%s] Disabling BLE client.", this->address_str().c_str());
43  this->disconnect();
44  }
45 }
46 
47 bool BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,
48  esp_ble_gattc_cb_param_t *param) {
49  if (!BLEClientBase::gattc_event_handler(event, esp_gattc_if, param))
50  return false;
51 
52  for (auto *node : this->nodes_)
53  node->gattc_event_handler(event, esp_gattc_if, param);
54 
55  if (!this->services_.empty() && this->all_nodes_established_()) {
56  this->release_services();
57  ESP_LOGD(TAG, "All clients established, services released");
58  }
59  return true;
60 }
61 
62 void BLEClient::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
64 
65  for (auto *node : this->nodes_)
66  node->gap_event_handler(event, param);
67 }
68 
71  for (auto &node : nodes_)
72  node->node_state = state;
73 }
74 
76  if (this->state() != espbt::ClientState::ESTABLISHED)
77  return false;
78  for (auto &node : nodes_) {
79  if (node->node_state != espbt::ClientState::ESTABLISHED)
80  return false;
81  }
82  return true;
83 }
84 
85 } // namespace ble_client
86 } // namespace esphome
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
Definition: ble_client.cpp:47
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition: ble_client.cpp:62
std::vector< BLEService * > services_
bool parse_device(const espbt::ESPBTDevice &device) override
Definition: ble_client.cpp:31
void set_enabled(bool enabled)
Definition: ble_client.cpp:37
virtual void set_state(ClientState st)
void set_state(espbt::ClientState state) override
Definition: ble_client.cpp:69
std::vector< BLEClientNode * > nodes_
Definition: ble_client.h:75
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool parse_device(const espbt::ESPBTDevice &device) 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