public final class

NfcAdapter

extends Object
java.lang.Object
   ↳ android.nfc.NfcAdapter

Class Overview

Represents the local NFC adapter.

Use the helper getDefaultAdapter(Context) to get the default NFC adapter for this Android device.

Summary

Constants
String ACTION_NDEF_DISCOVERED Intent to start an activity when a tag with NDEF payload is discovered.
String ACTION_TAG_DISCOVERED Intent to start an activity when a tag is discovered.
String ACTION_TECH_DISCOVERED Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag.
String EXTRA_ID Optional extra containing a byte array containing the ID of the discovered tag for the ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED, and ACTION_TAG_DISCOVERED intents.
String EXTRA_NDEF_MESSAGES Optional extra containing an array of NdefMessage present on the discovered tag for the ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED, and ACTION_TAG_DISCOVERED intents.
String EXTRA_TAG Mandatory extra containing the Tag that was discovered for the ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED, and ACTION_TAG_DISCOVERED intents.
Public Methods
void disableForegroundDispatch(Activity activity)
Disable foreground dispatch to the given activity.
void disableForegroundNdefPush(Activity activity)
Disable NDEF message push over P2P.
void enableForegroundDispatch(Activity activity, PendingIntent intent, IntentFilter[] filters, String[][] techLists)
Enable foreground dispatch to the given Activity.
void enableForegroundNdefPush(Activity activity, NdefMessage msg)
Enable NDEF message push over P2P while this Activity is in the foreground.
static NfcAdapter getDefaultAdapter()
This method is deprecated. use getDefaultAdapter(Context)
static NfcAdapter getDefaultAdapter(Context context)
Helper to get the default NFC Adapter.
boolean isEnabled()
Return true if this NFC Adapter has any features enabled.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String ACTION_NDEF_DISCOVERED

Since: API Level 10

Intent to start an activity when a tag with NDEF payload is discovered.

The system inspects the first NdefRecord in the first NdefMessage and looks for a URI, SmartPoster, or MIME record. If a URI or SmartPoster record is found the intent will contain the URI in its data field. If a MIME record is found the intent will contain the MIME type in its type field. This allows activities to register IntentFilters targeting specific content on tags. Activities should register the most specific intent filters possible to avoid the activity chooser dialog, which can disrupt the interaction with the tag as the user interacts with the screen.

If the tag has an NDEF payload this intent is started before ACTION_TECH_DISCOVERED. If any activities respond to this intent neither ACTION_TECH_DISCOVERED or ACTION_TAG_DISCOVERED will be started.

Constant Value: "android.nfc.action.NDEF_DISCOVERED"

public static final String ACTION_TAG_DISCOVERED

Since: API Level 9

Intent to start an activity when a tag is discovered.

This intent will not be started when a tag is discovered if any activities respond to ACTION_NDEF_DISCOVERED or ACTION_TECH_DISCOVERED for the current tag.

Constant Value: "android.nfc.action.TAG_DISCOVERED"

public static final String ACTION_TECH_DISCOVERED

Since: API Level 10

Intent to start an activity when a tag is discovered and activities are registered for the specific technologies on the tag.

To receive this intent an activity must include an intent filter for this action and specify the desired tech types in a manifest meta-data entry. Here is an example manfiest entry:

   <activity android:name=".nfc.TechFilter" android:label="NFC/TechFilter">
       <!-- Add a technology filter -->
       <intent-filter>
           <action android:name="android.nfc.action.TECH_DISCOVERED" />
       </intent-filter>

       <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
           android:resource="@xml/filter_nfc"
       />
   </activity>
 

The meta-data XML file should contain one or more tech-list entries each consisting or one or more tech entries. The tech entries refer to the qualified class name implementing the technology, for example "android.nfc.tech.NfcA".

A tag matches if any of the tech-list sets is a subset of Tag.getTechList(). Each of the tech-lists is considered independently and the activity is considered a match is any single tech-list matches the tag that was discovered. This provides AND and OR semantics for filtering desired techs. Here is an example that will match any tag using NfcF or any tag using NfcA, MifareClassic, and Ndef:

 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <!-- capture anything using NfcF -->
     <tech-list>
         <tech>android.nfc.tech.NfcF</tech>
     </tech-list>

     <!-- OR -->

     <!-- capture all MIFARE Classics with NDEF payloads -->
     <tech-list>
         <tech>android.nfc.tech.NfcA</tech>
         <tech>android.nfc.tech.MifareClassic</tech>
         <tech>android.nfc.tech.Ndef</tech>
     </tech-list>
 </resources>
 

This intent is started after ACTION_NDEF_DISCOVERED and before ACTION_TAG_DISCOVERED. If any activities respond to ACTION_NDEF_DISCOVERED this intent will not be started. If any activities respond to this intent ACTION_TAG_DISCOVERED will not be started.

Constant Value: "android.nfc.action.TECH_DISCOVERED"

public static final String EXTRA_ID

Since: API Level 9

Optional extra containing a byte array containing the ID of the discovered tag for the ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED, and ACTION_TAG_DISCOVERED intents.

Constant Value: "android.nfc.extra.ID"

public static final String EXTRA_NDEF_MESSAGES

Since: API Level 9

Optional extra containing an array of NdefMessage present on the discovered tag for the ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED, and ACTION_TAG_DISCOVERED intents.

Constant Value: "android.nfc.extra.NDEF_MESSAGES"

public static final String EXTRA_TAG

Since: API Level 10

Mandatory extra containing the Tag that was discovered for the ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED, and ACTION_TAG_DISCOVERED intents.

Constant Value: "android.nfc.extra.TAG"

Public Methods

public void disableForegroundDispatch (Activity activity)

Since: API Level 10

Disable foreground dispatch to the given activity.

After calling enableForegroundDispatch(Activity, PendingIntent, IntentFilter[], String[][]), an activity must call this method before its onPause() callback completes.

This method must be called from the main thread.

Requires the NFC permission.

Parameters
activity the Activity to disable dispatch to
Throws
IllegalStateException if the Activity has already been paused

public void disableForegroundNdefPush (Activity activity)

Since: API Level 10

Disable NDEF message push over P2P.

After calling enableForegroundNdefPush(Activity, NdefMessage), an activity must call this method before its onPause() callback completes.

This method must be called from the main thread.

Requires the NFC permission.

Parameters
activity the Foreground activity
Throws
IllegalStateException if the Activity has already been paused
if this Android device does not support NDEF push

public void enableForegroundDispatch (Activity activity, PendingIntent intent, IntentFilter[] filters, String[][] techLists)

Since: API Level 10

Enable foreground dispatch to the given Activity.

This will give give priority to the foreground activity when dispatching a discovered Tag to an application.

If any IntentFilters are provided to this method they are used to match dispatch Intents for both the ACTION_NDEF_DISCOVERED and ACTION_TAG_DISCOVERED. Since ACTION_TECH_DISCOVERED relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled by passing in the tech lists separately. Each first level entry in the tech list represents an array of technologies that must all be present to match. If any of the first level sets match then the dispatch is routed through the given PendingIntent. In other words, the second level is ANDed together and the first level entries are ORed together.

If you pass null for both the filters and techLists parameters that acts a wild card and will cause the foreground activity to receive all tags via the ACTION_TAG_DISCOVERED intent.

This method must be called from the main thread, and only when the activity is in the foreground (resumed). Also, activities must call disableForegroundDispatch(Activity) before the completion of their onPause() callback to disable foreground dispatch after it has been enabled.

Requires the NFC permission.

Parameters
activity the Activity to dispatch to
intent the PendingIntent to start for the dispatch
filters the IntentFilters to override dispatching for, or null to always dispatch
techLists the tech lists used to perform matching for dispatching of the ACTION_TECH_DISCOVERED intent
Throws
IllegalStateException if the Activity is not currently in the foreground

public void enableForegroundNdefPush (Activity activity, NdefMessage msg)

Since: API Level 10

Enable NDEF message push over P2P while this Activity is in the foreground.

For this to function properly the other NFC device being scanned must support the "com.android.npp" NDEF push protocol. Support for this protocol is currently optional for Android NFC devices.

This method must be called from the main thread.

NOTE: While foreground NDEF push is active standard tag dispatch is disabled. Only the foreground activity may receive tag discovered dispatches via enableForegroundDispatch(Activity, PendingIntent, IntentFilter[], String[][]).

Requires the NFC permission.

Parameters
activity the foreground Activity
msg a NDEF Message to push over P2P
Throws
IllegalStateException if the Activity is not currently in the foreground
if this Android device does not support NDEF push

public static NfcAdapter getDefaultAdapter ()

Since: API Level 9

This method is deprecated.
use getDefaultAdapter(Context)

Get a handle to the default NFC Adapter on this Android device.

Most Android devices will only have one NFC Adapter (NFC Controller).

Returns
  • the default NFC adapter, or null if no NFC adapter exists

public static NfcAdapter getDefaultAdapter (Context context)

Since: API Level 10

Helper to get the default NFC Adapter.

Most Android devices will only have one NFC Adapter (NFC Controller).

This helper is the equivalent of:

NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
 NfcAdapter adapter = manager.getDefaultAdapter();
 

Parameters
context the calling application's context
Returns
  • the default NFC adapter, or null if no NFC adapter exists

public boolean isEnabled ()

Since: API Level 9

Return true if this NFC Adapter has any features enabled.

Application may use this as a helper to suggest that the user should turn on NFC in Settings.

If this method returns false, the NFC hardware is guaranteed not to generate or respond to any NFC transactions.

Returns
  • true if this NFC Adapter has any features enabled