From 591083a43d2d73424de2d72ff55dd73f22f5d3d3 Mon Sep 17 00:00:00 2001 From: h2zero Date: Sun, 29 Mar 2026 10:54:11 -0600 Subject: [PATCH] [Bugfix] Remote descriptor not found when char handles out of order This resolves the issue of retrieving characteristics out of handle order and then trying to subscribe or do some other fetch of a descriptor belonging to that characteristic and the search handle was limited to the handle of the next characteristic in the vector. This ensures that the vector is sorted in ascending order based on handle values so the next characteristic in the vector will always have a higher handle value and the desciptor serach will function as intended. --- src/NimBLERemoteService.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/NimBLERemoteService.cpp b/src/NimBLERemoteService.cpp index f07e64b45..2651f5fb3 100644 --- a/src/NimBLERemoteService.cpp +++ b/src/NimBLERemoteService.cpp @@ -25,6 +25,7 @@ # include "NimBLELog.h" # include +# include static const char* LOG_TAG = "NimBLERemoteService"; @@ -207,6 +208,12 @@ bool NimBLERemoteService::retrieveCharacteristics(const NimBLEUUID* uuidFilter) NimBLEUtils::taskWait(taskData, BLE_NPL_TIME_FOREVER); rc = taskData.m_flags; if (rc == 0 || rc == BLE_HS_EDONE) { + // sort the characteristics vector by handle to make sure the search range for descriptors is correct + std::sort(m_vChars.begin(), + m_vChars.end(), + [](const NimBLERemoteCharacteristic* a, const NimBLERemoteCharacteristic* b) { + return a->getHandle() < b->getHandle(); + }); NIMBLE_LOGD(LOG_TAG, "<< retrieveCharacteristics()"); return true; }