From 873de13b3d4663a7428b592b83bfa6acd3470261 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 28 Nov 2022 18:10:02 -1000 Subject: [PATCH] Fix bluetooth_gatt_write_descriptor never getting a response (#4112) --- .../components/bluetooth_proxy/bluetooth_connection.cpp | 8 ++++---- esphome/components/bluetooth_proxy/bluetooth_connection.h | 2 +- esphome/components/bluetooth_proxy/bluetooth_proxy.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp index efd258a456..1fd6f08bb5 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp @@ -236,7 +236,7 @@ esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) { return ESP_OK; } -esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::string &data) { +esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::string &data, bool response) { if (!this->connected()) { ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT descriptor, not connected.", this->connection_index_, this->address_str_.c_str()); @@ -252,9 +252,9 @@ esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::stri ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor %s", this->connection_index_, this->address_str_.c_str(), descriptor->uuid.to_string().c_str()); - auto err = - esp_ble_gattc_write_char_descr(this->gattc_if_, this->conn_id_, descriptor->handle, data.size(), - (uint8_t *) data.data(), ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE); + esp_err_t err = esp_ble_gattc_write_char_descr( + this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(), + response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE); if (err != ERR_OK) { ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char_descr error, err=%d", this->connection_index_, this->address_str_.c_str(), err); diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.h b/esphome/components/bluetooth_proxy/bluetooth_connection.h index fa0233d2fb..43346ef3e7 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.h +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.h @@ -17,7 +17,7 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase { esp_err_t read_characteristic(uint16_t handle); esp_err_t write_characteristic(uint16_t handle, const std::string &data, bool response); esp_err_t read_descriptor(uint16_t handle); - esp_err_t write_descriptor(uint16_t handle, const std::string &data); + esp_err_t write_descriptor(uint16_t handle, const std::string &data, bool response); esp_err_t notify_characteristic(uint16_t handle, bool enable); diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp index b5cb8be359..188e3dd5a5 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.cpp @@ -241,7 +241,7 @@ void BluetoothProxy::bluetooth_gatt_write_descriptor(const api::BluetoothGATTWri return; } - auto err = connection->write_descriptor(msg.handle, msg.data); + auto err = connection->write_descriptor(msg.handle, msg.data, true); if (err != ESP_OK) { api::global_api_server->send_bluetooth_gatt_error(msg.address, msg.handle, err); }