Skip to content

Conversation

@syscode1
Copy link

Problem

The NimBLERemoteCharacteristic::retrieveDescriptors() function incorrectly uses the service's end handle (getRemoteService()->getEndHandle()) as the search range limit when discovering descriptors.

This causes the following bug:

  • Descriptors from subsequent characteristics within the same service are incorrectly included
  • Results in wrong descriptors being associated with the current characteristic
  • Causes memory waste and logical errors

Example scenario:

Service (Handle 0x0001 - 0x0010)
  ├─ Characteristic A (Handle 0x0002)
  │   ├─ Descriptor A1 (Handle 0x0003)
  │   └─ Descriptor A2 (Handle 0x0004)
  ├─ Characteristic B (Handle 0x0005)
  │   └─ Descriptor B1 (Handle 0x0006)

Before fix: Searching descriptors for Characteristic A returns A1, A2, and B1
After fix: Searching descriptors for Characteristic A returns only A1 and A2 ✅

Solution

This PR fixes the issue by:

  1. Finding the next characteristic in the service
  2. Using next_characteristic_handle - 1 as the end handle for descriptor search
  3. Ensuring only descriptors belonging to the current characteristic are retrieved
  4. Maintaining backward compatibility for the last characteristic in a service

Changes Made

  • Modified retrieveDescriptors() to iterate through service characteristics
  • Calculate correct endHandle based on the next characteristic's handle
  • Falls back to service end handle for the last characteristic
  • Added debug logging for the calculated search range

Testing

This fix correctly handles:

  • ✅ Services with multiple characteristics
  • ✅ Last characteristic in a service
  • ✅ Characteristics with no descriptors

Files Changed

  • src/NimBLERemoteCharacteristic.cpp (+18 lines, -2 lines)

@h2zero
Copy link
Owner

h2zero commented Nov 30, 2025

Thanks! Looks like I broke this in 2.0.0 and hasn't been noticed until now. Could you please rebase this so only the changes required are in the PR?

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.
@syscode1 syscode1 force-pushed the fix/retrieve-descriptors-range branch from e865c64 to f4846e8 Compare November 30, 2025 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants