Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
public final class

Ndef

extends Object
implements TagTechnology
java.lang.Object
   ↳ android.nfc.tech.Ndef

Class Overview

Provides access to NDEF content and operations on a Tag .

Acquire a Ndef object using get(Tag) .

NDEF is an NFC Forum data format. The data formats are implemented in NdefMessage and NdefRecord . This class provides methods to retrieve and modify the NdefMessage on a tag.

There are currently four NFC Forum standardized tag types that can be formatted to contain NDEF data.

It is mandatory for all Android devices with NFC to correctly enumerate Ndef on NFC Forum Tag Types 1-4, and implement all NDEF operations as defined in this class.

Some vendors have there own well defined specifications for storing NDEF data on tags that do not fall into the above categories. Android devices with NFC should enumerate and implement Ndef under these vendor specifications where possible, but it is not mandatory. getType() returns a String describing this specification, for example MIFARE_CLASSIC is com.nxp.ndef.mifareclassic .

Android devices that support MIFARE Classic must also correctly implement Ndef on MIFARE Classic tags formatted to NDEF.

For guaranteed compatibility across all Android devices with NFC, it is recommended to use NFC Forum Types 1-4 in new deployments of NFC tags with NDEF payload. Vendor NDEF formats will not work on all Android devices.

Note: Methods that perform I/O operations require the NFC permission.

Summary

Constants
String MIFARE_CLASSIC NDEF on MIFARE Classic
String NFC_FORUM_TYPE_1 NFC Forum Tag Type 1
String NFC_FORUM_TYPE_2 NFC Forum Tag Type 2
String NFC_FORUM_TYPE_3 NFC Forum Tag Type 4
String NFC_FORUM_TYPE_4 NFC Forum Tag Type 4
Public Methods
boolean canMakeReadOnly ()
Indicates whether a tag can be made read-only with makeReadOnly() .
void close ()
Disable I/O operations to the tag from this TagTechnology object, and release resources.
void connect ()
Enable I/O operations to the tag from this TagTechnology object.
static Ndef get ( Tag tag)
Get an instance of Ndef for the given tag.
NdefMessage getCachedNdefMessage ()
Get the NdefMessage that was read from the tag at discovery time.
int getMaxSize ()
Get the maximum NDEF message size in bytes.
NdefMessage getNdefMessage ()
Read the current NdefMessage on this tag.
Tag getTag ()
Get the Tag object backing this TagTechnology object.
String getType ()
Get the NDEF tag type.
boolean isConnected ()
Helper to indicate if I/O operations should be possible.
boolean isWritable ()
Determine if the tag is writable.
boolean makeReadOnly ()
Make a tag read-only.
void writeNdefMessage ( NdefMessage msg)
Overwrite the NdefMessage on this tag.
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.nfc.tech.TagTechnology
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Constants

public static final String MIFARE_CLASSIC

NDEF on MIFARE Classic

Constant Value: "com.nxp.ndef.mifareclassic"

public static final String NFC_FORUM_TYPE_1

NFC Forum Tag Type 1

Constant Value: "org.nfcforum.ndef.type1"

public static final String NFC_FORUM_TYPE_2

NFC Forum Tag Type 2

Constant Value: "org.nfcforum.ndef.type2"

public static final String NFC_FORUM_TYPE_3

NFC Forum Tag Type 4

Constant Value: "org.nfcforum.ndef.type3"

public static final String NFC_FORUM_TYPE_4

NFC Forum Tag Type 4

Constant Value: "org.nfcforum.ndef.type4"

Public Methods

public boolean canMakeReadOnly ()

Indicates whether a tag can be made read-only with makeReadOnly() .

Does not cause any RF activity and does not block.

Returns
  • true if it is possible to make this tag read-only

public void close ()

Disable I/O operations to the tag from this TagTechnology object, and release resources.

Also causes all blocked I/O operations on other thread to be canceled and return with IOException .

Requires the NFC permission.

public void connect ()

Enable I/O operations to the tag from this TagTechnology object.

May cause RF activity and may block. Must not be called from the main application thread. A blocked call will be canceled with IOException by calling close() from another thread.

Only one TagTechnology object can be connected to a Tag at a time.

Applications must call close() when I/O operations are complete.

Requires the NFC permission.

public static Ndef get ( Tag tag)

Get an instance of Ndef for the given tag.

Returns null if Ndef was not enumerated in getTechList() . This indicates the tag is not NDEF formatted, or that this tag is NDEF formatted but under a vendor specification that this Android device does not implement.

Does not cause any RF activity and does not block.

Parameters
tag an NDEF compatible tag
Returns
  • Ndef object

public NdefMessage getCachedNdefMessage ()

Get the NdefMessage that was read from the tag at discovery time.

If the NDEF Message is modified by an I/O operation then it will not be updated here, this function only returns what was discovered when the tag entered the field.

Note that this method may return null if the tag was in the INITIALIZED state as defined by NFC Forum, as in this state the tag is formatted to support NDEF but does not contain a message yet.

Does not cause any RF activity and does not block.

Returns
  • NDEF Message read from the tag at discovery time, can be null

public int getMaxSize ()

Get the maximum NDEF message size in bytes.

Does not cause any RF activity and does not block.

Returns
  • size in bytes

public NdefMessage getNdefMessage ()

Read the current NdefMessage on this tag.

This always reads the current NDEF Message stored on the tag.

Note that this method may return null if the tag was in the INITIALIZED state as defined by NFC Forum, as in that state the tag is formatted to support NDEF but does not contain a message yet.

This is an I/O operation and will block until complete. It must not be called from the main application thread. A blocked call will be canceled with IOException if close() is called from another thread.

Requires the NFC permission.

Returns
  • the NDEF Message, can be null
Throws
TagLostException if the tag leaves the field
IOException if there is an I/O failure, or the operation is canceled
FormatException if the NDEF Message on the tag is malformed

public Tag getTag ()

Get the Tag object backing this TagTechnology object.

Returns

public String getType ()

Get the NDEF tag type.

Returns one of NFC_FORUM_TYPE_1 , NFC_FORUM_TYPE_2 , NFC_FORUM_TYPE_3 , NFC_FORUM_TYPE_4 , MIFARE_CLASSIC or another NDEF tag type that has not yet been formalized in this Android API.

Does not cause any RF activity and does not block.

Returns
  • a string representing the NDEF tag type

public boolean isConnected ()

Helper to indicate if I/O operations should be possible.

Returns true if connect() has completed, and close() has not been called, and the Tag is not known to be out of range.

Does not cause RF activity, and does not block.

Returns
  • true if I/O operations should be possible

public boolean isWritable ()

Determine if the tag is writable.

NFC Forum tags can be in read-only or read-write states.

Does not cause any RF activity and does not block.

Requires NFC permission.

Returns
  • true if the tag is writable

public boolean makeReadOnly ()

Make a tag read-only.

This sets the CC field to indicate the tag is read-only, and where possible permanently sets the lock bits to prevent any further modification of the memory.

This is a one-way process and cannot be reverted!

This is an I/O operation and will block until complete. It must not be called from the main application thread. A blocked call will be canceled with IOException if close() is called from another thread.

Requires the NFC permission.

Returns
  • true on success, false if it is not possible to make this tag read-only
Throws
TagLostException if the tag leaves the field
IOException if there is an I/O failure, or the operation is canceled

public void writeNdefMessage ( NdefMessage msg)

Overwrite the NdefMessage on this tag.

This is an I/O operation and will block until complete. It must not be called from the main application thread. A blocked call will be canceled with IOException if close() is called from another thread.

Requires the NFC permission.

Parameters
msg the NDEF Message to write, must not be null
Throws
TagLostException if the tag leaves the field
IOException if there is an I/O failure, or the operation is canceled
FormatException if the NDEF Message to write is malformed