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

早过忘川 提交于 2019-12-10 21:03:50

问题


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 android box in device mode similarly to when a phone is connected to a PC. At the minimum, I just need to check if something is connected to this USB port and at best get some info(manufacturer, model, serial) of the host connected. I don't need any data communication.

I have tried

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
manager.getAccessoryList();

but it turns up empty even though I have connected my laptop to it and adb and file transfers are working.

Although on further reading it seems android USB accessories API is limited to special android accessories only.


回答1:


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());
}



回答2:


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)



来源:https://stackoverflow.com/questions/40891108/how-to-get-list-of-usb-accessories-connected-to-android-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!