Detect already-connected USB device in main activity

This commit is contained in:
mimi89999
2025-12-29 13:16:01 +01:00
parent aa805d8f09
commit 89e88b2d8d

View File

@@ -185,6 +185,9 @@ class MainActivity : AppCompatActivity() {
registerReceiver(usbAttachReceiver, usbAttachFilter)
}
// Auto-connect to already-plugged USB FIDO devices
checkForUsbDevice()
// Check if started by USB device attachment
if (intent.action == UsbManager.ACTION_USB_DEVICE_ATTACHED) {
val device = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
@@ -310,6 +313,30 @@ class MainActivity : AppCompatActivity() {
showDeviceSelectionDialog(devices)
}
/**
* Check for already-plugged USB FIDO devices and connect if found.
* Only connects if there's exactly one device and we're not already connected.
*/
private fun checkForUsbDevice() {
// Skip if already connected
if (currentTransport?.isConnected == true) {
return
}
val devices = usbManager.deviceList.values
.filter { UsbTransport.isFidoDevice(it) }
// Only auto-connect if exactly one FIDO device is found
if (devices.size == 1) {
val device = devices.first()
if (usbManager.hasPermission(device)) {
connectToUsbDevice(device)
} else {
requestUsbPermission(device)
}
}
}
private fun showDeviceSelectionDialog(devices: Collection<UsbDevice>) {
val dialogView = layoutInflater.inflate(R.layout.dialog_usb_devices, null)
val recyclerView = dialogView.findViewById<RecyclerView>(R.id.deviceList)