Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check programmatically if device has NFC reader

Tags:

android

nfc

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.

like image 916
giozh Avatar asked May 09 '14 12:05

giozh


People also ask

How do I know if my phone has an NFC reader?

You can check for NFC settings on your Samsung Android device by looking in the settings app > connections > tap NFC and contactless payments. If you see an option for NFC there, your device is enabled to make NFC payments.

How do I check if NFC is enabled in Android programmatically?

How to check whether NFC is enabled or not in android? NfcManager manager = (NfcManager) context. getSystemService(Context. NFC_SERVICE); NfcAdapter adapter = manager.

How do I identify NFC tags?

An object equipped with NFC Tag can be uniquely identified thanks to the unique code of the chip. A very interesting feature of NFC tags is that they do not need any direct power supply, because they are activated directly by the magnetic field of the NFC sensor of the mobile phone or the device that reads them.


2 Answers

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
}
like image 124
Sainath Patwary karnate Avatar answered Oct 23 '22 07:10

Sainath Patwary karnate


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
    }
}
like image 45
Michael Roland Avatar answered Oct 23 '22 05:10

Michael Roland