Parse raw NDEF message bytes into records, and decode the common Well-Known record types (Text, URI).
{:nfc, :ndef, %{ndef: bytes}} delivers the raw NDEF message; run it through
parse/1. Keeping the parser in Elixir (rather than the native layer) means
one tested implementation shared across iOS and Android.
A record is %{tnf: 0..7, type: binary, id: binary, payload: binary} — the
raw NDEF fields. tnf (Type Name Format): 0 empty, 1 Well-Known (Text/URI),
2 MIME, 3 absolute URI, 4 external, 5 unknown, 6 unchanged.
Summary
Functions
Decode a Well-Known Text record (tnf: 1, type: "T") into
{:ok, %{text: binary, lang: binary}}, or :error for any other record.
Decode a Well-Known URI record (tnf: 1, type: "U") into
{:ok, uri_binary} with the abbreviation prefix expanded, or :error.
Encode records into a raw NDEF message binary — the inverse of parse/1,
ready for MobNfc.write_ndef/3. Sets MB/ME on the first/last record, uses the
Short-Record form when the payload is ≤ 255 bytes, and includes the ID field
only when non-empty. Accepts a single record or a list.
Parse an NDEF message into its records. Returns [] on empty/malformed input
(partial records already parsed are kept).
Build a Well-Known Text record (UTF-8) for encode/1 / write_ndef/3.
Build a Well-Known URI record, abbreviating a known scheme/prefix per the URI RTD table so the tag stores fewer bytes.
Types
Functions
@spec decode_text(ndef_record()) :: {:ok, %{text: binary(), lang: binary()}} | :error
Decode a Well-Known Text record (tnf: 1, type: "T") into
{:ok, %{text: binary, lang: binary}}, or :error for any other record.
@spec decode_uri(ndef_record()) :: {:ok, binary()} | :error
Decode a Well-Known URI record (tnf: 1, type: "U") into
{:ok, uri_binary} with the abbreviation prefix expanded, or :error.
@spec encode(ndef_record() | [ndef_record()]) :: binary()
Encode records into a raw NDEF message binary — the inverse of parse/1,
ready for MobNfc.write_ndef/3. Sets MB/ME on the first/last record, uses the
Short-Record form when the payload is ≤ 255 bytes, and includes the ID field
only when non-empty. Accepts a single record or a list.
MobNfc.Ndef.encode([MobNfc.Ndef.uri_record("https://mob.io")])
@spec parse(binary()) :: [ndef_record()]
Parse an NDEF message into its records. Returns [] on empty/malformed input
(partial records already parsed are kept).
@spec text_record(binary(), binary()) :: ndef_record()
Build a Well-Known Text record (UTF-8) for encode/1 / write_ndef/3.
MobNfc.Ndef.text_record("hello") # lang "en"
MobNfc.Ndef.text_record("bonjour", "fr")
@spec uri_record(binary()) :: ndef_record()
Build a Well-Known URI record, abbreviating a known scheme/prefix per the URI RTD table so the tag stores fewer bytes.
MobNfc.Ndef.uri_record("https://mob.io") # prefix code 0x04 + "mob.io"