ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
ble_text_sensor.cpp
Go to the documentation of this file.
1#include "ble_text_sensor.h"
2
6#include "esphome/core/log.h"
7
8#ifdef USE_ESP32
9
10namespace esphome {
11namespace ble_client {
12
13static const char *const TAG = "ble_text_sensor";
14
15static const std::string EMPTY = "";
16
18 // Parent BLEClientNode has a loop() method, but this component uses
19 // polling via update() and BLE callbacks so loop isn't needed
20 this->disable_loop();
21}
22
24 LOG_TEXT_SENSOR("", "BLE Text Sensor", this);
25 ESP_LOGCONFIG(TAG,
26 " MAC address : %s\n"
27 " Service UUID : %s\n"
28 " Characteristic UUID: %s\n"
29 " Descriptor UUID : %s\n"
30 " Notifications : %s",
31 this->parent()->address_str().c_str(), this->service_uuid_.to_string().c_str(),
32 this->char_uuid_.to_string().c_str(), this->descr_uuid_.to_string().c_str(), YESNO(this->notify_));
33 LOG_UPDATE_INTERVAL(this);
34}
35
36void BLETextSensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
37 esp_ble_gattc_cb_param_t *param) {
38 switch (event) {
39 case ESP_GATTC_OPEN_EVT: {
40 if (param->open.status == ESP_GATT_OK) {
41 ESP_LOGI(TAG, "[%s] Connected successfully!", this->get_name().c_str());
42 break;
43 }
44 break;
45 }
46 case ESP_GATTC_CLOSE_EVT: {
47 this->status_set_warning();
48 this->publish_state(EMPTY);
49 break;
50 }
51 case ESP_GATTC_SEARCH_CMPL_EVT: {
52 this->handle = 0;
53 auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
54 if (chr == nullptr) {
55 this->status_set_warning();
56 this->publish_state(EMPTY);
57 ESP_LOGW(TAG, "No sensor characteristic found at service %s char %s", this->service_uuid_.to_string().c_str(),
58 this->char_uuid_.to_string().c_str());
59 break;
60 }
61 this->handle = chr->handle;
62 if (this->descr_uuid_.get_uuid().len > 0) {
63 auto *descr = chr->get_descriptor(this->descr_uuid_);
64 if (descr == nullptr) {
65 this->status_set_warning();
66 this->publish_state(EMPTY);
67 ESP_LOGW(TAG, "No sensor descriptor found at service %s char %s descr %s",
68 this->service_uuid_.to_string().c_str(), this->char_uuid_.to_string().c_str(),
69 this->descr_uuid_.to_string().c_str());
70 break;
71 }
72 this->handle = descr->handle;
73 }
74 if (this->notify_) {
75 auto status = esp_ble_gattc_register_for_notify(this->parent()->get_gattc_if(),
76 this->parent()->get_remote_bda(), chr->handle);
77 if (status) {
78 ESP_LOGW(TAG, "esp_ble_gattc_register_for_notify failed, status=%d", status);
79 }
80 } else {
81 this->node_state = espbt::ClientState::ESTABLISHED;
82 }
83 break;
84 }
85 case ESP_GATTC_READ_CHAR_EVT: {
86 if (param->read.handle == this->handle) {
87 if (param->read.status != ESP_GATT_OK) {
88 ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
89 break;
90 }
92 this->publish_state(this->parse_data(param->read.value, param->read.value_len));
93 }
94 break;
95 }
96 case ESP_GATTC_NOTIFY_EVT: {
97 if (param->notify.handle != this->handle)
98 break;
99 ESP_LOGV(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
100 param->notify.handle, param->notify.value[0]);
101 this->publish_state(this->parse_data(param->notify.value, param->notify.value_len));
102 break;
103 }
104 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
105 if (param->reg_for_notify.status == ESP_GATT_OK && param->reg_for_notify.handle == this->handle)
106 this->node_state = espbt::ClientState::ESTABLISHED;
107 break;
108 }
109 default:
110 break;
111 }
112}
113
114std::string BLETextSensor::parse_data(uint8_t *value, uint16_t value_len) {
115 std::string text(value, value + value_len);
116 return text;
117}
118
120 if (this->node_state != espbt::ClientState::ESTABLISHED) {
121 ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str());
122 return;
123 }
124 if (this->handle == 0) {
125 ESP_LOGW(TAG, "[%s] Cannot poll, no service or characteristic found", this->get_name().c_str());
126 return;
127 }
128
129 auto status = esp_ble_gattc_read_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->handle,
130 ESP_GATT_AUTH_REQ_NONE);
131 if (status) {
132 this->status_set_warning();
133 this->publish_state(EMPTY);
134 ESP_LOGW(TAG, "[%s] Error sending read request for sensor, status=%d", this->get_name().c_str(), status);
135 }
136}
137
138} // namespace ble_client
139} // namespace esphome
140#endif
uint8_t status
Definition bl0942.h:8
void status_set_warning(const char *message=nullptr)
void disable_loop()
Disable this component's loop.
void status_clear_warning()
const StringRef & get_name() const
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
std::string parse_data(uint8_t *value, uint16_t value_len)
std::string to_string() const
Definition ble_uuid.cpp:171
esp_bt_uuid_t get_uuid() const
Definition ble_uuid.cpp:170
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
void publish_state(const std::string &state)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7