4

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.

1pratham
  • 113
  • 1
  • 2
  • 9

2 Answers2

0
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());
}
T.Coutlakis
  • 2,436
  • 1
  • 19
  • 19
-1

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)

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • 1
    I think both of them just checks if any device is connected to android device with android device acting as a USB host. What I need to check is if the android device is connected to a USB host like a PC with the android device acting as a USB device – 1pratham Dec 01 '16 at 15:17
  • there is a USB protocol negotiation in which is determined the basic communication parameters (which device is host which is device,...) -> http://www.beyondlogic.org/usbnutshell/usb3.shtml maybe you can sniff this when installing `usbmon` on your android however this is only possible when recompiling the android kernel (http://unix.stackexchange.com/questions/138742/how-to-dump-usb-traffic) . – ralf htp Dec 02 '16 at 19:02
  • then you can check which USB related kernel modules are active on the android (https://www.cyberciti.biz/faq/linux-show-the-status-of-modules-driver/, http://stackoverflow.com/questions/11396023/getting-a-list-of-active-drivers-on-android) If a USB acts as host the HCI drivers are loaded (https://en.wikipedia.org/wiki/Host_controller_interface) – ralf htp Dec 02 '16 at 19:03