How to get list of USB accessories connected to android device?

前端 未结 2 1951
清酒与你
清酒与你 2021-01-21 09:13

I have a Lollipop based android box. The box has a USB port similarly to micro USB port in Android phones. I want to check if any host is connected to the USB port with the andr

相关标签:
2条回答
  • 2021-01-21 09:24

    you can use the Android USBManager like in this thread Android : how to detect already connected usb device? or you can install libusb on android: https://github.com/libusb/libusb/tree/master/android

    (https://developer.android.com/guide/topics/connectivity/usb/host.html, https://electronics.stackexchange.com/questions/49140/what-exactly-are-the-difference-between-a-usb-host-and-device)

    0 讨论(0)
  • 2021-01-21 09:29
    UsbManager m = (UsbManager)getApplicationContext().getSystemService(USB_SERVICE);
    HashMap<String, UsbDevice> usbDevices = m.getDeviceList();
    Collection<UsbDevice> ite = usbDevices.values();
    UsbDevice[] usbs = ite.toArray(new UsbDevice[]{});
    for (UsbDevice usb : usbs) {    
        Log.d("Connected usb devices","Connected usb devices are "+ usb.getDeviceName());
    }
    
    0 讨论(0)
提交回复
热议问题