Skip to content

Commit b7c4f37

Browse files
committed
fix(suite-native): enable to confirm receive address outside our app
- We are closing device connection on going to background, so we have to reopen it once needed. We can open the device even on background. But Suite has to be still running, not killed. - Closing all the devices OnActivityEntersBackground is a good thing because we want to allow other apps (like browser) to work with the Trezor too and it is a good practice to frees up system resources.
1 parent 65a8499 commit b7c4f37

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

Diff for: packages/react-native-usb/android/src/main/java/io/trezor/rnusb/ReactNativeUsbModule.kt

+19-4
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,15 @@ class ReactNativeUsbModule : Module() {
150150
for (device in devicesList) {
151151
if (usbManager.hasPermission(device)) {
152152
Log.d(LOG_TAG, "Has permission, send event onDeviceConnected: $device")
153-
val webUsbDevice = openDevice(device.deviceName)
153+
154+
val webUsbDevice = if (hasOpenedConnection(device.deviceName)) {
155+
Log.d(LOG_TAG, "Device already opened: $device")
156+
getWebUSBDevice(device)
157+
} else {
158+
Log.d(LOG_TAG, "Opening device: $device")
159+
openDevice(device.deviceName)
160+
}
161+
154162
sendEvent(ON_DEVICE_CONNECT_EVENT_NAME, webUsbDevice)
155163
devicesHistory[device.deviceName] = webUsbDevice
156164
} else if (!devicesRequestedPermissions.contains(
@@ -218,7 +226,7 @@ class ReactNativeUsbModule : Module() {
218226
Log.e(LOG_TAG, "Failed to open device ${device.deviceName}")
219227
throw Exception("Failed to open device ${device.deviceName}")
220228
}
221-
Log.d(LOG_TAG, "Opening device ${device.deviceName}")
229+
222230
openedConnections[device.deviceName] = usbConnection
223231

224232
// log all endpoints for debug purposes
@@ -300,7 +308,10 @@ class ReactNativeUsbModule : Module() {
300308
val dataByteArray = data.split(",").map { it.toInt().toByte() }.toByteArray()
301309
Log.d(LOG_TAG, "dataByteArray: $dataByteArray")
302310
val device = getDeviceByName(deviceName)
303-
val usbConnection = getOpenedConnection(deviceName)
311+
val usbConnection = openedConnections.getOrPut(device.deviceName) {
312+
Log.d(LOG_TAG, "Reopening device ${device.deviceName}")
313+
usbManager.openDevice(device) ?: throw Exception("Failed to open device ${device.deviceName}")
314+
}
304315

305316
val usbEndpoint = device.getInterface(INTERFACE_INDEX).getEndpoint(1)
306317
if (usbEndpoint == null) {
@@ -315,7 +326,11 @@ class ReactNativeUsbModule : Module() {
315326
private fun transferIn(deviceName: String, endpointNumber: Int, length: Int): IntArray {
316327
Log.d(LOG_TAG, "Reading data from device $deviceName")
317328
val device = getDeviceByName(deviceName)
318-
val usbConnection = getOpenedConnection(deviceName)
329+
330+
val usbConnection = openedConnections.getOrPut(device.deviceName) {
331+
Log.d(LOG_TAG, "Reopening device ${device.deviceName}")
332+
usbManager.openDevice(device) ?: throw Exception("Failed to open device ${device.deviceName}")
333+
}
319334

320335
val usbEndpoint = device.getInterface(INTERFACE_INDEX).getEndpoint(0)
321336

0 commit comments

Comments
 (0)