How to Check if Android Device has NFC Feature programmatically in Android Studio


Android API 9 provided a new Feature hasSystemFeature(String) that is available in PackageManager class which let's you determine whether the Android Phone supports NFC(Near Field Communications) 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 NFC. Its syntax is:

public static final String FEATURE_NFC Constant Value: "android.hardware.nfc"

Steps to check whether a Android Device supports NFC

  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_NFC as its parameter
  4. If the device supports NFC, it will return true else it will return false.

Example for Determining whether Android Phone supports NFC

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

If you are facing some IO Permission Issue while compiling above code, check this post Set Permission for I/O Operations over NFC

Solution Worked 0 UpvotesUpvote
        
Solution Didn't Worked 0 DownvotesDownvote


Comments




Search