3

I am developing an application, which uses a usb device through Usb host mode. I use an IntentFilter for ACTION_USB_DEVICE_ATTACHED, so I can connect and communicate with the device after it is attached. The problem occurs when I turn in (or reboot) the phone with the usb device already plugged in. When I open my application after turning on, I cannot see the device.

The following code results in an empty deviceList.

usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();

The Android Developers Reference says about the getDeviceList():

The result will be empty if no devices are attached, or if USB host mode is inactive or unsupported.

USB host mode is obviously supported on the phone, and a device is attached too. So I guess the host mode is inactive by default.

If I disconnect and reattach the usb device, it works perfectly, but because the device and the phone is in a custom-made holder together, reconnecting them is a bit complicated. Is there a way to activate the host mode from code?

matzahtli
  • 41
  • 5
  • I have a somewhat similar but opposite question, my ACTION_USB_DEVICE_ATTACHED broadcast sends itself after a reboot: https://stackoverflow.com/questions/45814702/prevent-usb-device-attached-launching-app-after-a-reboot – Daniel Wilson Aug 22 '17 at 10:11

0 Answers0