@@ -132,6 +132,63 @@ bool NimBLERemoteCharacteristic::retrieveDescriptors(NimBLEDescriptorFilter* pFi
132132 return true ;
133133} // retrieveDescriptors
134134
135+ bool NimBLERemoteCharacteristic::retrieveDescriptors2 (NimBLEDescriptorFilter* pFilter) const {
136+ NIMBLE_LOGD (LOG_TAG, " >> retrieveDescriptors2() for characteristic: %s" , getUUID ().toString ().c_str ());
137+
138+ const auto pSvc = getRemoteService ();
139+ uint16_t endHandle = pSvc->getEndHandle ();
140+
141+ // Find the handle of the next characteristic to limit the descriptor search range.
142+ const auto & chars = pSvc->getCharacteristics (false );
143+ for (auto it = chars.begin (); it != chars.end (); ++it) {
144+ if ((*it)->getHandle () == this ->getHandle ()) {
145+ auto next_it = std::next (it);
146+ if (next_it != chars.end ()) {
147+ endHandle = (*next_it)->getHandle () - 1 ;
148+ NIMBLE_LOGD (LOG_TAG, " Search range limited to handle 0x%04X" , endHandle);
149+ }
150+ break ;
151+ }
152+ }
153+
154+ // If this is the last handle then there are no descriptors
155+ if (getHandle () == endHandle) {
156+ NIMBLE_LOGD (LOG_TAG, " << retrieveDescriptors2(): found 0 descriptors." );
157+ return true ;
158+ }
159+
160+ NimBLETaskData taskData (const_cast <NimBLERemoteCharacteristic*>(this ));
161+ NimBLEDescriptorFilter defaultFilter{nullptr , nullptr , &taskData};
162+ if (pFilter == nullptr ) {
163+ pFilter = &defaultFilter;
164+ }
165+
166+ int rc = ble_gattc_disc_all_dscs (getClient ()->getConnHandle (),
167+ getHandle (),
168+ endHandle, // Use the correctly calculated end handle
169+ NimBLERemoteCharacteristic::descriptorDiscCB,
170+ pFilter);
171+ if (rc != 0 ) {
172+ NIMBLE_LOGE (LOG_TAG, " ble_gattc_disc_all_dscs: rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
173+ return false ;
174+ }
175+
176+ auto prevDscCount = m_vDescriptors.size ();
177+ NimBLEUtils::taskWait (taskData, BLE_NPL_TIME_FOREVER);
178+ rc = ((NimBLETaskData*)pFilter->taskData )->m_flags ;
179+ if (rc != BLE_HS_EDONE) {
180+ NIMBLE_LOGE (LOG_TAG, " << retrieveDescriptors2(): failed: rc=%d %s" , rc, NimBLEUtils::returnCodeToString (rc));
181+ return false ;
182+ }
183+
184+ if (m_vDescriptors.size () > prevDscCount) {
185+ pFilter->dsc = m_vDescriptors.back ();
186+ }
187+
188+ NIMBLE_LOGD (LOG_TAG, " << retrieveDescriptors2(): found %d descriptors." , m_vDescriptors.size () - prevDscCount);
189+ return true ;
190+ } // retrieveDescriptors2
191+
135192/* *
136193 * @brief Get the descriptor instance with the given UUID that belongs to this characteristic.
137194 * @param [in] uuid The UUID of the descriptor to find.
@@ -184,7 +241,8 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
184241const std::vector<NimBLERemoteDescriptor*>& NimBLERemoteCharacteristic::getDescriptors (bool refresh) const {
185242 if (refresh) {
186243 deleteDescriptors ();
187- retrieveDescriptors ();
244+ // retrieveDescriptors();
245+ retrieveDescriptors2 ();
188246 }
189247
190248 return m_vDescriptors;
@@ -383,6 +441,10 @@ std::string NimBLERemoteCharacteristic::toString() const {
383441 return res;
384442} // toString
385443
444+ uint8_t NimBLERemoteCharacteristic::getProperties () const {
445+ return m_properties;
446+ }
447+
386448NimBLEClient* NimBLERemoteCharacteristic::getClient () const {
387449 return getRemoteService ()->getClient ();
388450} // getClient
0 commit comments