UsbManager.getDeviceList() returns empty

前端 未结 4 752
南笙
南笙 2020-12-21 05:46

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

In the Activity code:

        UsbManager manager = (UsbManager) getSystemServi         


        
相关标签:
4条回答
  • 2020-12-21 06:00

    I have had this problem with a Huawei Mate 20 Pro running Android Pie. Same programs work perfectly on Android 5 or 7 devices. There is some evidence this is an Android Pie problem: https://issuetracker.google.com/issues/121435921

    0 讨论(0)
  • 2020-12-21 06:02

    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.

    0 讨论(0)
  • 2020-12-21 06:07

    This code works fine for me. Although your code looks similar you can recheck.

      UsbManager usbManager = (UsbManager) getSystemService(USB_SERVICE);
      HashMap<String, UsbDevice> map = usbManager.getDeviceList();
      Iterator<UsbDevice> it = map.values().iterator();
    
      while(it.hasNext()) {
        UsbDevice device = it.next();
        // use device info
      }
    
    0 讨论(0)
  • 2020-12-21 06:09

    Probably you are trying to run through android emulator but.....according to Android specs for the emulator : http://developer.android.com/tools/devices/emulator.html#limitations

    The functional limitations of the emulator include:

    No support for USB connections
    

    So, only with real device you can test UsbManager

    0 讨论(0)
提交回复
热议问题