How to Check if Android Device Supports Connecting to USB Devices

(145 Views)


Android API 12 provided a new Feature hasSystemFeature(String) that is available in PackageManager class which let's you determine whether the Android Phone supports Connecting to USB Devices or not. Below is the syntax of hasSystemFeature(String)

public abstract boolean hasSystemFeature (String name)

Android API also provides a constant which can be passed to above method to check for USB Devices support. Its syntax is:

public static final String FEATURE_USB_ACCESSORY Constant Value: "android.hardware.usb.accessory"

Steps to check whether a Android Device Supports Connecting to USB Devices

  1. Create an instance of PackageManager:
  2. context = this; PackageManager pm = context.getPackageManager();
  3. Using PackageManager Instance invoke hasSystemFeature(String) method and pass PackageManager.FEATURE_USB_ACCESSORY as its parameter
  4. If the device supports Connecting to USB Devices, it will return true else it will return false.

Example for Determining whether Android Phone Supports Connecting to USB Devices

context = this; PackageManager pm = context.getPackageManager(); boolean isUsbSupported = pm.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY); if (isUsbSupported) { Toast.makeText(this, "Device supports USB", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Device doesn't supports USB", Toast.LENGTH_LONG).show(); }

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search