NdefMessage
class NdefMessage : Parcelable
| kotlin.Any | |
| ↳ | android.nfc.NdefMessage |
Represents an immutable NDEF Message.
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(android.nfc.NdefRecord[]) to construct from one or more NdefRecords.
NdefMessage and NdefRecord implementations are always available, even on Android devices that do not have NFC hardware.
NdefRecords 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.
Summary
| Inherited constants | |
|---|---|
| Public constructors | |
|---|---|
NdefMessage(record: NdefRecord!, vararg records: NdefRecord!)Construct an NDEF Message from one or more NDEF Records. |
|
NdefMessage(records: Array<NdefRecord!>!)Construct an NDEF Message from one or more NDEF Records. |
|
NdefMessage(data: ByteArray!)Construct an NDEF Message by parsing raw bytes. |
|
| Public methods | |
|---|---|
| Int |
Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. |
| Boolean |
Returns true if the specified NDEF Message contains identical NDEF Records. |
| Int |
Return the length of this NDEF Message if it is written to a byte array with |
| Array<NdefRecord!>! |
Get the NDEF Records inside this NDEF Message. |
| Int |
hashCode()Returns a hash code value for the object. |
| ByteArray! |
Return this NDEF Message as raw bytes. |
| String |
toString()Returns a string representation of the object. |
| Unit |
writeToParcel(dest: Parcel, flags: Int)Flatten this object in to a Parcel. |
| Properties | |
|---|---|
| static Parcelable.Creator<NdefMessage!> | |
Public constructors
NdefMessage
NdefMessage(
record: NdefRecord!,
vararg records: NdefRecord!)
Construct an NDEF Message from one or more NDEF Records.
| Parameters | |
|---|---|
record |
NdefRecord!: first record (mandatory) |
records |
NdefRecord!: additional records (optional) |
NdefMessage
NdefMessage(records: Array<NdefRecord!>!)
Construct an NDEF Message from one or more NDEF Records.
| Parameters | |
|---|---|
records |
Array<NdefRecord!>!: one or more records |
NdefMessage
NdefMessage(data: ByteArray!)
Construct an NDEF Message by parsing raw bytes.
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 NdefRecords 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.
| Parameters | |
|---|---|
data |
ByteArray!: raw bytes to parse |
| Exceptions | |
|---|---|
android.nfc.FormatException |
if the data cannot be parsed |
Public methods
describeContents
fun describeContents(): Int
Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(android.os.Parcel,int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.
| Return | |
|---|---|
Int |
a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or
|
equals
fun equals(other: Any?): Boolean
Returns true if the specified NDEF Message contains identical NDEF Records.
| Parameters | |
|---|---|
obj |
This value may be null. |
| Return | |
|---|---|
Boolean |
true if this object is the same as the obj argument; false otherwise. |
getByteArrayLength
fun getByteArrayLength(): Int
Return the length of this NDEF Message if it is written to a byte array with 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.
| Return | |
|---|---|
Int |
length of this NDEF Message when written to bytes with toByteArray |
See Also
getRecords
fun getRecords(): Array<NdefRecord!>!
Get the NDEF Records inside this NDEF Message.
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];
| Return | |
|---|---|
Array<NdefRecord!>! |
array of one or more NDEF records. |
hashCode
fun hashCode(): Int
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.
The general contract of hashCode is:
- Whenever it is invoked on the same object more than once during an execution of a Java application, the
hashCodemethod must consistently return the same integer, provided no information used inequalscomparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equalsmethod, then calling thehashCodemethod on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal according to the
equalsmethod, then calling thehashCodemethod 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.
| Return | |
|---|---|
Int |
a hash code value for this object. |
toByteArray
fun toByteArray(): ByteArray!
Return this NDEF Message as raw bytes.
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.
| Return | |
|---|---|
ByteArray! |
NDEF Message in binary format |
See Also
toString
fun toString(): String
Returns a string representation of the object.
| Return | |
|---|---|
String |
a string representation of the object. |
writeToParcel
fun writeToParcel(
dest: Parcel,
flags: Int
): Unit
Flatten this object in to a Parcel.
| Parameters | |
|---|---|
dest |
Parcel: The Parcel in which the object should be written. This value cannot be null. |
flags |
Int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of the following:
|