Skip to content

Commit 3422cf5

Browse files
authored
Find characteristic end handle using the next characterisic handle. Fixes #170
On some devices, searching for descriptors using the service end handle would return an invalid error. This patch instead uses the handle of the next characteristic as the end handle (if available).
1 parent 96c60fb commit 3422cf5

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/NimBLERemoteCharacteristic.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,21 @@ int NimBLERemoteCharacteristic::descriptorDiscCB(uint16_t conn_handle,
209209
bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filter) {
210210
NIMBLE_LOGD(LOG_TAG, ">> retrieveDescriptors() for characteristic: %s", getUUID().toString().c_str());
211211

212+
uint16_t endHandle = getRemoteService()->getEndHandle(this);
213+
if(m_handle >= endHandle) {
214+
return false;
215+
}
216+
212217
int rc = 0;
213218
ble_task_data_t taskData = {this, xTaskGetCurrentTaskHandle(), 0, nullptr};
214219
desc_filter_t filter = {uuid_filter, &taskData};
215220

216221
rc = ble_gattc_disc_all_dscs(getRemoteService()->getClient()->getConnId(),
217222
m_handle,
218-
getRemoteService()->getEndHandle(),
223+
endHandle,
219224
NimBLERemoteCharacteristic::descriptorDiscCB,
220225
&filter);
226+
221227
if (rc != 0) {
222228
NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_all_chrs: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
223229
return false;
@@ -226,12 +232,13 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(const NimBLEUUID *uuid_filt
226232
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
227233

228234
if(taskData.rc != 0) {
235+
NIMBLE_LOGE(LOG_TAG, "ble_gattc_disc_all_chrs: startHandle:%d endHandle:%d taskData.rc=%d %s", m_handle, endHandle, taskData.rc, NimBLEUtils::returnCodeToString(0x0100+taskData.rc));
229236
return false;
230237
}
231238

232239
return true;
233240
NIMBLE_LOGD(LOG_TAG, "<< retrieveDescriptors(): Found %d descriptors.", m_descriptorVector.size());
234-
} // getDescriptors
241+
} // retrieveDescriptors
235242

236243

237244
/**

src/NimBLERemoteService.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,23 @@ uint16_t NimBLERemoteService::getEndHandle() {
249249
return m_endHandle;
250250
} // getEndHandle
251251

252+
/**
253+
* @brief Get the end handle of specified NimBLERemoteCharacteristic.
254+
*/
255+
256+
uint16_t NimBLERemoteService::getEndHandle(NimBLERemoteCharacteristic *pCharacteristic) {
257+
uint16_t endHandle = m_endHandle;
258+
259+
for(auto &it: m_characteristicVector) {
260+
uint16_t defHandle = it->getDefHandle() - 1;
261+
if(defHandle > pCharacteristic->getDefHandle() && endHandle > defHandle) {
262+
endHandle = defHandle;
263+
}
264+
}
265+
266+
return endHandle;
267+
} // getEndHandle
268+
252269

253270
/**
254271
* @brief Get the service start handle.

src/NimBLERemoteService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class NimBLERemoteService {
7070

7171
uint16_t getStartHandle();
7272
uint16_t getEndHandle();
73+
uint16_t getEndHandle(NimBLERemoteCharacteristic *pCharacteristic);
7374
void releaseSemaphores();
7475

7576
// Properties

0 commit comments

Comments
 (0)