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

Nested Classes
interface NfcAdapter.CreateNdefMessageCallback A callback to be invoked when another NFC device capable of NDEF push (Android Beam) is within range. 
interface NfcAdapter.OnNdefPushCompleteCallback A callback to be invoked when the system successfully delivers your NdefMessage to another device. 
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)
This method is deprecated. use setNdefPushMessage(NdefMessage, Activity, Activity...) instead
void enableForegroundDispatch(Activity activity, PendingIntent intent, IntentFilter[] filters, String[][] techLists)
Enable foreground dispatch to the given Activity.
void enableForegroundNdefPush(Activity activity, NdefMessage message)
This method is deprecated. use setNdefPushMessage(NdefMessage, Activity, Activity...) instead
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.
void setNdefPushMessage(NdefMessage message, Activity activity, Activity... activities)
Set the NdefMessage to push over NFC during the specified activities.
void setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback callback, Activity activity, Activity... activities)
Set the callback to create a NdefMessage to push over NFC.
void setOnNdefPushCompleteCallback(NfcAdapter.OnNdefPushCompleteCallback callback, Activity activity, Activity... activities)
Set the callback on a successful NDEF push over NFC.
[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

This method is deprecated.
use setNdefPushMessage(NdefMessage, Activity, Activity...) instead

Disable NDEF message push over P2P.

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

Strongly recommend to use the new setNdefPushMessage(NdefMessage, Activity, Activity...) instead: it automatically hooks into your activity life-cycle, so you do not need to call enable/disable in your onResume/onPause.

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

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

Since: API Level 10

This method is deprecated.
use setNdefPushMessage(NdefMessage, Activity, Activity...) instead

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

You must explicitly call this method every time the activity is resumed, and you must call disableForegroundNdefPush(Activity) before your activity completes onPause().

Strongly recommend to use the new setNdefPushMessage(NdefMessage, Activity, Activity...) instead: it automatically hooks into your activity life-cycle, so you do not need to call enable/disable in your onResume/onPause.

For NDEF push to function properly the other NFC device must support either NFC Forum's SNEP (Simple Ndef Exchange Protocol), or Android's "com.android.npp" (Ndef Push Protocol). This was optional on Gingerbread level Android NFC devices, but SNEP is mandatory on Ice-Cream-Sandwich and beyond.

This method must be called from the main thread.

Requires the NFC permission.

Parameters
activity foreground activity
message a NDEF Message to push over NFC
Throws
IllegalStateException if the activity is not currently in the foreground

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

public void setNdefPushMessage (NdefMessage message, Activity activity, Activity... activities)

Since: API Level 14

Set the NdefMessage to push over NFC during the specified activities.

This method may be called at any time, but the NDEF message is only made available for NDEF push when one of the specified activities is in resumed (foreground) state.

Only one NDEF message can be pushed by the currently resumed activity. If both setNdefPushMessage(NdefMessage, Activity, Activity...) and setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...) are set then the callback will take priority.

Pass a null NDEF message to disable foreground NDEF push in the specified activities.

At least one activity must be specified, and usually only one is necessary.

Requires the NFC permission.

Parameters
message NDEF message to push over NFC, or null to disable
activity an activity in which NDEF push should be enabled to share the provided NDEF message
activities optional additional activities that should also enable NDEF push with the provided NDEF message

public void setNdefPushMessageCallback (NfcAdapter.CreateNdefMessageCallback callback, Activity activity, Activity... activities)

Since: API Level 14

Set the callback to create a NdefMessage to push over NFC.

This method may be called at any time, but this callback is only made if one of the specified activities is in resumed (foreground) state.

Only one NDEF message can be pushed by the currently resumed activity. If both setNdefPushMessage(NdefMessage, Activity, Activity...) and setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...) are set then the callback will take priority.

Pass a null callback to disable the callback in the specified activities.

At least one activity must be specified, and usually only one is necessary.

Requires the NFC permission.

Parameters
callback callback, or null to disable
activity an activity in which NDEF push should be enabled to share an NDEF message that's retrieved from the provided callback
activities optional additional activities that should also enable NDEF push using the provided callback

public void setOnNdefPushCompleteCallback (NfcAdapter.OnNdefPushCompleteCallback callback, Activity activity, Activity... activities)

Since: API Level 14

Set the callback on a successful NDEF push over NFC.

This method may be called at any time, but NDEF push and this callback can only occur when one of the specified activities is in resumed (foreground) state.

One or more activities must be specified.

Requires the NFC permission.

Parameters
callback callback, or null to disable
activity an activity to enable the callback (at least one is required)
activities zero or more additional activities to enable to callback