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

前端 未结 2 1965
清酒与你
清酒与你 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:29

    UsbManager m = (UsbManager)getApplicationContext().getSystemService(USB_SERVICE);
    HashMap usbDevices = m.getDeviceList();
    Collection ite = usbDevices.values();
    UsbDevice[] usbs = ite.toArray(new UsbDevice[]{});
    for (UsbDevice usb : usbs) {    
        Log.d("Connected usb devices","Connected usb devices are "+ usb.getDeviceName());
    }
    

提交回复
热议问题