ndef

package
v0.22.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MIMETypeWiFi  = "application/vnd.wfa.wsc"
	MIMETypeVCard = "text/vcard"
	MIMETypeJSON  = "application/json"
	MIMETypeText  = "text/plain"
)

Common MIME types used in NFC.

View Source
const (
	TNFEmpty       byte = 0x00 // Empty record
	TNFWellKnown   byte = 0x01 // NFC Forum well-known type
	TNFMedia       byte = 0x02 // Media-type (RFC 2046)
	TNFAbsoluteURI byte = 0x03 // Absolute URI (RFC 3986)
	TNFExternal    byte = 0x04 // NFC Forum external type
	TNFUnknown     byte = 0x05 // Unknown
	TNFUnchanged   byte = 0x06 // Unchanged (for chunked records)
	TNFReserved    byte = 0x07 // Reserved

)

TNF (Type Name Format) values as defined by NFC Forum.

View Source
const (
	TextRecordType = "T"
)

Text record constants.

View Source
const URIRecordType = "U"

URI record constants.

Variables

View Source
var (
	ErrEmptyMessage    = errors.New("ndef: empty message")
	ErrInvalidRecord   = errors.New("ndef: invalid record")
	ErrTruncatedRecord = errors.New("ndef: truncated record data")
	ErrInvalidTNF      = errors.New("ndef: invalid TNF value")
	ErrChunkedRecord   = errors.New("ndef: chunked records not supported")
)

Common errors.

View Source
var (
	ErrTextPayloadTooShort  = errors.New("ndef: text payload too short")
	ErrTextInvalidLangLen   = errors.New("ndef: invalid language code length")
	ErrTextLanguageTooLong  = errors.New("ndef: language code too long")
	ErrTextPayloadTruncated = errors.New("ndef: text payload truncated")
)

Text record errors.

View Source
var (
	ErrURIPayloadTooShort   = errors.New("ndef: URI payload too short")
	ErrURIInvalidPrefixCode = errors.New("ndef: invalid URI prefix code")
)

URI record errors.

Functions

func DecodeTextPayload

func DecodeTextPayload(payload []byte) (string, error)

DecodeTextPayload is a convenience function that extracts just the text string.

func DecodeURIPayload

func DecodeURIPayload(payload []byte) (string, error)

DecodeURIPayload is an alias for ParseURIRecord for API consistency.

func EncodeTextPayload

func EncodeTextPayload(text, language string) ([]byte, error)

EncodeTextPayload creates a text record payload from text and language.

func EncodeURIPayload

func EncodeURIPayload(uri string) []byte

EncodeURIPayload creates a URI record payload with optimal prefix compression.

func ParseURIRecord

func ParseURIRecord(payload []byte) (string, error)

ParseURIRecord extracts the full URI from a URI record payload.

func URIPrefixCode

func URIPrefixCode(prefix string) byte

URIPrefixCode returns the prefix code for a given URI prefix string. Returns 0 if no match is found.

func URIPrefixString

func URIPrefixString(code byte) string

URIPrefixString returns the prefix string for a given code. Returns empty string for invalid codes.

Types

type Message

type Message struct {
	Records []*Record
}

Message represents an NDEF message containing one or more records.

func (*Message) Marshal

func (m *Message) Marshal() ([]byte, error)

Marshal serializes the NDEF message to bytes.

func (*Message) Unmarshal

func (m *Message) Unmarshal(data []byte) (int, error)

Unmarshal parses NDEF message data and returns the number of bytes consumed.

type Record

type Record struct {
	Type    string
	ID      string
	Payload []byte
	TNF     byte
	// contains filtered or unexported fields
}

Record represents a single NDEF record.

func NewAbsoluteURIRecord

func NewAbsoluteURIRecord(uri string, payload []byte) *Record

NewAbsoluteURIRecord creates a new NDEF Absolute URI record. This is different from a URI well-known type record - the URI itself is the type.

func NewEmptyRecord

func NewEmptyRecord() *Record

NewEmptyRecord creates an empty NDEF record.

func NewExternalRecord

func NewExternalRecord(externalType string, payload []byte) *Record

NewExternalRecord creates a new NDEF External Type record. External types use the format "domain:type" (e.g., "example.com:mytype").

func NewMediaRecord

func NewMediaRecord(mediaType string, payload []byte) *Record

NewMediaRecord creates a new NDEF Media-type record. The mediaType parameter should be a MIME type (e.g., "text/plain", "application/json").

func NewTextRecord

func NewTextRecord(text, language string) *Record

NewTextRecord creates a new NDEF Text record. The language parameter should be an IANA language code (e.g., "en", "en-US").

func NewURIRecord

func NewURIRecord(uri string) *Record

NewURIRecord creates a new NDEF URI record. The URI is automatically compressed using the NFC Forum URI prefix table if a matching prefix is found.

func (*Record) MB

func (r *Record) MB() bool

MB returns true if this record is the first in a message.

func (*Record) ME

func (r *Record) ME() bool

ME returns true if this record is the last in a message.

func (*Record) Marshal

func (r *Record) Marshal() ([]byte, error)

Marshal serializes a single NDEF record to bytes.

func (*Record) SetMB

func (r *Record) SetMB(v bool)

SetMB sets the Message Begin flag.

func (*Record) SetME

func (r *Record) SetME(v bool)

SetME sets the Message End flag.

func (*Record) Unmarshal

func (r *Record) Unmarshal(data []byte) (int, error)

Unmarshal parses a single NDEF record and returns the number of bytes consumed.

type TextRecord

type TextRecord struct {
	Text     string
	Language string
	UTF16    bool // true if UTF-16 encoded (rare)
}

TextRecord represents parsed text record data.

func ParseTextRecord

func ParseTextRecord(payload []byte) (*TextRecord, error)

ParseTextRecord extracts text content from a Text record payload.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL