Skip to content

Commit 3e5b5f5

Browse files
hjleeh2zero
authored andcommitted
Fix descriptor search range in retrieveDescriptors()
The previous implementation incorrectly used the service's end handle when searching for descriptors, which caused it to retrieve descriptors from subsequent characteristics as well. This fix calculates the correct end handle by finding the next characteristic's handle and using (next_handle - 1) as the search limit. This ensures only descriptors belonging to the current characteristic are retrieved. Fixes incorrect descriptor retrieval when multiple characteristics exist in the same service.
1 parent eb2d822 commit 3e5b5f5

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/NimBLERemoteCharacteristic.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,24 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(
9494
bool NimBLERemoteCharacteristic::retrieveDescriptors(NimBLEDescriptorFilter* pFilter) const {
9595
NIMBLE_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str());
9696

97+
const auto pSvc = getRemoteService();
98+
uint16_t endHandle = pSvc->getEndHandle();
99+
100+
// Find the handle of the next characteristic to limit the descriptor search range.
101+
const auto& chars = pSvc->getCharacteristics(false);
102+
for (auto it = chars.begin(); it != chars.end(); ++it) {
103+
if ((*it)->getHandle() == this->getHandle()) {
104+
auto next_it = std::next(it);
105+
if (next_it != chars.end()) {
106+
endHandle = (*next_it)->getHandle() - 1;
107+
NIMBLE_LOGD(LOG_TAG, "Search range limited to handle 0x%04X", endHandle);
108+
}
109+
break;
110+
}
111+
}
112+
97113
// If this is the last handle then there are no descriptors
98-
if (getHandle() == getRemoteService()->getEndHandle()) {
114+
if (getHandle() == endHandle) {
99115
NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): found 0 descriptors.");
100116
return true;
101117
}
@@ -108,7 +124,7 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(NimBLEDescriptorFilter* pFi
108124

109125
int rc = ble_gattc_disc_all_dscs(getClient()->getConnHandle(),
110126
getHandle(),
111-
getRemoteService()->getEndHandle(),
127+
endHandle,
112128
NimBLERemoteCharacteristic::descriptorDiscCB,
113129
pFilter);
114130
if (rc != 0) {

0 commit comments

Comments
 (0)