diff --git a/src/include/platform/CHIPDeviceEvent.h b/src/include/platform/CHIPDeviceEvent.h index 2ca528f2cd0924..4b3ca4f7f62ca4 100644 --- a/src/include/platform/CHIPDeviceEvent.h +++ b/src/include/platform/CHIPDeviceEvent.h @@ -258,6 +258,12 @@ enum InternalEventTypes kCHIPoBLEUnsubscribe, kCHIPoBLEWriteReceived, kCHIPoBLEIndicateConfirm, + + /** + * Post this event in case of a BLE connection error. This event should be posted + * if the BLE central disconnects without unsubscribing from the BLE characteristic. + * This event should populate CHIPoBLEConnectionError structure. + */ kCHIPoBLEConnectionError, kCHIPoBLENotifyConfirm }; diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index 62843f9bddbd2c..62e22e0005b981 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -1269,6 +1269,8 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(struct ble_gap_event * gapEvent) peer_delete(gapEvent->disconnect.conn.conn_handle); #endif + // There can be a case where the BLE central disconnects without unsubscribing from the BLE characteristic. + // In such situations, it is necessary to clear the subscription and post a connection error event. if (UnsetSubscribed(gapEvent->disconnect.conn.conn_handle)) { CHIP_ERROR disconReason; @@ -1284,7 +1286,12 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(struct ble_gap_event * gapEvent) disconReason = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT; break; } - HandleConnectionError(gapEvent->disconnect.conn.conn_handle, disconReason); + + ChipDeviceEvent connectionErrorEvent; + connectionErrorEvent.Type = DeviceEventType::kCHIPoBLEConnectionError; + connectionErrorEvent.CHIPoBLEConnectionError.ConId = gapEvent->disconnect.conn.conn_handle; + connectionErrorEvent.CHIPoBLEConnectionError.Reason = disconReason; + ReturnErrorOnFailure(PlatformMgr().PostEvent(&connectionErrorEvent)); } ChipDeviceEvent disconnectEvent;