Question

Is there a way to check at run time whether a device has an NFC reader? My app uses NFC to perform a task, but if no reader is present, it can perform the same task by using a button.

Was it helpful?

Solution

Hope This works for you

NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {

    //Yes NFC available 
}else if(adapter != null && !adapter.isEnabled()){

   //NFC is not enabled.Need to enable by the user.
}else{
   //NFC is not supported
}

OTHER TIPS

The simplest way to check if an Android device has NFC functionality is to check for the system feature PackageManager.FEATURE_NFC ("android.hardware.nfc"):

PackageManager pm = context.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
    // device has NFC functionality
}

However, there exist devices (at least one of Sony's first Android NFC smartphones has this issue) that do not properly report the FEATURE_NFC. (That's those devices that do not allow you to install apps that require NFC functionality through Play Store does such a check for apps that require NFC.)

Therefore, the more reliable solution is the one described by Sainath Patwary karnate. To check if a device has NFC functionality (or rather if a device has a running NFC service), you can use:

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
if (nfcAdapter != null) {
    // device has NFC functionality
}

If you also want to check if the user enabled NFC on their device, you may use the NfcAdapter's isEnabled() method. But be warned that it's not always as easy as described by Sainath Patwary karnate. Particularly on Android 4.0.*, the isEnabled() method sometimes throws undocumented exceptions when the NFC service had crashed before, so you might want to catch those exceptions. Moreover, on Android >= 2.3.4 and < 4.1 (I could not reproduce the problem on later versions but that does not mean it is not there!), the first call to isEnabled() after the NFC service had been stopped or crashed always returned false, so it is advisable to always ignore the result of the first call of isEnabled().

if (nfcAdapter != null) {
    try {
        nfcAdapter.isEnabled();
    } catch (Exception e) {}
    bool isEnabled = false;
    try {
        isEnabled = nfcAdapter.isEnabled();
    } catch (Exception e) {}
    if (isEnabled) {
        // NFC functionality is available and enabled
    }
}

Here's my function that I use for detecting NFC presence.

public static boolean deviceHasNfc() {
    // Is NFC adapter present (whether enabled or not)
    NfcManager nfcMgr = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
    if (manager != null) {
        NfcAdapter adapter = manager.getDefaultAdapter();
        return adapter != null;
    }
    return false;
}

As stated in @Sainath's answer you can also detect if the NFC is enabled using adapter.isEnabled()

For those of you doing Kotlin here is a quick enabled check extension following the rules posted above

fun Context.isNfcEnabled(): Boolean {
    val nfcAdapter = NfcAdapter.getDefaultAdapter(this)
    if (nfcAdapter != null) {
        return try {
            nfcAdapter.isEnabled
        } catch (exp: Exception) {
            // Double try this as there are times it will fail first time
            try {
                nfcAdapter.isEnabled
            } catch (exp: Exception) {
                false
            }
        }
    }
    return false
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top