# NdefMessage

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  
Summary:[Inherited Constants](https://developer.android.com/reference/android/nfc/NdefMessage#inhconstants)\|[Fields](https://developer.android.com/reference/android/nfc/NdefMessage#lfields)\|[Ctors](https://developer.android.com/reference/android/nfc/NdefMessage#pubctors)\|[Methods](https://developer.android.com/reference/android/nfc/NdefMessage#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/nfc/NdefMessage#inhmethods)  

# NdefMessage

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/nfc/NdefMessage "View this page in Kotlin")\|Java

`
public

final

class
NdefMessage
`  
`

extends `[Object](https://developer.android.com/reference/java/lang/Object)`


``


implements

`[Parcelable](https://developer.android.com/reference/android/os/Parcelable)`


`

|---|-------------------------|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.nfc.NdefMessage |

<br />

*** ** * ** ***

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[])](https://developer.android.com/reference/android/nfc/NdefMessage#NdefMessage(byte[]))to construct an NDEF Message from binary data, or[NdefMessage(android.nfc.NdefRecord[])](https://developer.android.com/reference/android/nfc/NdefMessage#NdefMessage(android.nfc.NdefRecord[]))to construct from one or more[NdefRecord](https://developer.android.com/reference/android/nfc/NdefRecord)s.

[NdefMessage](https://developer.android.com/reference/android/nfc/NdefMessage)and[NdefRecord](https://developer.android.com/reference/android/nfc/NdefRecord)implementations are always available, even on Android devices that do not have NFC hardware.

[NdefRecord](https://developer.android.com/reference/android/nfc/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.  
**See also:**

- [NfcAdapter.ACTION_NDEF_DISCOVERED](https://developer.android.com/reference/android/nfc/NfcAdapter#ACTION_NDEF_DISCOVERED)
- [NdefRecord](https://developer.android.com/reference/android/nfc/NdefRecord)

## Summary

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ### Inherited constants                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From interface[android.os.Parcelable](https://developer.android.com/reference/android/os/Parcelable) |-------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `int` | [CONTENTS_FILE_DESCRIPTOR](https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR) Descriptor bit used with[describeContents()](https://developer.android.com/reference/android/os/Parcelable#describeContents()): indicates that the Parcelable object's flattened representation includes a file descriptor.                                                                                                                                            | | `int` | [PARCELABLE_WRITE_RETURN_VALUE](https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE) Flag for use with[writeToParcel(Parcel, int)](https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)): the object being written is a return value, that is the result of a function such as "`Parcelable someFunction()`", "`void someFunction(out Parcelable)`", or "`void someFunction(inout Parcelable)`". | |

|                                                                                                                                       ### Fields                                                                                                                                       ||
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------|
| ` public static final `[Creator](https://developer.android.com/reference/android/os/Parcelable.Creator)`<`[NdefMessage](https://developer.android.com/reference/android/nfc/NdefMessage)`>` | [CREATOR](https://developer.android.com/reference/android/nfc/NdefMessage#CREATOR) <br /> |

|                                                                                                                                                                                      ### Public constructors                                                                                                                                                                                       ||
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[NdefMessage](https://developer.android.com/reference/android/nfc/NdefMessage#NdefMessage(android.nfc.NdefRecord,%20android.nfc.NdefRecord[]))`(`[NdefRecord](https://developer.android.com/reference/android/nfc/NdefRecord)` record, `[NdefRecord...](https://developer.android.com/reference/android/nfc/NdefRecord)` records) ` Construct an NDEF Message from one or more NDEF Records. |
| ` `[NdefMessage](https://developer.android.com/reference/android/nfc/NdefMessage#NdefMessage(android.nfc.NdefRecord[]))`(`[NdefRecord[]](https://developer.android.com/reference/android/nfc/NdefRecord)` records) ` Construct an NDEF Message from one or more NDEF Records.                                                                                                                   |
| ` `[NdefMessage](https://developer.android.com/reference/android/nfc/NdefMessage#NdefMessage(byte[]))`(byte[] data) ` Construct an NDEF Message by parsing raw bytes.                                                                                                                                                                                                                           |

|                                                                                                                                                                                ### Public methods                                                                                                                                                                                 ||
|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` int`                                                                            | ` `[describeContents](https://developer.android.com/reference/android/nfc/NdefMessage#describeContents())`() ` Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.                                                                         |
| ` boolean`                                                                        | ` `[equals](https://developer.android.com/reference/android/nfc/NdefMessage#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Returns true if the specified NDEF Message contains identical NDEF Records.                                 |
| ` int`                                                                            | ` `[getByteArrayLength](https://developer.android.com/reference/android/nfc/NdefMessage#getByteArrayLength())`() ` Return the length of this NDEF Message if it is written to a byte array with[toByteArray()](https://developer.android.com/reference/android/nfc/NdefMessage#toByteArray()). |
| ` `[NdefRecord[]](https://developer.android.com/reference/android/nfc/NdefRecord) | ` `[getRecords](https://developer.android.com/reference/android/nfc/NdefMessage#getRecords())`() ` Get the NDEF Records inside this NDEF Message.                                                                                                                                              |
| ` int`                                                                            | ` `[hashCode](https://developer.android.com/reference/android/nfc/NdefMessage#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                       |
| ` byte[]`                                                                         | ` `[toByteArray](https://developer.android.com/reference/android/nfc/NdefMessage#toByteArray())`() ` Return this NDEF Message as raw bytes.                                                                                                                                                    |
| ` `[String](https://developer.android.com/reference/java/lang/String)             | ` `[toString](https://developer.android.com/reference/android/nfc/NdefMessage#toString())`() ` Returns a string representation of the object.                                                                                                                                                  |
| ` void`                                                                           | ` `[writeToParcel](https://developer.android.com/reference/android/nfc/NdefMessage#writeToParcel(android.os.Parcel,%20int))`(`[Parcel](https://developer.android.com/reference/android/os/Parcel)` dest, int flags) ` Flatten this object in to a Parcel.                                      |

| ### Inherited methods |
|-----------------------|---|
| From class` `[java.lang.Object](https://developer.android.com/reference/java/lang/Object)` ` |--------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` `[Object](https://developer.android.com/reference/java/lang/Object)          | ` `[clone](https://developer.android.com/reference/java/lang/Object#clone())`() ` Creates and returns a copy of this object.                                                                                                                                                      | | ` boolean`                                                                     | ` `[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))`(`[Object](https://developer.android.com/reference/java/lang/Object)` obj) ` Indicates whether some other object is "equal to" this one.                                           | | ` void`                                                                        | ` `[finalize](https://developer.android.com/reference/java/lang/Object#finalize())`() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.                                                          | | ` final `[Class](https://developer.android.com/reference/java/lang/Class)`<?>` | ` `[getClass](https://developer.android.com/reference/java/lang/Object#getClass())`() ` Returns the runtime class of this`Object`.                                                                                                                                                | | ` int`                                                                         | ` `[hashCode](https://developer.android.com/reference/java/lang/Object#hashCode())`() ` Returns a hash code value for the object.                                                                                                                                                 | | ` final void`                                                                  | ` `[notify](https://developer.android.com/reference/java/lang/Object#notify())`() ` Wakes up a single thread that is waiting on this object's monitor.                                                                                                                            | | ` final void`                                                                  | ` `[notifyAll](https://developer.android.com/reference/java/lang/Object#notifyAll())`() ` Wakes up all threads that are waiting on this object's monitor.                                                                                                                         | | ` `[String](https://developer.android.com/reference/java/lang/String)          | ` `[toString](https://developer.android.com/reference/java/lang/Object#toString())`() ` Returns a string representation of the object.                                                                                                                                            | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long,%20int))`(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed. | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait(long))`(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*, or until a certain amount of real time has elapsed.                   | | ` final void`                                                                  | ` `[wait](https://developer.android.com/reference/java/lang/Object#wait())`() ` Causes the current thread to wait until it is awakened, typically by being*notified* or*interrupted*.                                                                                             | ||
| From interface` `[android.os.Parcelable](https://developer.android.com/reference/android/os/Parcelable)` ` |------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` abstract int`  | ` `[describeContents](https://developer.android.com/reference/android/os/Parcelable#describeContents())`() ` Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.                                    | | ` abstract void` | ` `[writeToParcel](https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int))`(`[Parcel](https://developer.android.com/reference/android/os/Parcel)` dest, int flags) ` Flatten this object in to a Parcel. | ||

## Fields

### CREATOR

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public static final Creator<NdefMessage> CREATOR
```

<br />

<br />

## Public constructors

### NdefMessage

Added in[API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public NdefMessage (NdefRecord record, 
                NdefRecord... records)
```

Construct an NDEF Message from one or more NDEF Records.

<br />

|                          Parameters                           ||
|-----------|----------------------------------------------------|
| `record`  | `NdefRecord`: first record (mandatory) <br />      |
| `records` | `NdefRecord`: additional records (optional) <br /> |

### NdefMessage

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public NdefMessage (NdefRecord[] records)
```

Construct an NDEF Message from one or more NDEF Records.

<br />

|                     Parameters                      ||
|-----------|------------------------------------------|
| `records` | `NdefRecord`: one or more records <br /> |

### NdefMessage

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public NdefMessage (byte[] data)
```

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[NdefRecord](https://developer.android.com/reference/android/nfc/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[])](https://developer.android.com/reference/android/nfc/NdefRecord#NdefRecord(short,%20byte[],%20byte[],%20byte[]))

If either strict validation of the binary format fails, or basic validation during record construction fails, a[FormatException](https://developer.android.com/reference/android/nfc/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[])](https://developer.android.com/reference/android/nfc/NdefRecord#NdefRecord(short,%20byte[],%20byte[],%20byte[])), 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.

<br />

|                Parameters                 ||
|--------|-----------------------------------|
| `data` | `byte`: raw bytes to parse <br /> |

|                                                        Throws                                                        ||
|----------------------------------------------------------------------------------------|------------------------------|
| [FormatException](https://developer.android.com/reference/android/nfc/FormatException) | if the data cannot be parsed |

## Public methods

### describeContents

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int describeContents ()
```

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)](https://developer.android.com/reference/android/os/Parcelable#writeToParcel(android.os.Parcel,%20int)), the return value of this method must include the[CONTENTS_FILE_DESCRIPTOR](https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR)bit.

<br />

|                                                                                                                         Returns                                                                                                                         ||
|-------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `int` | a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either`0`or[CONTENTS_FILE_DESCRIPTOR](https://developer.android.com/reference/android/os/Parcelable#CONTENTS_FILE_DESCRIPTOR) <br /> |

### equals

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public boolean equals (Object obj)
```

Returns true if the specified NDEF Message contains identical NDEF Records.

<br />

|                    Parameters                    ||
|-------|-------------------------------------------|
| `obj` | `Object`: This value may be`null`. <br /> |

|                                         Returns                                          ||
|-----------|-------------------------------------------------------------------------------|
| `boolean` | `true`if this object is the same as the obj argument;`false`otherwise. <br /> |

### getByteArrayLength

Added in[API level 16](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int getByteArrayLength ()
```

Return the length of this NDEF Message if it is written to a byte array with[toByteArray()](https://developer.android.com/reference/android/nfc/NdefMessage#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()](https://developer.android.com/reference/android/nfc/NdefMessage#toByteArray()).

<br />

|                                                                              Returns                                                                               ||
|-------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `int` | length of this NDEF Message when written to bytes with[toByteArray()](https://developer.android.com/reference/android/nfc/NdefMessage#toByteArray()) <br /> |

**See also:**

- [toByteArray()](https://developer.android.com/reference/android/nfc/NdefMessage#toByteArray())  

### getRecords

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public NdefRecord[] getRecords ()
```

Get the NDEF Records inside this NDEF Message.

An[NdefMessage](https://developer.android.com/reference/android/nfc/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];
 
```

<br />

<br />

|                                                          Returns                                                          ||
|--------------------------------------------------------------------------------|-------------------------------------------|
| [NdefRecord[]](https://developer.android.com/reference/android/nfc/NdefRecord) | array of one or more NDEF records. <br /> |

### hashCode

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public int hashCode ()
```

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by[HashMap](https://developer.android.com/reference/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`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.
- If two objects are equal according to the[equals](https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object))method, then calling the`hashCode`method 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[equals](https://developer.android.com/reference/java/lang/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.

<br />

<br />

|                     Returns                      ||
|-------|-------------------------------------------|
| `int` | a hash code value for this object. <br /> |

### toByteArray

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public byte[] toByteArray ()
```

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.

<br />

|                    Returns                     ||
|----------|--------------------------------------|
| `byte[]` | NDEF Message in binary format <br /> |

**See also:**

- [getByteArrayLength()](https://developer.android.com/reference/android/nfc/NdefMessage#getByteArrayLength())  

### toString

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public String toString ()
```

Returns a string representation of the object.

<br />

|                                                      Returns                                                      ||
|--------------------------------------------------------------------|-----------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String) | a string representation of the object. <br /> |

### writeToParcel

Added in[API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)  

```
public void writeToParcel (Parcel dest, 
                int flags)
```

Flatten this object in to a Parcel.

<br />

|                                                                                                                                                                                                                               Parameters                                                                                                                                                                                                                                ||
|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `dest`  | `Parcel`: The Parcel in which the object should be written. This value cannot be`null`. <br />                                                                                                                                                                                                                                                                                                                                                                 |
| `flags` | `int`: Additional flags about how the object should be written. May be 0 or[Parcelable.PARCELABLE_WRITE_RETURN_VALUE](https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE). Value is either`0`or a combination of[Parcelable.PARCELABLE_WRITE_RETURN_VALUE](https://developer.android.com/reference/android/os/Parcelable#PARCELABLE_WRITE_RETURN_VALUE), and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES <br /> |