fix(server): notify characteristics on client disconnect#1081
fix(server): notify characteristics on client disconnect#1081m-mcgowan wants to merge 1 commit intoh2zero:masterfrom
Conversation
When a BLE client disconnects, iterate through all characteristics and call processSubRequest(peerInfo, 0) for any that had active subscriptions from the disconnecting client. This ensures that: 1. The onSubscribe callback is called with subValue=0 2. Applications tracking subscriber counts get notified 3. Subscription state is properly cleaned up Without this fix, applications using NuS-NimBLE-Serial or similar wrappers that track subscriber counts would retain stale subscription state after client disconnects, causing issues with reconnection. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Thanks, this is good but the stack should be sending |
|
The CCCD write that triggers Happy to add logging to capture the exact GAP events if helpful. This seemed to be the simplest fix to handle the situation with the events that are currently sent. |
|
I'd like to see some logs for sure, in my experience the subscribe event is triggered with a 0 value every time a client disconnects if they were subscribed to a notification. My log just now: |
Summary
When a BLE client disconnects, characteristics with active subscriptions are not notified. This leaves applications with stale subscription state, causing issues with reconnection.
This PR adds code to the
BLE_GAP_EVENT_DISCONNECThandler to iterate through all characteristics and callprocessSubRequest(peerInfo, 0)for any that had active subscriptions from the disconnecting client.Problem
Applications using wrappers like NuS-NimBLE-Serial track subscriber counts via the
onSubscribecallback. Without this fix:onSubscribe(subValue=1)called → count = 1Solution
In
BLE_GAP_EVENT_DISCONNECT, before callingonDisconnect:processSubRequest(peerInfo, 0)to trigger theonSubscribecallback withsubValue=0This ensures:
onSubscribecallback is called withsubValue=0Testing
Tested with ESP32-S3 using Arduino framework and NuS-NimBLE-Serial. Before the fix, reconnection after disconnect would fail. After the fix, reconnection works reliably.
Note
An equivalent fix has been submitted to esp-nimble-cpp (#393 in h2zero/esp-nimble-cpp) for ESP-IDF builds.