@@ -108,10 +108,10 @@ const std::vector<NimBLERemoteCharacteristic*>& NimBLERemoteService::getCharacte
108108 * @brief Callback for Characteristic discovery.
109109 * @return success == 0 or error code.
110110 */
111- int NimBLERemoteService::characteristicDiscCB (uint16_t connHandle,
112- const ble_gatt_error* error,
113- const ble_gatt_chr* chr,
114- void * arg) {
111+ int NimBLERemoteService::chrDiscCB (uint16_t connHandle,
112+ const ble_gatt_error* error,
113+ const ble_gatt_chr* chr,
114+ void * arg) {
115115 const int rc = error->status ;
116116 auto pTaskData = (NimBLETaskData*)arg;
117117 const auto pSvc = (NimBLERemoteService*)pTaskData->m_pInstance ;
@@ -137,26 +137,22 @@ int NimBLERemoteService::characteristicDiscCB(uint16_t connHandle,
137137 * This function will not return until we have all the characteristics.
138138 * @return True if successfully retrieved, success = BLE_HS_EDONE.
139139 */
140- bool NimBLERemoteService::retrieveCharacteristics (const NimBLEUUID* uuidFilter , NimBLERemoteCharacteristic** out) const {
141- NIMBLE_LOGD (LOG_TAG, " >> retrieveCharacteristics()" );
140+ bool NimBLERemoteService::retrieveCharacteristics (const NimBLEUUID* uuid , NimBLERemoteCharacteristic** out) const {
141+ NIMBLE_LOGD (LOG_TAG, " >> retrieveCharacteristics() for service: %s " , getUUID (). toString (). c_str () );
142142 NimBLETaskData taskData (const_cast <NimBLERemoteService*>(this ));
143- int rc = 0 ;
144-
145- if (uuidFilter == nullptr ) {
146- rc = ble_gattc_disc_all_chrs (m_pClient->getConnHandle (),
147- getHandle (),
148- getEndHandle (),
149- NimBLERemoteService::characteristicDiscCB,
150- &taskData);
151- } else {
152- rc = ble_gattc_disc_chrs_by_uuid (m_pClient->getConnHandle (),
153- getHandle (),
154- getEndHandle (),
155- uuidFilter->getBase (),
156- NimBLERemoteService::characteristicDiscCB,
157- &taskData);
143+ const uint16_t hdlConn = m_pClient->getConnHandle ();
144+ const uint16_t hdlEnd = getEndHandle ();
145+ const uint16_t hdlStart = getHandle ();
146+ // If this is the last handle then there are no more characteristics
147+ if (hdlStart == hdlEnd) {
148+ NIMBLE_LOGD (LOG_TAG, " << retrieveCharacteristics(): found %d characteristics." , m_vChars.size ());
149+ return true ;
158150 }
159151
152+ int rc = (uuid == nullptr )
153+ ? ble_gattc_disc_all_chrs (hdlConn, hdlStart, hdlEnd, chrDiscCB, &taskData)
154+ : ble_gattc_disc_chrs_by_uuid (hdlConn, hdlStart, hdlEnd, uuid->getBase (), chrDiscCB, &taskData);
155+
160156 if (rc != 0 ) {
161157 NIMBLE_LOGE (LOG_TAG, " ble_gattc_disc_chrs rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
162158 return false ;
0 commit comments