public final class NdefRecord
- Object
- 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 = 0 | TNF (Type Name Format) – record contains no payload. |
public static final byte TNF_WELL_KNOWN = 1 | TNF – type is one of the NFC Forum well-known types (e.g. T, U, Sp). |
public static final byte TNF_MIME_MEDIA = 2 | TNF – type is a MIME media type (RFC 2046). |
public static final byte TNF_ABSOLUTE_URI = 3 | TNF – type is an absolute URI. |
public static final byte TNF_EXTERNAL_TYPE = 4 | TNF – external type, namespaced as domain:type (e.g. android.com:pkg). |
public static final byte TNF_UNKNOWN = 5 | TNF – record is unknown / unparsed. |
public static final byte TNF_UNCHANGED = 6 | TNF – 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
Inherited methods
Field details
TNF_EMPTY
public static final byte TNF_EMPTY = 0TNF_WELL_KNOWN
public static final byte TNF_WELL_KNOWN = 1TNF_MIME_MEDIA
public static final byte TNF_MIME_MEDIA = 2TNF_ABSOLUTE_URI
public static final byte TNF_ABSOLUTE_URI = 3TNF_EXTERNAL_TYPE
public static final byte TNF_EXTERNAL_TYPE = 4domain:type (e.g.
android.com:pkg).TNF_UNKNOWN
public static final byte TNF_UNKNOWN = 5TNF_UNCHANGED
public static final byte TNF_UNCHANGED = 6Constructor details
NdefRecord
public NdefRecord(byte tnf, byte[] type, byte[] id, byte[] payload)Parameters
tnfbyte- one of the
TNF_*constants typebyte[]- type field; meaning depends on
tnf(RTD value, MIME type string bytes, …). Must not benull– pass an empty array forTNF_EMPTY/TNF_UNKNOWN idbyte[]- optional record id; pass an empty array if unused
payloadbyte[]- record payload; must not be
null
Method details
rtdText
public static byte[] rtdText()rtdUri
public static byte[] rtdUri()rtdSmartPoster
public static byte[] rtdSmartPoster()rtdAndroidApp
public static byte[] rtdAndroidApp()android.com:pkg).getTnf
public byte getTnf()TNF_* constants.getType
public byte[] getType()getId
public byte[] getId()getPayload
public byte[] getPayload()createText
public static NdefRecord createText(String languageCode, String 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
languageCodeString- BCP-47 language tag,
nulldefaults to"en". Must be ASCII and at most 63 bytes long textString- the text payload, UTF-8 (the spec also allows UTF-16; this factory always writes UTF-8)
createUri
public static NdefRecord createUri(String 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)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)Parameters
packageNameString- e.g.
"com.example.app"
getTextPayload
public String getTextPayload()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()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.