Android APIs
public final class

NdefRecord

extends Object
implements Parcelable
java.lang.Object
   ↳ android.nfc.NdefRecord

Class Overview

Represents an immutable NDEF Record.

NDEF (NFC Data Exchange Format) is a light-weight binary format, used to encapsulate typed data. It is specified by the NFC Forum, for transmission and storage with NFC, however it is transport agnostic.

NDEF defines messages and records. An NDEF Record contains typed data, such as MIME-type media, a URI, or a custom application payload. An NDEF Message is a container for one or more NDEF Records.

This class represents logical (complete) NDEF Records, and can not be used to represent chunked (partial) NDEF Records. However NdefMessage(byte[]) can be used to parse a message containing chunked records, and will return a message with unchunked (complete) records.

A logical NDEF Record always contains a 3-bit TNF (Type Name Field) that provides high level typing for the rest of the record. The remaining fields are variable length and not always present:

  • type: detailed typing for the payload
  • id: identifier meta-data, not commonly used
  • payload: the actual payload

Helpers such as createUri(Uri), createMime(String, byte[]) and createExternal(String, String, byte[]) are included to create well-formatted NDEF Records with correctly set tnf, type, id and payload fields, please use these helpers whenever possible.

Use the constructor NdefRecord(short, byte[], byte[], byte[]) if you know what you are doing and what to set the fields individually. Only basic validation is performed with this constructor, so it is possible to create records that do not confirm to the strict NFC Forum specifications.

The binary representation of an NDEF Record includes additional flags to indicate location with an NDEF message, provide support for chunking of NDEF records, and to pack optional fields. This class does not expose those details. To write an NDEF Record as binary you must first put it into an NdefMessage, then call toByteArray().

NdefMessage and NdefRecord implementations are always available, even on Android devices that do not have NFC hardware.

NdefRecords are intended to be immutable (and thread-safe), however they may contain mutable fields. So take care not to modify mutable fields passed into constructors, or modify mutable fields obtained by getter methods, unless such modification is explicitly marked as safe.

Summary

Constants
short TNF_ABSOLUTE_URI Indicates the type field contains an absolute-URI BNF construct defined by RFC 3986.
short TNF_EMPTY Indicates the record is empty.
short TNF_EXTERNAL_TYPE Indicates the type field contains an external type name.
short TNF_MIME_MEDIA Indicates the type field contains a media-type BNF construct, defined by RFC 2046.
short TNF_UNCHANGED Indicates the payload is an intermediate or final chunk of a chunked NDEF Record.
short TNF_UNKNOWN Indicates the payload type is unknown.
short TNF_WELL_KNOWN Indicates the type field contains a well-known RTD type name.
[Expand]
Inherited Constants
From interface android.os.Parcelable
Fields
public static final Creator<NdefRecord> CREATOR
public static final byte[] RTD_ALTERNATIVE_CARRIER RTD Alternative Carrier type.
public static final byte[] RTD_HANDOVER_CARRIER RTD Handover Carrier type.
public static final byte[] RTD_HANDOVER_REQUEST RTD Handover Request type.
public static final byte[] RTD_HANDOVER_SELECT RTD Handover Select type.
public static final byte[] RTD_SMART_POSTER RTD Smart Poster type.
public static final byte[] RTD_TEXT RTD Text type.
public static final byte[] RTD_URI RTD URI type.
Public Constructors
NdefRecord(short tnf, byte[] type, byte[] id, byte[] payload)
Construct an NDEF Record from its component fields.
NdefRecord(byte[] data)
This constructor was deprecated in API level 16. use NdefMessage(byte[]) instead.
Public Methods
static NdefRecord createApplicationRecord(String packageName)
Create a new Android Application Record (AAR).
static NdefRecord createExternal(String domain, String type, byte[] data)
Create a new NDEF Record containing external (application-specific) data.
static NdefRecord createMime(String mimeType, byte[] mimeData)
Create a new NDEF Record containing MIME data.
static NdefRecord createUri(String uriString)
Create a new NDEF Record containing a URI.
static NdefRecord createUri(Uri uri)
Create a new NDEF Record containing a URI.
int describeContents()
Describe the kinds of special objects contained in this Parcelable's marshalled representation.
boolean equals(Object obj)
Returns true if the specified NDEF Record contains identical tnf, type, id and payload fields.
byte[] getId()
Returns the variable length ID.
byte[] getPayload()
Returns the variable length payload.
short getTnf()
Returns the 3-bit TNF.
byte[] getType()
Returns the variable length Type field.
int hashCode()
Returns an integer hash code for this object.
byte[] toByteArray()
This method was deprecated in API level 16. use toByteArray() instead
String toMimeType()
Map this record to a MIME type, or return null if it cannot be mapped.
String toString()
Returns a string containing a concise, human-readable description of this object.
Uri toUri()
Map this record to a URI, or return null if it cannot be mapped.
void writeToParcel(Parcel dest, int flags)
Flatten this object in to a Parcel.
[Expand]
Inherited Methods
From class java.lang.Object
From interface android.os.Parcelable

Constants

public static final short TNF_ABSOLUTE_URI

Added in API level 9

Indicates the type field contains an absolute-URI BNF construct defined by RFC 3986.

When creating new records prefer createUri(Uri), since it offers more compact URI encoding (#RTD_URI allows compression of common URI prefixes).

See Also
Constant Value: 3 (0x00000003)

public static final short TNF_EMPTY

Added in API level 9

Indicates the record is empty.

Type, id and payload fields are empty in a TNF_EMPTY record.

Constant Value: 0 (0x00000000)

public static final short TNF_EXTERNAL_TYPE

Added in API level 9

Indicates the type field contains an external type name.

Used to encode custom payloads. When creating new records use the helper createExternal(String, String, byte[]).

The external-type RTD format is specified in NFCForum-TS-RTD_1.0.

Note this TNF should not be used with RTD_TEXT or RTD_URI constants. Those are well known RTD constants, not external RTD constants.

Constant Value: 4 (0x00000004)

public static final short TNF_MIME_MEDIA

Added in API level 9

Indicates the type field contains a media-type BNF construct, defined by RFC 2046.

Use this with MIME type names such as "image/jpeg", or using the helper createMime(String, byte[]).

Constant Value: 2 (0x00000002)

public static final short TNF_UNCHANGED

Added in API level 9

Indicates the payload is an intermediate or final chunk of a chunked NDEF Record.

TNF_UNCHANGED can not be used with this class since all NdefRecords are already unchunked, however they may appear in the binary format.

Constant Value: 6 (0x00000006)

public static final short TNF_UNKNOWN

Added in API level 9

Indicates the payload type is unknown.

NFC Forum explains this should be treated similarly to the "application/octet-stream" MIME type. The payload type is not explicitly encoded within the record.

The type field is empty in an TNF_UNKNOWN record.

Constant Value: 5 (0x00000005)

public static final short TNF_WELL_KNOWN

Added in API level 9

Indicates the type field contains a well-known RTD type name.

Use this tnf with RTD types such as RTD_TEXT, RTD_URI.

The RTD type name format is specified in NFCForum-TS-RTD_1.0.

Constant Value: 1 (0x00000001)

Fields

public static final Creator<NdefRecord> CREATOR

Added in API level 9

public static final byte[] RTD_ALTERNATIVE_CARRIER

Added in API level 9

RTD Alternative Carrier type. For use with TNF_WELL_KNOWN.

See Also

public static final byte[] RTD_HANDOVER_CARRIER

Added in API level 9

RTD Handover Carrier type. For use with TNF_WELL_KNOWN.

See Also

public static final byte[] RTD_HANDOVER_REQUEST

Added in API level 9

RTD Handover Request type. For use with TNF_WELL_KNOWN.

See Also

public static final byte[] RTD_HANDOVER_SELECT

Added in API level 9

RTD Handover Select type. For use with TNF_WELL_KNOWN.

See Also

public static final byte[] RTD_SMART_POSTER

Added in API level 9

RTD Smart Poster type. For use with TNF_WELL_KNOWN.

See Also

public static final byte[] RTD_TEXT

Added in API level 9

RTD Text type. For use with TNF_WELL_KNOWN.

See Also

public static final byte[] RTD_URI

Added in API level 9

RTD URI type. For use with TNF_WELL_KNOWN.

See Also

Public Constructors

public NdefRecord (short tnf, byte[] type, byte[] id, byte[] payload)

Added in API level 9

Construct an NDEF Record from its component fields.

Recommend to use helpers such as {#createUri} or {createExternal(String, String, byte[]) where possible, since they perform stricter validation that the record is correctly formatted as per NDEF specifications. However if you know what you are doing then this constructor offers the most flexibility.

An NdefRecord represents a logical (complete) record, and cannot represent NDEF Record chunks.

Basic validation of the tnf, type, id and payload is performed as per the following rules:

  • The tnf paramter must be a 3-bit value.
  • Records with a tnf of TNF_EMPTY cannot have a type, id or payload.
  • Records with a tnf of TNF_UNKNOWN or 0x07 cannot have a type.
  • Records with a tnf of TNF_UNCHANGED are not allowed since this class only represents complete (unchunked) records.
This minimal validation is specified by NFCForum-TS-NDEF_1.0 section 3.2.6 (Type Name Format).

If any of the above validation steps fail then IllegalArgumentException is thrown.

Deep inspection of the type, id and payload fields is not performed, so it is possible to create NDEF Records that conform to section 3.2.6 but fail other more strict NDEF specification requirements. For example, the payload may be invalid given the tnf and type.

To omit a type, id or payload field, set the parameter to an empty byte array or null.

Parameters
tnf a 3-bit TNF constant
type byte array, containing zero to 255 bytes, or null
id byte array, containing zero to 255 bytes, or null
payload byte array, containing zero to (2 ** 32 - 1) bytes, or null
Throws
if a valid record cannot be created

public NdefRecord (byte[] data)

Added in API level 9

This constructor was deprecated in API level 16.
use NdefMessage(byte[]) instead.

Construct an NDEF Record from raw bytes.

This method is deprecated, use NdefMessage(byte[]) instead. This is because it does not make sense to parse a record: the NDEF binary format is only defined for a message, and the record flags MB and ME do not make sense outside of the context of an entire message.

This implementation will attempt to parse a single record by ignoring the MB and ME flags, and otherwise following the rules of NdefMessage(byte[]).

Parameters
data raw bytes to parse
Throws
FormatException if the data cannot be parsed into a valid record

Public Methods

public static NdefRecord createApplicationRecord (String packageName)

Added in API level 14

Create a new Android Application Record (AAR).

This record indicates to other Android devices the package that should be used to handle the entire NDEF message. You can embed this record anywhere into your message to ensure that the intended package receives the message.

When an Android device dispatches an NdefMessage containing one or more Android application records, the applications contained in those records will be the preferred target for the ACTION_NDEF_DISCOVERED intent, in the order in which they appear in the message. This dispatch behavior was first added to Android in Ice Cream Sandwich.

If none of the applications have a are installed on the device, a Market link will be opened to the first application.

Note that Android application records do not overrule applications that have called enableForegroundDispatch(Activity, PendingIntent, IntentFilter[], String[][]).

Parameters
packageName Android package name
Returns
  • Android application NDEF record

public static NdefRecord createExternal (String domain, String type, byte[] data)

Added in API level 16

Create a new NDEF Record containing external (application-specific) data.

Use this method to encode application specific data into an NDEF Record. The data is typed by a domain name (usually your Android package name) and a domain-specific type. This data is packaged into a "NFC Forum External Type" NDEF Record.

NFC Forum requires that the domain and type used in an external record are treated as case insensitive, however Android intent filtering is always case sensitive. So this method will force the domain and type to lower-case before creating the NDEF Record.

The unchecked exception IllegalArgumentException will be thrown if the domain and type have serious problems, for example if either field is empty, so always catch this exception if you are passing user-generated data into this method.

There are no such restrictions on the payload data.

For efficiency, This method might not make an internal copy of the data byte array, so take care not to modify the data byte array while still using the returned NdefRecord. Reference specification: NFCForum-TS-RTD_1.0

Parameters
domain domain-name of issuing organization
type domain-specific type of data
data payload as bytes
Throws
if either domain or type are empty or invalid

public static NdefRecord createMime (String mimeType, byte[] mimeData)

Added in API level 16

Create a new NDEF Record containing MIME data.

Use this method to encode MIME-typed data into an NDEF Record, such as "text/plain", or "image/jpeg".

The mimeType parameter will be normalized with normalizeMimeType(String) to follow Android best practices for intent filtering, for example to force lower-case. However the unchecked exception IllegalArgumentException may be thrown if the mimeType parameter has serious problems, for example if it is empty, so always catch this exception if you are passing user-generated data into this method.

For efficiency, This method might not make an internal copy of the mimeData byte array, so take care not to modify the mimeData byte array while still using the returned NdefRecord.

Parameters
mimeType a valid MIME type
mimeData MIME data as bytes
Returns
  • an NDEF Record containing the MIME-typed data
Throws
if the mimeType is empty or invalid

public static NdefRecord createUri (String uriString)

Added in API level 14

Create a new NDEF Record containing a URI.

Use this method to encode a URI (or URL) into an NDEF Record.

Uses the well known URI type representation: TNF_WELL_KNOWN and RTD_URI. This is the most efficient encoding of a URI into NDEF.

The uriString parameter will be normalized with normalizeScheme() to set the scheme to lower case to follow Android best practices for intent filtering. However the unchecked exception IllegalArgumentException may be thrown if the uriString parameter has serious problems, for example if it is empty, so always catch this exception if you are passing user-generated data into this method.

Reference specification: NFCForum-TS-RTD_URI_1.0

Parameters
uriString string URI to encode.
Returns
  • an NDEF Record containing the URI
Throws
if the uriString is empty or invalid

public static NdefRecord createUri (Uri uri)

Added in API level 14

Create a new NDEF Record containing a URI.

Use this method to encode a URI (or URL) into an NDEF Record.

Uses the well known URI type representation: TNF_WELL_KNOWN and RTD_URI. This is the most efficient encoding of a URI into NDEF.

The uri parameter will be normalized with normalizeScheme() to set the scheme to lower case to follow Android best practices for intent filtering. However the unchecked exception IllegalArgumentException may be thrown if the uri parameter has serious problems, for example if it is empty, so always catch this exception if you are passing user-generated data into this method.

Reference specification: NFCForum-TS-RTD_URI_1.0

Parameters
uri URI to encode.
Returns
  • an NDEF Record containing the URI
Throws
if the uri is empty or invalid

public int describeContents ()

Added in API level 9

Describe the kinds of special objects contained in this Parcelable's marshalled representation.

Returns
  • a bitmask indicating the set of special object types marshalled by the Parcelable.

public boolean equals (Object obj)

Added in API level 9

Returns true if the specified NDEF Record contains identical tnf, type, id and payload fields.

Parameters
obj the object to compare this instance with.
Returns
  • true if the specified object is equal to this Object; false otherwise.

public byte[] getId ()

Added in API level 9

Returns the variable length ID.

Returns an empty byte array if this record does not have an id field.

public byte[] getPayload ()

Added in API level 9

Returns the variable length payload.

Returns an empty byte array if this record does not have a payload field.

public short getTnf ()

Added in API level 9

Returns the 3-bit TNF.

TNF is the top-level type.

public byte[] getType ()

Added in API level 9

Returns the variable length Type field.

This should be used in conjunction with the TNF field to determine the payload format.

Returns an empty byte array if this record does not have a type field.

public int hashCode ()

Added in API level 9

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
  • this object's hash code.

public byte[] toByteArray ()

Added in API level 9

This method was deprecated in API level 16.
use toByteArray() instead

Return this NDEF Record as a byte array.

This method is deprecated, use toByteArray() instead. This is because the NDEF binary format is not defined for a record outside of the context of a message: the MB and ME flags cannot be set without knowing the location inside a message.

This implementation will attempt to serialize a single record by always setting the MB and ME flags (in other words, assume this is a single-record NDEF Message).

public String toMimeType ()

Added in API level 16

Map this record to a MIME type, or return null if it cannot be mapped.

Currently this method considers all TNF_MIME_MEDIA records to be MIME records, as well as some TNF_WELL_KNOWN records such as RTD_TEXT. If this is a MIME record then the MIME type as string is returned, otherwise null is returned.

This method does not perform validation that the MIME type is actually valid. It always attempts to return a string containing the type if this is a MIME record.

The returned MIME type will by normalized to lower-case using normalizeMimeType(String).

The MIME payload can be obtained using getPayload().

Returns
  • MIME type as a string, or null if this is not a MIME record

public String toString ()

Added in API level 9

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

   getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method.

Returns
  • a printable representation of this object.

public Uri toUri ()

Added in API level 16

Map this record to a URI, or return null if it cannot be mapped.

Currently this method considers the following to be URI records:

If this is not a URI record by the above rules, then null is returned.

This method does not perform validation that the URI is actually valid: it always attempts to create and return a URI if this record appears to be a URI record by the above rules.

The returned URI will be normalized to have a lower case scheme using normalizeScheme().

Returns
  • URI, or null if this is not a URI record

public void writeToParcel (Parcel dest, int flags)

Added in API level 9

Flatten this object in to a Parcel.

Parameters
dest The Parcel in which the object should be written.
flags Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE.