From 595b416b3721adc4a32cf1882350bf64ae1de974 Mon Sep 17 00:00:00 2001
From: Rodrigo Garcia <rodrigo.garcia@espressif.com>
Date: Thu, 23 Jun 2022 15:55:44 -0300
Subject: [PATCH] Fixes reading BLE Remote Descriptor

---
 libraries/BLE/src/BLERemoteDescriptor.cpp | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/libraries/BLE/src/BLERemoteDescriptor.cpp b/libraries/BLE/src/BLERemoteDescriptor.cpp
index 1538741d437..f99cdb8c8f3 100644
--- a/libraries/BLE/src/BLERemoteDescriptor.cpp
+++ b/libraries/BLE/src/BLERemoteDescriptor.cpp
@@ -51,9 +51,26 @@ BLEUUID BLERemoteDescriptor::getUUID() {
 
 void BLERemoteDescriptor::gattClientEventHandler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* evtParam) {
 	switch(event) {
+		// ESP_GATTC_READ_DESCR_EVT
+		// This event indicates that the server has responded to the read request.
+		//
+		// read:
+		// - esp_gatt_status_t  status
+		// - uint16_t           conn_id
+		// - uint16_t           handle
+		// - uint8_t*           value
+		// - uint16_t           value_len
 		case ESP_GATTC_READ_DESCR_EVT:
-			if (evtParam->read.handle != getHandle())
-				break;
+			// If this event is not for us, then nothing further to do.
+			if (evtParam->read.handle != getHandle()) break;
+			// At this point, we have determined that the event is for us, so now we save the value
+			if (evtParam->read.status == ESP_GATT_OK) {
+				// it will read the cached value of the descriptor
+				m_value = std::string((char*) evtParam->read.value, evtParam->read.value_len);
+			} else {
+				m_value = "";
+			}
+			// Unlock the semaphore to ensure that the requestor of the data can continue.
 			m_semaphoreReadDescrEvt.give();
 			break;