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