Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - UsbManager.getDeviceList() returns empty

I want to use the UsbManager API by wrote following code:

In the Activity code:

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

        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();

        Log.d(TAG, "size =" + deviceList.size());

In the Manifest:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

    <permission android:name="android.hardware.usb.host" >

</permission>

<uses-feature android:name="android.hardware.usb.host" android:required="true" />

I also followed this question, checked my Galaxy SII 9100G. It does have /system/etc/permissions/android.hardware.usb.host.xml, and the right permissions.

But manager.getDeviceList() always return zero, when I tried flash-driver and mouse.

Can anyone give me any suggestion?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There isn't common answer for your question. It heavily depend on your device. Emulator don't support USB host at all, some devices need root to do it, some devices has right USB host support out of the box.

EDIT

I'm recently finish my own investigation for this problem for device TF-MID1007G. May be this will helpful for someone later. There is great app to perform diagnostic of USB host abilities of device - USB Host Diagnostic (btw many thanks to author, you really save my day ). This app show verdict about usb host support of device, in my case it was usb host supported by kernel, but restricted for third party apps. Fortunately this app can also (on rooted devices only) fix your device system to add ability work with usb host. This fix remains even after reset device. After this fix my app can enumerate usb host devices, request permission and so on, as described in standard android API. So short list of action to enable normal usb host supporting on this device

  • root device with VRoot or Baidu Root (i were used last one, because don't have any windows near me)
  • install USB Host Diagnostic
  • Start diagnosic, and after it unsuccessfully completes it request root privileges and confirm you to fix system, agree with it
  • After fix, sure VERDICT part contain line 3rd party apps - Full
  • After all i were reset my device to remove root (it's dangerous in general, and Baidu Root always request to install some bloatware)

Now this device ready to work with USB devices.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...