0

I am building an Android app using Kotlin. What I am trying to do is that I am trying to list down all the devices connected via USB. But the device list is always empty.

First I put in the following declaration in the AndroidManifest.xml file:

<uses-feature android:name="android.hardware.usb.host" />

When the activity is open, within the onCreate method, I called the following function to get the list of USB connected devices:

private fun getDeviceList(): ArrayList<UsbDevice> {
        var deviceList: ArrayList<UsbDevice> = ArrayList()

        var usbManager: UsbManager = activity?.getSystemService(Context.USB_SERVICE) as UsbManager
        for (device in usbManager.deviceList.values) {
            deviceList.add(
                UsbDevice(
                    id = device.deviceId.toString(),
                    name = device.deviceName,
                    model = "device"
                )
            )
        }

        if (usbManager.accessoryList != null) {
            for (accessory in usbManager.accessoryList) {
                deviceList.add(
                    UsbDevice(
                        id = accessory.serial.toString(),
                        name = accessory.description.toString(),
                        model = "accessory"
                    )
                )
            }   
        }

        return deviceList
    }

I have connected my device to my laptop. But when I opened the app, the device list is always empty. What is wrong with code and how can I fix it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

1 Answers1

2

According to the documentation the feature android.hardware.usb.host does the following:

When your Android-powered device is in USB host mode, it acts as the USB host, powers the bus, and enumerates connected USB devices. USB host mode is supported in Android 3.1 and higher.

So in other words this feature is designed to enumerate USB OTG devices like USB flash drives, webcams, or other USB client devices).

When you connect laptop and phone via USB the laptop usually becomes the USB host (master) and the phone is the client. Therefore this feature is not designed to work with such a connection and thus you are getting no device.

The only way to make it work would be to select in the USB notification menu that the Android phone should work as USB host. But as most PC are not able to act as USB client this will also not get any result.

Robert
  • 39,162
  • 17
  • 99
  • 152
  • I set my Android Device as host using my phone. My laptop can be client. But it is still now showing up in the list. – Wai Yan Hein Jun 06 '21 at 18:46
  • What device type is your laptop in client mode? – Robert Jun 06 '21 at 19:40
  • It Acer - E5-574G-5713 with Windows 10 installed. – Wai Yan Hein Jun 06 '21 at 19:55
  • If is a regular PC I don't think that it can act as USB client why do you think it can? Therefore I was asking what USB device type it mimics when in client mode. – Robert Jun 06 '21 at 20:10
  • Which means it is a PC in a compact case including monitor keyboard, touchpad and battery. As PCs are usually not USB client capable (USB dual role) you can't transmit data between two PCs connected via USB-C unless one support the dual role, see here https://superuser.com/q/1215710/62676 – Robert Jun 07 '21 at 07:08
  • Yes, that was the issue. I plugged into a device that can be the client device and it appeared in the list. – Wai Yan Hein Jun 09 '21 at 12:11
  • Hello, what about the Raspberry Pi model 4? I tried connecting my Android Device to the Raspberry Pi and the Pi is not detected by the app. I suppose Pi can be acting as the client. @Robert – Wai Yan Hein Jun 12 '21 at 13:47
  • Please read this and the linked question https://raspberrypi.stackexchange.com/questions/32197/can-the-pi-emulate-an-hid-device-with-via-usb – Robert Jun 12 '21 at 14:51
  • Thanks. My Raspberry PI cannot be the client. When I used Arduino, it is working. – Wai Yan Hein Jun 13 '21 at 15:05