Skip to content

Commit 7ca386e

Browse files
committed
fix: connect ble found devices directly
1 parent 316bf77 commit 7ca386e

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

app/src/main/java/to/bitkit/ui/sheets/hardware/HwConnectViewModel.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class HwConnectViewModel @Inject constructor(
3838
companion object {
3939
/** Delay between scan attempts while searching for a nearby device. */
4040
private val SCAN_INTERVAL = 2.seconds
41+
42+
/** Prefix used by Android USB attach intents for [android.hardware.usb.UsbDevice.deviceName]. */
43+
private const val USB_DEVICE_PATH_PREFIX = "/dev/"
4144
}
4245

4346
private val _uiState = MutableStateFlow(HwConnectUiState())
@@ -80,7 +83,7 @@ class HwConnectViewModel @Inject constructor(
8083
errorMessage = null,
8184
)
8285
}
83-
scanUsbBeforeConnect = true
86+
scanUsbBeforeConnect = deviceId.startsWith(USB_DEVICE_PATH_PREFIX)
8487
}
8588

8689
fun onConnectClick(deviceIdOverride: String? = null) {

app/src/test/java/to/bitkit/ui/sheets/hardware/HwConnectViewModelTest.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlinx.coroutines.test.runCurrent
1414
import org.junit.Before
1515
import org.junit.Test
1616
import org.mockito.kotlin.mock
17+
import org.mockito.kotlin.never
1718
import org.mockito.kotlin.verify
1819
import org.mockito.kotlin.whenever
1920
import to.bitkit.R
@@ -102,11 +103,12 @@ class HwConnectViewModelTest : BaseUnitTest() {
102103
}
103104

104105
@Test
105-
fun `onConnectClick scans usb before connecting a device seeded by route`() = test {
106+
fun `onConnectClick scans usb before connecting a usb attach route path`() = test {
107+
val devicePath = "/dev/bus/usb/001/002"
106108
val connectedFeatures = features(model = "Safe 3")
107109
whenever(hwWalletRepo.scan(includeBluetooth = false)).thenReturn(Result.success(emptyList()))
108-
whenever(hwWalletRepo.connect("usb1")).thenReturn(Result.success(connectedFeatures))
109-
sut.onFoundRoute(deviceId = "usb1", deviceModel = "Trezor")
110+
whenever(hwWalletRepo.connect(devicePath)).thenReturn(Result.success(connectedFeatures))
111+
sut.onFoundRoute(deviceId = devicePath, deviceModel = "Trezor")
110112

111113
sut.effects.test {
112114
sut.onConnectClick()
@@ -115,7 +117,23 @@ class HwConnectViewModelTest : BaseUnitTest() {
115117
}
116118

117119
verify(hwWalletRepo).scan(includeBluetooth = false)
118-
verify(hwWalletRepo).connect("usb1")
120+
verify(hwWalletRepo).connect(devicePath)
121+
}
122+
123+
@Test
124+
fun `onConnectClick does not usb rescan before connecting a bluetooth route device`() = test {
125+
val connectedFeatures = features(model = "Safe 7")
126+
whenever(hwWalletRepo.connect("ble-device-id")).thenReturn(Result.success(connectedFeatures))
127+
sut.onFoundRoute(deviceId = "ble-device-id", deviceModel = "Trezor Safe 7")
128+
129+
sut.effects.test {
130+
sut.onConnectClick()
131+
assertEquals(HwConnectEffect.NavigateToPaired, awaitItem())
132+
cancelAndIgnoreRemainingEvents()
133+
}
134+
135+
verify(hwWalletRepo).connect("ble-device-id")
136+
verify(hwWalletRepo, never()).scan(includeBluetooth = false)
119137
}
120138

121139
@Test

0 commit comments

Comments
 (0)