public final class NdefRecord

  1. Object
  2. NdefRecord

A single NDEF (NFC Data Exchange Format) record. An NDEF tag carries one NdefMessage which contains one or more NdefRecords.

Most apps construct records via the typed factories: createText(String, String) for human-readable text, createUri(String) for URIs (the most common payload – launches the associated app on the device), [#createMime(String, byte[])] for binary MIME payloads, and createApplicationRecord(String) for the special Android Application Record (AAR) that pins a tag to a specific package.

The low-level constructor [#NdefRecord(byte, byte[], byte[], byte[])] is available for vendor / external-type records.

Records are immutable – modify them by building a new instance.

Fields

public static final byte TNF_EMPTY = 0TNF (Type Name Format) – record contains no payload.
public static final byte TNF_WELL_KNOWN = 1TNF – type is one of the NFC Forum well-known types (e.g. T, U, Sp).
public static final byte TNF_MIME_MEDIA = 2TNF – type is a MIME media type (RFC 2046).
public static final byte TNF_ABSOLUTE_URI = 3TNF – type is an absolute URI.
public static final byte TNF_EXTERNAL_TYPE = 4TNF – external type, namespaced as domain:type (e.g. android.com:pkg).
public static final byte TNF_UNKNOWN = 5TNF – record is unknown / unparsed.
public static final byte TNF_UNCHANGED = 6TNF – continuation of a chunked record (rare).

Constructors

public NdefRecord(byte tnf, byte[] type, byte[] id, byte[] payload)Constructs a record from its raw NDEF fields.

Methods

public static byte[] rtdText()Record Type Definition (RTD) for well-known text records.
public static byte[] rtdUri()RTD for well-known URI records.
public static byte[] rtdSmartPoster()RTD for SmartPoster (URI + title).
public static byte[] rtdAndroidApp()RTD for Android Application Record (external type android.com:pkg).
public byte getTnf()One of the TNF_* constants.
public byte[] getType()Raw type field.
public byte[] getId()Raw record id.
public byte[] getPayload()Raw payload bytes.
public static NdefRecord createText(String languageCode, String text)Builds a well-known TEXT (T) record per NFC Forum RTD-Text 1.0.
public static NdefRecord createUri(String uri)Builds a well-known URI (U) record.
public static NdefRecord createMime(String mimeType, byte[] payload)Builds a MIME media record (TNF = TNF_MIME_MEDIA).
public static NdefRecord createExternal(String domain, String type, byte[] payload)Builds an external-type record (TNF = TNF_EXTERNAL_TYPE).
public static NdefRecord createApplicationRecord(String packageName)Builds an Android Application Record (AAR).
public String getTextPayload()Convenience: decodes a createText(String, String) payload back to its text content.
public String getUriPayload()Convenience: decodes a createUri(String) payload back to its full URI (re-expanding the leading prefix code).

Inherited methods

Field details

TNF_EMPTY

public static final byte TNF_EMPTY = 0
TNF (Type Name Format) – record contains no payload.

TNF_WELL_KNOWN

public static final byte TNF_WELL_KNOWN = 1
TNF – type is one of the NFC Forum well-known types (e.g. T, U, Sp). See rtdText(), rtdUri().

TNF_MIME_MEDIA

public static final byte TNF_MIME_MEDIA = 2
TNF – type is a MIME media type (RFC 2046).

TNF_ABSOLUTE_URI

public static final byte TNF_ABSOLUTE_URI = 3
TNF – type is an absolute URI.

TNF_EXTERNAL_TYPE

public static final byte TNF_EXTERNAL_TYPE = 4
TNF – external type, namespaced as domain:type (e.g. android.com:pkg).

TNF_UNKNOWN

public static final byte TNF_UNKNOWN = 5
TNF – record is unknown / unparsed.

TNF_UNCHANGED

public static final byte TNF_UNCHANGED = 6
TNF – continuation of a chunked record (rare).

Constructor details

NdefRecord

public NdefRecord(byte tnf, byte[] type, byte[] id, byte[] payload)
Constructs a record from its raw NDEF fields. Most callers should prefer one of the typed factories below.

Parameters

tnf byte
one of the TNF_* constants
type byte[]
type field; meaning depends on tnf (RTD value, MIME type string bytes, …). Must not be null – pass an empty array for TNF_EMPTY / TNF_UNKNOWN
id byte[]
optional record id; pass an empty array if unused
payload byte[]
record payload; must not be null

Method details

rtdText

public static byte[] rtdText()
Record Type Definition (RTD) for well-known text records. Returns a defensive copy so callers cannot mutate the shared constant.

rtdUri

public static byte[] rtdUri()
RTD for well-known URI records.

rtdSmartPoster

public static byte[] rtdSmartPoster()
RTD for SmartPoster (URI + title).

rtdAndroidApp

public static byte[] rtdAndroidApp()
RTD for Android Application Record (external type android.com:pkg).

getTnf

public byte getTnf()
One of the TNF_* constants.

getType

public byte[] getType()
Raw type field. Defensively copied – mutating the returned array does not affect the record.

getId

public byte[] getId()
Raw record id. Empty when no id was assigned.

getPayload

public byte[] getPayload()
Raw payload bytes. Defensively copied.

createText

public static NdefRecord createText(String languageCode, String text)
Builds a well-known TEXT (T) record per NFC Forum RTD-Text 1.0. The text is UTF-8 encoded; the BCP-47 language tag (e.g. "en", "ja") goes in the leading status byte block.

Parameters

languageCode String
BCP-47 language tag, null defaults to "en". Must be ASCII and at most 63 bytes long
text String
the text payload, UTF-8 (the spec also allows UTF-16; this factory always writes UTF-8)

createUri

public static NdefRecord createUri(String uri)
Builds a well-known URI (U) record. Common URI prefixes (http://www., https://www., tel:, mailto:, …) are replaced with the one-byte abbreviation codes defined in NFC Forum RTD-URI 1.0 to save tag space.

createMime

public static NdefRecord createMime(String mimeType, byte[] payload)
Builds a MIME media record (TNF = TNF_MIME_MEDIA). Use for binary payloads – images, small structured data, application/vnd.*.

createExternal

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

Builds an external-type record (TNF = TNF_EXTERNAL_TYPE). The domain:type string is encoded as ASCII and stored in the type field; the payload is passed through verbatim.

External types are the recommended way to ship custom data on a tag without colliding with NFC Forum well-known types.

createApplicationRecord

public static NdefRecord createApplicationRecord(String packageName)
Builds an Android Application Record (AAR). When a tag carrying an AAR is tapped on Android, the OS launches the named package instead of offering the user a chooser. Honoured only on Android – iOS ignores AARs.

Parameters

packageName String
e.g. "com.example.app"

getTextPayload

public String getTextPayload()
Convenience: decodes a createText(String, String) payload back to its text content. Returns null if the record is not a well-known text record.

getUriPayload

public String getUriPayload()
Convenience: decodes a createUri(String) payload back to its full URI (re-expanding the leading prefix code). Returns null if the record is not a recognised URI record.