public final class NdefMessage extends Object implements Parcelable
NDEF (NFC Data Exchange Format) is a light-weight binary format, used to encapsulate typed data. It is specified by the NFC Forum, for transmission and storage with NFC, however it is transport agnostic.
NDEF defines messages and records. An NDEF Record contains typed data, such as MIME-type media, a URI, or a custom application payload. An NDEF Message is a container for one or more NDEF Records.
When an Android device receives an NDEF Message (for example by reading an NFC tag) it processes it through a dispatch mechanism to determine an activity to launch. The type of the first record in the message has special importance for message dispatch, so design this record carefully.
Use NdefMessage(byte[])
to construct an NDEF Message from
binary data, or NdefMessage(NdefRecord[])
to
construct from one or more NdefRecord
s.
NdefMessage
and NdefRecord
implementations are
always available, even on Android devices that do not have NFC hardware.
NdefRecord
s are intended to be immutable (and thread-safe),
however they may contain mutable fields. So take care not to modify
mutable fields passed into constructors, or modify mutable fields
obtained by getter methods, unless such modification is explicitly
marked as safe.
NfcAdapter.ACTION_NDEF_DISCOVERED
,
NdefRecord
Parcelable.ClassLoaderCreator<T>, Parcelable.Creator<T>
Modifier and Type | Field and Description |
---|---|
static Parcelable.Creator<NdefMessage> |
CREATOR |
CONTENTS_FILE_DESCRIPTOR, PARCELABLE_ELIDE_DUPLICATES, PARCELABLE_WRITE_RETURN_VALUE
Constructor and Description |
---|
NdefMessage(byte[] data)
Construct an NDEF Message by parsing raw bytes.
|
NdefMessage(NdefRecord[] records)
Construct an NDEF Message from one or more NDEF Records.
|
NdefMessage(NdefRecord record,
NdefRecord... records)
Construct an NDEF Message from one or more NDEF Records.
|
Modifier and Type | Method and Description |
---|---|
int |
describeContents()
Describe the kinds of special objects contained in this Parcelable
instance's marshaled representation.
|
boolean |
equals(Object obj)
Returns true if the specified NDEF Message contains
identical NDEF Records.
|
int |
getByteArrayLength()
Return the length of this NDEF Message if it is written to a byte array
with
toByteArray() . |
NdefRecord[] |
getRecords()
Get the NDEF Records inside this NDEF Message.
|
int |
hashCode()
Returns a hash code value for the object.
|
byte[] |
toByteArray()
Return this NDEF Message as raw bytes.
|
String |
toString()
Returns a string representation of the object.
|
void |
writeToParcel(Parcel dest,
int flags)
Flatten this object in to a Parcel.
|
public static final Parcelable.Creator<NdefMessage> CREATOR
public NdefMessage(byte[] data) throws FormatException
Strict validation of the NDEF binary structure is performed: there must be at least one record, every record flag must be correct, and the total length of the message must match the length of the input data.
This parser can handle chunked records, and converts them
into logical NdefRecord
s within the message.
Once the input data has been parsed to one or more logical
records, basic validation of the tnf, type, id, and payload fields
of each record is performed, as per the documentation on
on NdefRecord.NdefRecord(short, byte[], byte[], byte[])
If either strict validation of the binary format fails, or
basic validation during record construction fails, a
FormatException
is thrown
Deep inspection of the type, id and payload fields of
each record is not performed, so it is possible to parse input
that has a valid binary format and confirms to the basic
validation requirements of
NdefRecord.NdefRecord(short, byte[], byte[], byte[])
,
but fails more strict requirements as specified by the
NFC Forum.
It is safe to re-use the data byte array after construction: this constructor will make an internal copy of all necessary fields.
data
- raw bytes to parseFormatException
- if the data cannot be parsedpublic NdefMessage(NdefRecord record, NdefRecord... records)
record
- first record (mandatory)records
- additional records (optional)public NdefMessage(NdefRecord[] records)
records
- one or more recordspublic NdefRecord[] getRecords()
An NdefMessage
always has one or more NDEF Records: so the
following code to retrieve the first record is always safe
(no need to check for null or array length >= 1):
NdefRecord firstRecord = ndefMessage.getRecords()[0];
public int getByteArrayLength()
toByteArray()
.
An NDEF Message can be formatted to bytes in different ways
depending on chunking, SR, and ID flags, so the length returned
by this method may not be equal to the length of the original
byte array used to construct this NDEF Message. However it will
always be equal to the length of the byte array produced by
toByteArray()
.
toByteArray()
toByteArray()
public byte[] toByteArray()
The NDEF Message is formatted as per the NDEF 1.0 specification, and the byte array is suitable for network transmission or storage in an NFC Forum NDEF compatible tag.
This method will not chunk any records, and will always use the short record (SR) format and omit the identifier field when possible.
getByteArrayLength
public int describeContents()
Parcelable
Parcelable.writeToParcel(Parcel, int)
,
the return value of this method must include the
Parcelable.CONTENTS_FILE_DESCRIPTOR
bit.describeContents
in interface Parcelable
Parcelable.CONTENTS_FILE_DESCRIPTOR
public void writeToParcel(Parcel dest, int flags)
Parcelable
writeToParcel
in interface Parcelable
dest
- The Parcel in which the object should be written.flags
- Additional flags about how the object should be written.
May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE
.public int hashCode()
Object
HashMap
.
The general contract of hashCode
is:
hashCode
method
must consistently return the same integer, provided no information
used in equals
comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode
method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public boolean equals(Object obj)
equals
in class Object
obj
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public String toString()
Object
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())