Skip to content

Commit 20f9e8b

Browse files
committed
Update SerialConnectionFactory.kt
1 parent e8f77e2 commit 20f9e8b

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

Transmission/src/main/java/org/operatorfoundation/transmission/SerialConnectionFactory.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ class SerialConnectionFactory(context: Context)
231231
_connectionState.value = ConnectionState.Disconnected
232232
}
233233

234+
/**
235+
* Closes the current connection and resets state to Disconnected.
236+
* Does not cancel the connection scope, so reconnection remains possible.
237+
* Call this when a USB device detach event is received.
238+
*/
239+
fun onDeviceDetached() {
240+
val currentState = _connectionState.value
241+
if (currentState is ConnectionState.Connected) {
242+
currentState.connection.close()
243+
}
244+
_connectionState.value = ConnectionState.Disconnected
245+
}
246+
234247
/**
235248
* Creates the actual SerialConnection instance from a permitted USB device.
236249
*

0 commit comments

Comments
 (0)