How to detect if Android has full USB support or not?

南笙酒味 提交于 2019-12-20 02:15:31

问题


My application uses UsbManager to communicate with USB cameras. Some devices don't have USB support. These will return null for (UsbManager)context.getSystemService( Context.USB_SERVICE ), or they will throw a NoSuchMethodError exception when enumerating devices. I can detect both and display a message properly. Unfortunately, some Androids that don't detect USB devices cause neither of these problems. They just return an empty list of USB devices. How can I properly detect that this system doesn't support USB OTG?


回答1:


The official way to determine if the device has USB host capability is to use the associated system feature.

Ideally, add a <uses-feature> element to your manifest, indicating that you are interested in the android.hardware.usb.host feature. If you do not absolutely need that feature, add android:required="false" as an attribute.

If you went with android:required="false", and you want to determine whether the device is a USB host at runtime, use PackageManager, hasSystemFeature(), and FEATURE_USB_HOST. FEATURE_USB_HOST is defined as the same string as you would be using in <uses-feature> (android.hardware.usb.host).




回答2:


public static boolean getUsbConnection(Context context) {
        intent = context.registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE"));
        return intent.getExtras().getBoolean("connected");
    }


来源:https://stackoverflow.com/questions/23934248/how-to-detect-if-android-has-full-usb-support-or-not

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