ESPHome 2025.6.3
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
20 LOG_TEXT_SENSOR("", "BLE Text Sensor", this);
21 ESP_LOGCONFIG(TAG,
22 " MAC address : %s\n"
23 " Service UUID : %s\n"
24 " Characteristic UUID: %s\n"
25 " Descriptor UUID : %s\n"
26 " Notifications : %s",
27 this->parent()->address_str().c_str(), this->service_uuid_.to_string().c_str(),
28 this->char_uuid_.to_string().c_str(), this->descr_uuid_.to_string().c_str(), YESNO(this->notify_));
29 LOG_UPDATE_INTERVAL(this);
30}
31
32void BLETextSensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
33 esp_ble_gattc_cb_param_t *param) {
34 switch (event) {
35 case ESP_GATTC_OPEN_EVT: {
36 if (param->open.status == ESP_GATT_OK) {
37 ESP_LOGI(TAG, "[%s] Connected successfully!", this->get_name().c_str());
38 break;
39 }
40 break;
41 }
42 case ESP_GATTC_CLOSE_EVT: {
43 this->status_set_warning();
44 this->publish_state(EMPTY);
45 break;
46 }
47 case ESP_GATTC_SEARCH_CMPL_EVT: {
48 this->handle = 0;
49 auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
50 if (chr == nullptr) {
51 this->status_set_warning();
52 this->publish_state(EMPTY);
53 ESP_LOGW(TAG, "No sensor characteristic found at service %s char %s", this->service_uuid_.to_string().c_str(),
54 this->char_uuid_.to_string().c_str());
55 break;
56 }
57 this->handle = chr->handle;
58 if (this->descr_uuid_.get_uuid().len > 0) {
59 auto *descr = chr->get_descriptor(this->descr_uuid_);
60 if (descr == nullptr) {
61 this->status_set_warning();
62 this->publish_state(EMPTY);
63 ESP_LOGW(TAG, "No sensor descriptor found at service %s char %s descr %s",
64 this->service_uuid_.to_string().c_str(), this->char_uuid_.to_string().c_str(),
65 this->descr_uuid_.to_string().c_str());
66 break;
67 }
68 this->handle = descr->handle;
69 }
70 if (this->notify_) {
71 auto status = esp_ble_gattc_register_for_notify(this->parent()->get_gattc_if(),
72 this->parent()->get_remote_bda(), chr->handle);
73 if (status) {
74 ESP_LOGW(TAG, "esp_ble_gattc_register_for_notify failed, status=%d", status);
75 }
76 } else {
77 this->node_state = espbt::ClientState::ESTABLISHED;
78 }
79 break;
80 }
81 case ESP_GATTC_READ_CHAR_EVT: {
82 if (param->read.handle == this->handle) {
83 if (param->read.status != ESP_GATT_OK) {
84 ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
85 break;
86 }
88 this->publish_state(this->parse_data(param->read.value, param->read.value_len));
89 }
90 break;
91 }
92 case ESP_GATTC_NOTIFY_EVT: {
93 if (param->notify.handle != this->handle)
94 break;
95 ESP_LOGV(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
96 param->notify.handle, param->notify.value[0]);
97 this->publish_state(this->parse_data(param->notify.value, param->notify.value_len));
98 break;
99 }
100 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
101 if (param->reg_for_notify.status == ESP_GATT_OK && param->reg_for_notify.handle == this->handle)
102 this->node_state = espbt::ClientState::ESTABLISHED;
103 break;
104 }
105 default:
106 break;
107 }
108}
109
110std::string BLETextSensor::parse_data(uint8_t *value, uint16_t value_len) {
111 std::string text(value, value + value_len);
112 return text;
113}
114
116 if (this->node_state != espbt::ClientState::ESTABLISHED) {
117 ESP_LOGW(TAG, "[%s] Cannot poll, not connected", this->get_name().c_str());
118 return;
119 }
120 if (this->handle == 0) {
121 ESP_LOGW(TAG, "[%s] Cannot poll, no service or characteristic found", this->get_name().c_str());
122 return;
123 }
124
125 auto status = esp_ble_gattc_read_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->handle,
126 ESP_GATT_AUTH_REQ_NONE);
127 if (status) {
128 this->status_set_warning();
129 this->publish_state(EMPTY);
130 ESP_LOGW(TAG, "[%s] Error sending read request for sensor, status=%d", this->get_name().c_str(), status);
131 }
132}
133
134} // namespace ble_client
135} // namespace esphome
136#endif
uint8_t status
Definition bl0942.h:8
void status_set_warning(const char *message="unspecified")
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