How to check whether NFC is enabled or not in android?
Asked Answered
P

5

36

How can i check whether NFC is enabled or not programmatically? Is there any way to enable the NFC on the device from my program? Please help me

Peristyle answered 1/10, 2011 at 6:42 Comment(0)
W
66
NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
    // adapter exists and is enabled.
}

You cannot enable the NFC programmatically. The user has to do it manually through settings or hardware button.

Wina answered 1/10, 2011 at 6:59 Comment(2)
so if the returned value is FALSE, it means the device doesnt have the NFC capability, is that true @userSeven7s?Narcho
That is not correct. If the adapter is null, the device does not have NFC. Explanation on the Developer websiteLilley
A
16

This can be done simply using the following code:

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);

if (nfcAdapter == null) {
    // NFC is not available for device
} else if (!nfcAdapter.isEnabled()) {
    // NFC is available for device but not enabled
} else {
    // NFC is enabled
}

Remember that the user can turn off NFC, even while using your app.

Source: https://developer.android.com/guide/topics/connectivity/nfc/nfc#manifest

Although you can't programically enable NFC yourself, you can ask the user to enable it by having a button to open NFC settings like so:

Intent intent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    intent = new Intent(Settings.ACTION_NFC_SETTINGS);
} else {
    Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
}

startActivity(intent);
Ardeth answered 17/9, 2018 at 22:33 Comment(1)
This is a more complete answer which includes how to direct user to NFC settings. Thanks.Merchantman
F
9

I might be a little late here, but I've implemented a 'complete' example with detection of

  1. NFC capability (hardware), and
  2. Initial NFC state (enabled or disabled in settings), and
  3. Changes to the state

I've also added a corresponding Beam example which uses the

nfcAdapter.isNdefPushEnabled()

method introduced in later Android versions to detect beam state like in 2) and 3).

Falk answered 20/4, 2013 at 0:1 Comment(2)
Can you update your answer, as Google code has shut down.Ardeth
Android Beam is no longer supported.Albemarle
T
8

Use PackageManager and hasSystemFeature("android.hardware.nfc"), matching the <uses-feature android:name="android.hardware.nfc" android:required="false" /> element you should have in your manifest.

Since 2.3.3 you can also use NfcAdapter.getDefaultAdapter() to get the adapter (if available) and call its isEnabled() method to check whether NFC is currently turned on.

Timeout answered 1/10, 2011 at 6:55 Comment(0)
M
1
mNfcAdapter = NfcAdapter.getDefaultAdapter(this.getApplicationContext());
    try {
        if (mNfcAdapter != null) {
            result = true;
        }
    }

We can verify using NfcAdapter with context.

Mandamandaean answered 22/4, 2014 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.