ESPHome 2025.6.3
Loading...
Searching...
No Matches
ble_binary_output.cpp
Go to the documentation of this file.
1#include "ble_binary_output.h"
2#include "esphome/core/log.h"
4
5#ifdef USE_ESP32
6namespace esphome {
7namespace ble_client {
8
9static const char *const TAG = "ble_binary_output";
10
12 ESP_LOGCONFIG(TAG, "BLE Binary Output:");
13 ESP_LOGCONFIG(TAG,
14 " MAC address : %s\n"
15 " Service UUID : %s\n"
16 " Characteristic UUID: %s",
17 this->parent_->address_str().c_str(), this->service_uuid_.to_string().c_str(),
18 this->char_uuid_.to_string().c_str());
19 LOG_BINARY_OUTPUT(this);
20}
21
22void BLEBinaryOutput::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
23 esp_ble_gattc_cb_param_t *param) {
24 switch (event) {
25 case ESP_GATTC_SEARCH_CMPL_EVT: {
26 auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
27 if (chr == nullptr) {
28 ESP_LOGW(TAG, "Characteristic %s was not found in service %s", this->char_uuid_.to_string().c_str(),
29 this->service_uuid_.to_string().c_str());
30 break;
31 }
32 this->char_handle_ = chr->handle;
33 this->char_props_ = chr->properties;
34 if (this->require_response_ && this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
35 this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
36 ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_RSP");
37 } else if (!this->require_response_ && this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
38 this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
39 ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_NO_RSP");
40 } else {
41 ESP_LOGE(TAG, "Characteristic %s does not allow writing with%s response", this->char_uuid_.to_string().c_str(),
42 this->require_response_ ? "" : "out");
43 break;
44 }
45 this->node_state = espbt::ClientState::ESTABLISHED;
46 ESP_LOGD(TAG, "Found characteristic %s on device %s", this->char_uuid_.to_string().c_str(),
47 this->parent()->address_str().c_str());
48 this->node_state = espbt::ClientState::ESTABLISHED;
49 break;
50 }
51 case ESP_GATTC_WRITE_CHAR_EVT: {
52 if (param->write.handle == this->char_handle_) {
53 if (param->write.status != 0)
54 ESP_LOGW(TAG, "[%s] Write error, status=%d", this->char_uuid_.to_string().c_str(), param->write.status);
55 }
56 break;
57 }
58 default:
59 break;
60 }
61}
62
64 if (this->node_state != espbt::ClientState::ESTABLISHED) {
65 ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
66 this->char_uuid_.to_string().c_str());
67 return;
68 }
69 uint8_t state_as_uint = (uint8_t) state;
70 ESP_LOGV(TAG, "[%s] Write State: %d", this->char_uuid_.to_string().c_str(), state_as_uint);
71 esp_err_t err =
72 esp_ble_gattc_write_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->char_handle_,
73 sizeof(state_as_uint), &state_as_uint, this->write_type_, ESP_GATT_AUTH_REQ_NONE);
74 if (err != ESP_GATT_OK)
75 ESP_LOGW(TAG, "[%s] Write error, err=%d", this->char_uuid_.to_string().c_str(), err);
76}
77
78} // namespace ble_client
79} // namespace esphome
80#endif
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 to_string() const
Definition ble_uuid.cpp:171
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7