Check programmatically if device has NFC reader
Asked Answered
A

4

19

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.

Acanthus answered 9/5, 2014 at 12:17 Comment(0)
N
55

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
}
Non answered 9/5, 2014 at 12:21 Comment(3)
You might want to differentiate between adapter == null (no reader exists) and !adapter.isEnabled() (the reader has been turned off). This way, the user could be prompted to enable the NFC adapter.Tweezers
ya '@323go' it always depend on the individuals, is that developer needs that or not, if he don't want to show popup simple he will check only with adapter != null condition i think it not a big dealNon
I disagree. You could provide a complete answer, and let the developer decide what the target audience is.Tweezers
G
12

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
    }
}
Gerrard answered 9/5, 2014 at 15:27 Comment(2)
but nfc service could crash arbitrarily, or only in particular situation?Acanthus
Typically, on devices with NXP chipset, the NFC service often crashes when communication with a card is interrupted or when the phone does not properly recognize a card (e.g. because the card does not receive sufficient power).Gerrard
M
2

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()

Methodist answered 14/6, 2018 at 21:14 Comment(0)
L
2

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
}
Latifundium answered 16/7, 2019 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.