Class 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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteTNF -- type is an absolute URI.static final byteTNF (Type Name Format) -- record contains no payload.static final byteTNF -- external type, namespaced asdomain:type(e.g.android.com:pkg).static final byteTNF -- type is a MIME media type (RFC 2046).static final byteTNF -- continuation of a chunked record (rare).static final byteTNF -- record is unknown / unparsed.static final byteTNF -- type is one of the NFC Forum well-known types (e.g.T,U,Sp). -
Constructor Summary
ConstructorsConstructorDescriptionNdefRecord(byte tnf, byte[] type, byte[] id, byte[] payload) Constructs a record from its raw NDEF fields. -
Method Summary
Modifier and TypeMethodDescriptionstatic NdefRecordcreateApplicationRecord(String packageName) Builds an Android Application Record (AAR).static NdefRecordcreateExternal(String domain, String type, byte[] payload) Builds an external-type record (TNF =TNF_EXTERNAL_TYPE).static NdefRecordcreateMime(String mimeType, byte[] payload) Builds a MIME media record (TNF =TNF_MIME_MEDIA).static NdefRecordcreateText(String languageCode, String text) Builds a well-known TEXT (T) record per NFC Forum RTD-Text 1.0.static NdefRecordBuilds a well-known URI (U) record.byte[]getId()Raw record id.byte[]Raw payload bytes.Convenience: decodes acreateText(String, String)payload back to its text content.bytegetTnf()One of theTNF_*constants.byte[]getType()Raw type field.Convenience: decodes acreateUri(String)payload back to its full URI (re-expanding the leading prefix code).static byte[]RTD for Android Application Record (external typeandroid.com:pkg).static byte[]RTD for SmartPoster (URI + title).static byte[]rtdText()Record Type Definition (RTD) for well-known text records.static byte[]rtdUri()RTD for well-known URI records.
-
Field Details
-
TNF_EMPTY
public static final byte TNF_EMPTYTNF (Type Name Format) -- record contains no payload.- See Also:
-
TNF_WELL_KNOWN
-
TNF_MIME_MEDIA
public static final byte TNF_MIME_MEDIATNF -- type is a MIME media type (RFC 2046).- See Also:
-
TNF_ABSOLUTE_URI
public static final byte TNF_ABSOLUTE_URITNF -- type is an absolute URI.- See Also:
-
TNF_EXTERNAL_TYPE
public static final byte TNF_EXTERNAL_TYPETNF -- external type, namespaced asdomain:type(e.g.android.com:pkg).- See Also:
-
TNF_UNKNOWN
public static final byte TNF_UNKNOWNTNF -- record is unknown / unparsed.- See Also:
-
TNF_UNCHANGED
public static final byte TNF_UNCHANGEDTNF -- continuation of a chunked record (rare).- See Also:
-
-
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: one of theTNF_*constantstype: type field; meaning depends ontnf(RTD value, MIME type string bytes, ...). Must not benull-- pass an empty array forTNF_EMPTY/TNF_UNKNOWNid: optional record id; pass an empty array if unusedpayload: record payload; must not benull
-
-
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 typeandroid.com:pkg). -
getTnf
public byte getTnf()One of theTNF_*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
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: BCP-47 language tag,nulldefaults to"en". Must be ASCII and at most 63 bytes longtext: the text payload, UTF-8 (the spec also allows UTF-16; this factory always writes UTF-8)
-
createUri
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
Builds a MIME media record (TNF =TNF_MIME_MEDIA). Use for binary payloads -- images, small structured data, application/vnd.*. -
createExternal
Builds an external-type record (TNF =
TNF_EXTERNAL_TYPE). Thedomain:typestring 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
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: e.g."com.example.app"
-
getTextPayload
Convenience: decodes acreateText(String, String)payload back to its text content. Returnsnullif the record is not a well-known text record. -
getUriPayload
Convenience: decodes acreateUri(String)payload back to its full URI (re-expanding the leading prefix code). Returnsnullif the record is not a recognised URI record.
-