Class NdefRecord

java.lang.Object
com.codename1.nfc.NdefRecord

public final class NdefRecord extends Object

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 Details

    • TNF_EMPTY

      public static final byte TNF_EMPTY
      TNF (Type Name Format) -- record contains no payload.
      See Also:
    • TNF_WELL_KNOWN

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

      public static final byte TNF_MIME_MEDIA
      TNF -- type is a MIME media type (RFC 2046).
      See Also:
    • TNF_ABSOLUTE_URI

      public static final byte TNF_ABSOLUTE_URI
      TNF -- type is an absolute URI.
      See Also:
    • TNF_EXTERNAL_TYPE

      public static final byte TNF_EXTERNAL_TYPE
      TNF -- external type, namespaced as domain:type (e.g. android.com:pkg).
      See Also:
    • TNF_UNKNOWN

      public static final byte TNF_UNKNOWN
      TNF -- record is unknown / unparsed.
      See Also:
    • TNF_UNCHANGED

      public static final byte TNF_UNCHANGED
      TNF -- 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 the TNF_* constants
      • type: 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: optional record id; pass an empty array if unused
      • payload: 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: BCP-47 language tag, null defaults to "en". Must be ASCII and at most 63 bytes long
      • text: 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: 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.