diff --git a/src/NimBLEServer.cpp b/src/NimBLEServer.cpp index 3c062f78..f614756d 100644 --- a/src/NimBLEServer.cpp +++ b/src/NimBLEServer.cpp @@ -432,6 +432,23 @@ int NimBLEServer::handleGapEvent(ble_gap_event* event, void* arg) { } peerInfo.m_desc = event->disconnect.conn; + + // Notify characteristics that the client has unsubscribed. + // This ensures onSubscribe callbacks are called with subValue=0 + // so applications can clean up their subscription state. + for (const auto& svc : pServer->m_svcVec) { + for (const auto& chr : svc->getCharacteristics()) { + auto subscribers = chr->getSubscribers(); + for (const auto& entry : subscribers) { + if (entry.getConnHandle() == event->disconnect.conn.conn_handle && + (entry.isSubNotify() || entry.isSubIndicate())) { + chr->processSubRequest(peerInfo, 0); + break; + } + } + } + } + pServer->m_pServerCallbacks->onDisconnect(pServer, peerInfo, event->disconnect.reason); # if !MYNEWT_VAL(BLE_EXT_ADV) if (pServer->m_advertiseOnDisconnect) {