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

[9342 views]




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

                  Liked Article? Please Buy me a Tea 😭





Learn Python in a Week and Master It

Comments







Search
Need Programming Help??


Below Offers ends in












VPN Pro Secure and Fast v3.0.8 Premium Mod Apk Download

Quiz For You:

Search Tags