Android APIs
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.

Developer Guides

For more information about using NFC, read the Near Field Communication developer guide.

To perform basic file sharing between devices, read Sharing Files with NFC.

Summary

Nested Classes
interface NfcAdapter.CreateBeamUrisCallback  
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. 
interface NfcAdapter.ReaderCallback A callback to be invoked when the system finds a tag while the foreground activity is operating in reader mode. 
Constants
String ACTION_ADAPTER_STATE_CHANGED Broadcast Action: The state of the local NFC adapter has been changed.
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_ADAPTER_STATE Used as an int extra field in ACTION_ADAPTER_STATE_CHANGED intents to request the current power state.
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 Extra containing an array of NdefMessage present on the discovered tag.
String EXTRA_READER_PRESENCE_CHECK_DELAY Int Extra for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).
String EXTRA_TAG Mandatory extra containing the Tag that was discovered for the ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED, and ACTION_TAG_DISCOVERED intents.
int FLAG_READER_NFC_A Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).
int FLAG_READER_NFC_B Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).
int FLAG_READER_NFC_BARCODE Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).
int FLAG_READER_NFC_F Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).
int FLAG_READER_NFC_V Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).
int FLAG_READER_NO_PLATFORM_SOUNDS Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).
int FLAG_READER_SKIP_NDEF_CHECK Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).
int STATE_OFF
int STATE_ON
int STATE_TURNING_OFF
int STATE_TURNING_ON
Public Methods
void disableForegroundDispatch(Activity activity)
Disable foreground dispatch to the given activity.
void disableForegroundNdefPush(Activity activity)
This method was deprecated in API level 14. use setNdefPushMessage(NdefMessage, Activity, Activity...) instead
void disableReaderMode(Activity activity)
Restore the NFC adapter to normal mode of operation: supporting peer-to-peer (Android Beam), card emulation, and polling for all supported tag technologies.
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 was deprecated in API level 14. use setNdefPushMessage(NdefMessage, Activity, Activity...) instead
void enableReaderMode(Activity activity, NfcAdapter.ReaderCallback callback, int flags, Bundle extras)
Limit the NFC controller to reader mode while this Activity is in the foreground.
static NfcAdapter getDefaultAdapter(Context context)
Helper to get the default NFC Adapter.
boolean invokeBeam(Activity activity)
Manually invoke Android Beam to share data.
boolean isEnabled()
Return true if this NFC Adapter has any features enabled.
boolean isNdefPushEnabled()
Return true if the NDEF Push (Android Beam) feature is enabled.
void setBeamPushUris(Uri[] uris, Activity activity)
Set one or more Uris to send using Android Beam (TM).
void setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback callback, Activity activity)
Set a callback that will dynamically generate one or more Uris to send using Android Beam (TM).
void setNdefPushMessage(NdefMessage message, Activity activity, Activity... activities)
Set a static NdefMessage to send using Android Beam (TM).
void setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback callback, Activity activity, Activity... activities)
Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).
void setOnNdefPushCompleteCallback(NfcAdapter.OnNdefPushCompleteCallback callback, Activity activity, Activity... activities)
Set a callback on successful Android Beam (TM).
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String ACTION_ADAPTER_STATE_CHANGED

Added in API level 18

Broadcast Action: The state of the local NFC adapter has been changed.

For example, NFC has been turned on or off.

Always contains the extra field EXTRA_ADAPTER_STATE

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

public static final String ACTION_NDEF_DISCOVERED

Added in 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.

The MIME type or data URI of this intent are normalized before dispatch - so that MIME, URI scheme and URI host are always lower-case.

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

public static final String ACTION_TAG_DISCOVERED

Added in 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

Added in 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_ADAPTER_STATE

Added in API level 18

Used as an int extra field in ACTION_ADAPTER_STATE_CHANGED intents to request the current power state. Possible values are: STATE_OFF, STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF,

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

public static final String EXTRA_ID

Added in 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

Added in API level 9

Extra containing an array of NdefMessage present on the discovered tag.

This extra is mandatory for ACTION_NDEF_DISCOVERED intents, and optional for ACTION_TECH_DISCOVERED, and ACTION_TAG_DISCOVERED intents.

When this extra is present there will always be at least one NdefMessage element. Most NDEF tags have only one NDEF message, but we use an array for future compatibility.

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

public static final String EXTRA_READER_PRESENCE_CHECK_DELAY

Added in API level 19

Int Extra for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).

Setting this integer extra allows the calling application to specify the delay that the platform will use for performing presence checks on any discovered tag.

Constant Value: "presence"

public static final String EXTRA_TAG

Added in 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 static final int FLAG_READER_NFC_A

Added in API level 19

Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).

Setting this flag enables polling for Nfc-A technology.

Constant Value: 1 (0x00000001)

public static final int FLAG_READER_NFC_B

Added in API level 19

Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).

Setting this flag enables polling for Nfc-B technology.

Constant Value: 2 (0x00000002)

public static final int FLAG_READER_NFC_BARCODE

Added in API level 19

Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).

Setting this flag enables polling for NfcBarcode technology.

Constant Value: 16 (0x00000010)

public static final int FLAG_READER_NFC_F

Added in API level 19

Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).

Setting this flag enables polling for Nfc-F technology.

Constant Value: 4 (0x00000004)

public static final int FLAG_READER_NFC_V

Added in API level 19

Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).

Setting this flag enables polling for Nfc-V (ISO15693) technology.

Constant Value: 8 (0x00000008)

public static final int FLAG_READER_NO_PLATFORM_SOUNDS

Added in API level 19

Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).

Setting this flag allows the caller to prevent the platform from playing sounds when it discovers a tag.

Constant Value: 256 (0x00000100)

public static final int FLAG_READER_SKIP_NDEF_CHECK

Added in API level 19

Flag for use with enableReaderMode(Activity, ReaderCallback, int, Bundle).

Setting this flag allows the caller to prevent the platform from performing an NDEF check on the tags it finds.

Constant Value: 128 (0x00000080)

public static final int STATE_OFF

Added in API level 18

Constant Value: 1 (0x00000001)

public static final int STATE_ON

Added in API level 18

Constant Value: 3 (0x00000003)

public static final int STATE_TURNING_OFF

Added in API level 18

Constant Value: 4 (0x00000004)

public static final int STATE_TURNING_ON

Added in API level 18

Constant Value: 2 (0x00000002)

Public Methods

public void disableForegroundDispatch (Activity activity)

Added in 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)

Added in API level 10

This method was deprecated in API level 14.
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 disableReaderMode (Activity activity)

Added in API level 19

Restore the NFC adapter to normal mode of operation: supporting peer-to-peer (Android Beam), card emulation, and polling for all supported tag technologies.

Parameters
activity the Activity that currently has reader mode enabled

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

Added in 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)

Added in API level 10

This method was deprecated in API level 14.
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 void enableReaderMode (Activity activity, NfcAdapter.ReaderCallback callback, int flags, Bundle extras)

Added in API level 19

Limit the NFC controller to reader mode while this Activity is in the foreground.

In this mode the NFC controller will only act as an NFC tag reader/writer, thus disabling any peer-to-peer (Android Beam) and card-emulation modes of the NFC adapter on this device.

Use FLAG_READER_SKIP_NDEF_CHECK to prevent the platform from performing any NDEF checks in reader mode. Note that this will prevent the Ndef tag technology from being enumerated on the tag, and that NDEF-based tag dispatch will not be functional.

For interacting with tags that are emulated on another Android device using Android's host-based card-emulation, the recommended flags are FLAG_READER_NFC_A and FLAG_READER_SKIP_NDEF_CHECK.

Parameters
activity the Activity that requests the adapter to be in reader mode
callback the callback to be called when a tag is discovered
flags Flags indicating poll technologies and other optional parameters
extras Additional extras for configuring reader mode.

public static NfcAdapter getDefaultAdapter (Context context)

Added in 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 invokeBeam (Activity activity)

Manually invoke Android Beam to share data.

The Android Beam animation is normally only shown when two NFC-capable devices come into range. By calling this method, an Activity can invoke the Beam animation directly even if no other NFC device is in range yet. The Beam animation will then prompt the user to tap another NFC-capable device to complete the data transfer.

The main advantage of using this method is that it avoids the need for the user to tap the screen to complete the transfer, as this method already establishes the direction of the transfer and the consent of the user to share data. Callers are responsible for making sure that the user has consented to sharing data on NFC tap.

Note that to use this method, the passed in Activity must have already set data to share over Beam by using method calls such as setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...) or setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback, Activity).

Parameters
activity the current foreground Activity that has registered data to share
Returns
  • whether the Beam animation was successfully invoked

public boolean isEnabled ()

Added in API level 9

Return true if this NFC Adapter has any features enabled.

If this method returns false, the NFC hardware is guaranteed not to generate or respond to any NFC communication over its NFC radio.

Applications can use this to check if NFC is enabled. Applications can request Settings UI allowing the user to toggle NFC using:

startActivity(new Intent(Settings.ACTION_NFC_SETTINGS))

Returns
  • true if this NFC Adapter has any features enabled

public boolean isNdefPushEnabled ()

Added in API level 16

Return true if the NDEF Push (Android Beam) feature is enabled.

This function will return true only if both NFC is enabled, and the NDEF Push feature is enabled.

Note that if NFC is enabled but NDEF Push is disabled then this device can still receive NDEF messages, it just cannot send them.

Applications cannot directly toggle the NDEF Push feature, but they can request Settings UI allowing the user to toggle NDEF Push using startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS))

Example usage in an Activity that requires NDEF Push:

 protected void onResume() {
     super.onResume();
     if (!nfcAdapter.isEnabled()) {
         startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
     } else if (!nfcAdapter.isNdefPushEnabled()) {
         startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
     }
 }

Returns
  • true if NDEF Push feature is enabled

public void setBeamPushUris (Uri[] uris, Activity activity)

Added in API level 16

Set one or more Uris to send using Android Beam (TM). Every Uri you provide must have either scheme 'file' or scheme 'content'.

For the data provided through this method, Android Beam tries to switch to alternate transports such as Bluetooth to achieve a fast transfer speed. Hence this method is very suitable for transferring large files such as pictures or songs.

The receiving side will store the content of each Uri in a file and present a notification to the user to open the file with a Intent with action ACTION_VIEW. If multiple URIs are sent, the Intent will refer to the first of the stored files.

This method may be called at any time before onDestroy(), but the URI(s) are only made available for Android Beam when the specified activity(s) are in resumed (foreground) state. The recommended approach is to call this method during your Activity's onCreate(Bundle) - see sample code below. This method does not immediately perform any I/O or blocking work, so is safe to call on your main thread.

setBeamPushUris(Uri[], Activity) and setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback, Activity) have priority over both setNdefPushMessage(NdefMessage, Activity, Activity...) and setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...).

If setBeamPushUris(Uri[], Activity) is called with a null Uri array, and/or setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback, Activity) is called with a null callback, then the Uri push will be completely disabled for the specified activity(s).

Code example:

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
     if (nfcAdapter == null) return;  // NFC not available on this device
     nfcAdapter.setBeamPushUris(new Uri[] {uri1, uri2}, this);
 }
And that is it. Only one call per activity is necessary. The Android OS will automatically release its references to the Uri(s) and the Activity object when it is destroyed if you follow this pattern.

If your Activity wants to dynamically supply Uri(s), then set a callback using setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback, Activity) instead of using this method.

Do not pass in an Activity that has already been through onDestroy(). This is guaranteed if you call this API during onCreate(Bundle).

If this device does not support alternate transports such as Bluetooth or WiFI, calling this method does nothing.

Requires the NFC permission.

Parameters
uris an array of Uri(s) to push over Android Beam
activity activity for which the Uri(s) will be pushed

public void setBeamPushUrisCallback (NfcAdapter.CreateBeamUrisCallback callback, Activity activity)

Added in API level 16

Set a callback that will dynamically generate one or more Uris to send using Android Beam (TM). Every Uri the callback provides must have either scheme 'file' or scheme 'content'.

For the data provided through this callback, Android Beam tries to switch to alternate transports such as Bluetooth to achieve a fast transfer speed. Hence this method is very suitable for transferring large files such as pictures or songs.

The receiving side will store the content of each Uri in a file and present a notification to the user to open the file with a Intent with action ACTION_VIEW. If multiple URIs are sent, the Intent will refer to the first of the stored files.

This method may be called at any time before onDestroy(), but the URI(s) are only made available for Android Beam when the specified activity(s) are in resumed (foreground) state. The recommended approach is to call this method during your Activity's onCreate(Bundle) - see sample code below. This method does not immediately perform any I/O or blocking work, so is safe to call on your main thread.

setBeamPushUris(Uri[], Activity) and setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback, Activity) have priority over both setNdefPushMessage(NdefMessage, Activity, Activity...) and setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...).

If setBeamPushUris(Uri[], Activity) is called with a null Uri array, and/or setBeamPushUrisCallback(NfcAdapter.CreateBeamUrisCallback, Activity) is called with a null callback, then the Uri push will be completely disabled for the specified activity(s).

Code example:

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
     if (nfcAdapter == null) return;  // NFC not available on this device
     nfcAdapter.setBeamPushUrisCallback(callback, this);
 }
And that is it. Only one call per activity is necessary. The Android OS will automatically release its references to the Uri(s) and the Activity object when it is destroyed if you follow this pattern.

Do not pass in an Activity that has already been through onDestroy(). This is guaranteed if you call this API during onCreate(Bundle).

If this device does not support alternate transports such as Bluetooth or WiFI, calling this method does nothing.

Requires the NFC permission.

Parameters
callback callback, or null to disable
activity activity for which the Uri(s) will be pushed

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

Added in API level 14

Set a static NdefMessage to send using Android Beam (TM).

This method may be called at any time before onDestroy(), but the NDEF message is only made available for NDEF push when the specified activity(s) are in resumed (foreground) state. The recommended approach is to call this method during your Activity's onCreate(Bundle) - see sample code below. This method does not immediately perform any I/O or blocking work, so is safe to call on your main thread.

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.

If neither setNdefPushMessage(NdefMessage, Activity, Activity...) or setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...) have been called for your activity, then the Android OS may choose to send a default NDEF message on your behalf, such as a URI for your application.

If setNdefPushMessage(NdefMessage, Activity, Activity...) is called with a null NDEF message, and/or setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...) is called with a null callback, then NDEF push will be completely disabled for the specified activity(s). This also disables any default NDEF message the Android OS would have otherwise sent on your behalf for those activity(s).

If you want to prevent the Android OS from sending default NDEF messages completely (for all activities), you can include a <meta-data> element inside the <application> element of your AndroidManifest.xml file, like this:

 <application ...>
     <meta-data android:name="android.nfc.disable_beam_default"
         android:value="true" />
 </application>

The API allows for multiple activities to be specified at a time, but it is strongly recommended to just register one at a time, and to do so during the activity's onCreate(Bundle). For example:

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
     if (nfcAdapter == null) return;  // NFC not available on this device
     nfcAdapter.setNdefPushMessage(ndefMessage, this);
 }
And that is it. Only one call per activity is necessary. The Android OS will automatically release its references to the NDEF message and the Activity object when it is destroyed if you follow this pattern.

If your Activity wants to dynamically generate an NDEF message, then set a callback using setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...) instead of a static message.

Do not pass in an Activity that has already been through onDestroy(). This is guaranteed if you call this API during onCreate(Bundle).

For sending large content such as pictures and songs, consider using setBeamPushUris(Uri[], Activity), which switches to alternate transports such as Bluetooth to achieve a fast transfer rate.

Requires the NFC permission.

Parameters
message NDEF message to push over NFC, or null to disable
activity activity for which the NDEF message will be pushed
activities optional additional activities, however we strongly recommend to only register one at a time, and to do so in that activity's onCreate(Bundle)

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

Added in API level 14

Set a callback that dynamically generates NDEF messages to send using Android Beam (TM).

This method may be called at any time before onDestroy(), but the NDEF message callback can only occur when the specified activity(s) are in resumed (foreground) state. The recommended approach is to call this method during your Activity's onCreate(Bundle) - see sample code below. This method does not immediately perform any I/O or blocking work, so is safe to call on your main thread.

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.

If neither setNdefPushMessage(NdefMessage, Activity, Activity...) or setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...) have been called for your activity, then the Android OS may choose to send a default NDEF message on your behalf, such as a URI for your application.

If setNdefPushMessage(NdefMessage, Activity, Activity...) is called with a null NDEF message, and/or setNdefPushMessageCallback(NfcAdapter.CreateNdefMessageCallback, Activity, Activity...) is called with a null callback, then NDEF push will be completely disabled for the specified activity(s). This also disables any default NDEF message the Android OS would have otherwise sent on your behalf for those activity(s).

If you want to prevent the Android OS from sending default NDEF messages completely (for all activities), you can include a <meta-data> element inside the <application> element of your AndroidManifest.xml file, like this:

 <application ...>
     <meta-data android:name="android.nfc.disable_beam_default"
         android:value="true" />
 </application>

The API allows for multiple activities to be specified at a time, but it is strongly recommended to just register one at a time, and to do so during the activity's onCreate(Bundle). For example:

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
     if (nfcAdapter == null) return;  // NFC not available on this device
     nfcAdapter.setNdefPushMessageCallback(callback, this);
 }
And that is it. Only one call per activity is necessary. The Android OS will automatically release its references to the callback and the Activity object when it is destroyed if you follow this pattern.

Do not pass in an Activity that has already been through onDestroy(). This is guaranteed if you call this API during onCreate(Bundle).

For sending large content such as pictures and songs, consider using setBeamPushUris(Uri[], Activity), which switches to alternate transports such as Bluetooth to achieve a fast transfer rate.

Requires the NFC permission.

Parameters
callback callback, or null to disable
activity activity for which the NDEF message will be pushed
activities optional additional activities, however we strongly recommend to only register one at a time, and to do so in that activity's onCreate(Bundle)

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

Added in API level 14

Set a callback on successful Android Beam (TM).

This method may be called at any time before onDestroy(), but the callback can only occur when the specified activity(s) are in resumed (foreground) state. The recommended approach is to call this method during your Activity's onCreate(Bundle) - see sample code below. This method does not immediately perform any I/O or blocking work, so is safe to call on your main thread.

The API allows for multiple activities to be specified at a time, but it is strongly recommended to just register one at a time, and to do so during the activity's onCreate(Bundle). For example:

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
     if (nfcAdapter == null) return;  // NFC not available on this device
     nfcAdapter.setOnNdefPushCompleteCallback(callback, this);
 }
And that is it. Only one call per activity is necessary. The Android OS will automatically release its references to the callback and the Activity object when it is destroyed if you follow this pattern.

Do not pass in an Activity that has already been through onDestroy(). This is guaranteed if you call this API during onCreate(Bundle).

Requires the NFC permission.

Parameters
callback callback, or null to disable
activity activity for which the NDEF message will be pushed
activities optional additional activities, however we strongly recommend to only register one at a time, and to do so in that activity's onCreate(Bundle)