public final class

NdefRecord

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

Class Overview

Represents a logical (unchunked) NDEF (NFC Data Exchange Format) record.

An NDEF record always contains:

  • 3-bit TNF (Type Name Format) field: Indicates how to interpret the type field
  • Variable length type: Describes the record format
  • Variable length ID: A unique identifier for the record
  • Variable length payload: The actual data payload

The underlying record representation may be chunked across several NDEF records when the payload is large.

This is an immutable data class.

Summary

Constants
short TNF_ABSOLUTE_URI Indicates the type field contains a value that follows the absolute-URI BNF construct defined by RFC 3986.
short TNF_EMPTY Indicates no type, id, or payload is associated with this NDEF Record.
short TNF_EXTERNAL_TYPE Indicates the type field contains a value that follows the RTD external name specification.
short TNF_MIME_MEDIA Indicates the type field contains a value that follows the 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 uses the RTD type name format.
[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.
NdefRecord(byte[] data)
Construct an NDEF Record from raw bytes.
Public Methods
int describeContents()
Describe the kinds of special objects contained in this Parcelable's marshalled representation.
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.
byte[] toByteArray()
Returns this entire NDEF Record as a byte array.
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

Since: API Level 9

Indicates the type field contains a value that follows the absolute-URI BNF construct defined by RFC 3986.

Constant Value: 3 (0x00000003)

public static final short TNF_EMPTY

Since: API Level 9

Indicates no type, id, or payload is associated with this NDEF Record.

Type, id and payload fields must all be empty to be a valid TNF_EMPTY record.

Constant Value: 0 (0x00000000)

public static final short TNF_EXTERNAL_TYPE

Since: API Level 9

Indicates the type field contains a value that follows the RTD external name specification.

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

Since: API Level 9

Indicates the type field contains a value that follows the media-type BNF construct defined by RFC 2046.

Constant Value: 2 (0x00000002)

public static final short TNF_UNCHANGED

Since: API Level 9

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

The payload type is specified in the first chunk, and subsequent chunks must use TNF_UNCHANGED with an empty type field. TNF_UNCHANGED must not be used in any other situation.

Constant Value: 6 (0x00000006)

public static final short TNF_UNKNOWN

Since: API Level 9

Indicates the payload type is unknown.

This is similar to the "application/octet-stream" MIME type. The payload type is not explicitly encoded within the NDEF Message.

The type field must be empty to be a valid TNF_UNKNOWN record.

Constant Value: 5 (0x00000005)

public static final short TNF_WELL_KNOWN

Since: API Level 9

Indicates the type field uses the RTD type name format.

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

Constant Value: 1 (0x00000001)

Fields

public static final Creator<NdefRecord> CREATOR

Since: API Level 9

public static final byte[] RTD_ALTERNATIVE_CARRIER

Since: API Level 9

RTD Alternative Carrier type. For use with TNF_WELL_KNOWN.

public static final byte[] RTD_HANDOVER_CARRIER

Since: API Level 9

RTD Handover Carrier type. For use with TNF_WELL_KNOWN.

public static final byte[] RTD_HANDOVER_REQUEST

Since: API Level 9

RTD Handover Request type. For use with TNF_WELL_KNOWN.

public static final byte[] RTD_HANDOVER_SELECT

Since: API Level 9

RTD Handover Select type. For use with TNF_WELL_KNOWN.

public static final byte[] RTD_SMART_POSTER

Since: API Level 9

RTD Smart Poster type. For use with TNF_WELL_KNOWN.

public static final byte[] RTD_TEXT

Since: API Level 9

RTD Text type. For use with TNF_WELL_KNOWN.

public static final byte[] RTD_URI

Since: API Level 9

RTD URI type. For use with TNF_WELL_KNOWN.

Public Constructors

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

Since: API Level 9

Construct an NDEF Record.

Applications should not attempt to manually chunk NDEF Records - the implementation of android.nfc will automatically chunk an NDEF Record when necessary (and only present a single logical NDEF Record to the application). So applications should not use TNF_UNCHANGED.

Parameters
tnf a 3-bit TNF constant
type byte array, containing zero to 255 bytes, must not be null
id byte array, containing zero to 255 bytes, must not be null
payload byte array, containing zero to (2 ** 32 - 1) bytes, must not be null

public NdefRecord (byte[] data)

Since: API Level 9

Construct an NDEF Record from raw bytes.

Validation is performed to make sure the header is valid, and that the id, type and payload sizes appear to be valid.

Throws
FormatException if the data is not a valid NDEF record

Public Methods

public int describeContents ()

Since: 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 byte[] getId ()

Since: API Level 9

Returns the variable length ID.

public byte[] getPayload ()

Since: API Level 9

Returns the variable length payload.

public short getTnf ()

Since: API Level 9

Returns the 3-bit TNF.

TNF is the top-level type.

public byte[] getType ()

Since: API Level 9

Returns the variable length Type field.

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

public byte[] toByteArray ()

Since: API Level 9

Returns this entire NDEF Record as a byte array.

public void writeToParcel (Parcel dest, int flags)

Since: 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.