@@ -137,12 +137,13 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
137137 // characteristic assign overload
138138 chr[i]->assign (&disc_chr->chars [d]);
139139
140- // Discovery All descriptors as well
141- _hdl_range.start_handle = disc_chr->chars [d].handle_value + 1 ;
142- if (_hdl_range.start_handle < _hdl_range.end_handle )
140+ ble_gattc_handle_range_t range = { disc_chr->chars [d].handle_value + 1 , _hdl_range.end_handle };
141+
142+ // Discovery All descriptors if possible
143+ if ( range.start_handle <= range.start_handle )
143144 {
144- // skip discovery descriptor if it is last characteristic without descriptor
145- chr[i]->discoverDescriptor (conn_handle);
145+ // skip discovery descriptor
146+ chr[i]->discoverDescriptor (conn_handle, range );
146147 }
147148
148149 found++;
@@ -153,6 +154,10 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
153154 }
154155
155156 // increase handle range for next discovery
157+ // should be last descriptor +1, but that will cause missing on the next Characteristic !!!!!
158+ // Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 (Char declaration) in the result
159+ //
160+ // To be safe we use last chars + 1
156161 _hdl_range.start_handle = disc_chr->chars [ disc_chr->count -1 ].handle_value + 1 ;
157162 }
158163
@@ -161,13 +166,13 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
161166 return found;
162167}
163168
164- uint16_t BLEDiscovery::_discoverDescriptor (uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t * disc_desc, uint16_t bufsize)
169+ uint16_t BLEDiscovery::_discoverDescriptor (uint16_t conn_handle, ble_gattc_evt_desc_disc_rsp_t * disc_desc, uint16_t bufsize, ble_gattc_handle_range_t hdl_range )
165170{
166- LOG_LV2 (Discover, " [DESC] Handle start = %d, end = %d" , _hdl_range .start_handle , _hdl_range .end_handle );
171+ LOG_LV2 (Discover, " [DESC] Handle start = %d, end = %d" , hdl_range .start_handle , hdl_range .end_handle );
167172
168173 _adamsg.prepare (disc_desc, bufsize);
169174
170- VERIFY_STATUS ( sd_ble_gattc_descriptors_discover (conn_handle, &_hdl_range ), 0 );
175+ VERIFY_STATUS ( sd_ble_gattc_descriptors_discover (conn_handle, &hdl_range ), 0 );
171176
172177 // wait for discovery event
173178 int32_t bytecount = _adamsg.waitUntilComplete (BLE_DISCOVERY_TIMEOUT);
@@ -180,11 +185,6 @@ uint16_t BLEDiscovery::_discoverDescriptor(uint16_t conn_handle, ble_gattc_evt_d
180185 LOG_LV2 (Discover, " [DESC] Descriptor %d: uuid = 0x%04X, handle = %d" , i, disc_desc->descs [i].uuid .uuid , disc_desc->descs [i].handle );
181186 }
182187
183- // increase handle range for next discovery
184- // should be +1 more, but that will cause missing on the next Characteristic !!!!!
185- // Reason is descriptor also include BLE_UUID_CHARACTERISTIC 0x2803 (Char declaration) in the result
186- _hdl_range.start_handle = disc_desc->descs [ disc_desc->count -1 ].handle ;
187-
188188 return disc_desc->count ;
189189}
190190
0 commit comments