@@ -644,6 +644,8 @@ class TrezorRepo @Inject constructor(
644644 awaitSetup()
645645 TrezorDebugLog .log(" RECONNECT" , " Setup OK" )
646646 TrezorDebugLog .log(" RECONNECT" , " Scanning for devices..." )
647+ val knownDevices = (_state .value.knownDevices + loadKnownDevices()).distinctBy { it.id }
648+ val knownDevice = knownDevices.find { it.matches(deviceId) }
647649 val scannedDevices = trezorService.scan()
648650 TrezorDebugLog .log(
649651 " RECONNECT" ,
@@ -652,6 +654,7 @@ class TrezorRepo @Inject constructor(
652654 // Honor the transport the user selected — connect to exactly the
653655 // entry they tapped instead of overriding Bluetooth with USB.
654656 val device = scannedDevices.find { it.id == deviceId }
657+ ? : knownDevice?.takeIf { it.transportType == TransportType .BLUETOOTH }?.toDeviceInfo()
655658 ? : throw AppError (" Device not found nearby — is it powered on?" )
656659 TrezorDebugLog .log(" RECONNECT" , " Found matching device: id=${device.id} , name=${device.name} " )
657660 TrezorDebugLog .log(" RECONNECT" , " Calling connectWithThpRetry..." )
@@ -952,15 +955,27 @@ class TrezorRepo @Inject constructor(
952955 throw e
953956 }
954957 TrezorDebugLog .log(" THPRetry" , " Error is retryable, attempting second connect..." )
955- Logger .warn(" Connection failed for $deviceId , retrying" , e, context = TAG )
958+ Logger .warn(" Failed to connect to ' $deviceId ' , retrying" , e, context = TAG )
956959 logCredentialFileState(deviceId, " BEFORE 2nd attempt" )
957- val result = connectDevice(deviceId, selection, requestUsbPermission)
960+ val result = runSuspendCatching {
961+ connectDevice(deviceId, selection, requestUsbPermission)
962+ }.onFailure {
963+ disconnectAfterFailedConnect(deviceId)
964+ }.getOrThrow()
958965 logCredentialFileState(deviceId, " AFTER 2nd attempt (success)" )
959966 TrezorDebugLog .log(" THPRetry" , " Second attempt succeeded" )
960967 result
961968 }
962969 }
963970
971+ private suspend fun disconnectAfterFailedConnect (deviceId : String ) {
972+ runSuspendCatching { trezorService.disconnect() }
973+ .onFailure {
974+ Logger .warn(" Failed to disconnect stale Trezor session for '$deviceId '" , it, context = TAG )
975+ }
976+ _state .update { it.copy(connected = null ) }
977+ }
978+
964979 private suspend fun connectDevice (
965980 deviceId : String ,
966981 selection : WalletSelection ,
@@ -1034,6 +1049,16 @@ data class KnownDevice(
10341049
10351050private fun KnownDevice.matches (deviceId : String ) = id == deviceId || path == deviceId
10361051
1052+ private fun KnownDevice.toDeviceInfo () = TrezorDeviceInfo (
1053+ id = id,
1054+ transportType = transportType.toCoreTransportType(),
1055+ name = name,
1056+ path = path,
1057+ label = label,
1058+ model = model,
1059+ isBootloader = false ,
1060+ )
1061+
10371062private fun TrezorTransportType.toTransportType (): TransportType = when (this ) {
10381063 TrezorTransportType .BLUETOOTH -> TransportType .BLUETOOTH
10391064 TrezorTransportType .USB -> TransportType .USB
0 commit comments