Added in [API level 10](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Constants](https://developer.android.com/reference/android/nfc/tech/Ndef#constants) \| [Methods](https://developer.android.com/reference/android/nfc/tech/Ndef#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/nfc/tech/Ndef#inhmethods)

# Ndef

*** ** * ** ***


`
public

final

class
Ndef
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


implements

https://developer.android.com/reference/android/nfc/tech/TagTechnology


`

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

<br />

*** ** * ** ***

Provides access to NDEF content and operations on a `https://developer.android.com/reference/android/nfc/Tag`.

Acquire a `https://developer.android.com/reference/android/nfc/tech/Ndef` object using `https://developer.android.com/reference/android/nfc/tech/Ndef#get(android.nfc.Tag)`.

NDEF is an NFC Forum data format. The data formats are implemented in
`https://developer.android.com/reference/android/nfc/NdefMessage` and
`https://developer.android.com/reference/android/nfc/NdefRecord`. This class provides methods to
retrieve and modify the `https://developer.android.com/reference/android/nfc/NdefMessage`
on a tag.

There are currently four NFC Forum standardized tag types that can be
formatted to contain NDEF data.

- NFC Forum Type 1 Tag (`https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_1`), such as the Innovision Topaz
- NFC Forum Type 2 Tag (`https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_2`), such as the NXP MIFARE Ultralight
- NFC Forum Type 3 Tag (`https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_3`), such as Sony Felica
- NFC Forum Type 4 Tag (`https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_4`), such as NXP MIFARE Desfire

It is mandatory for all Android devices with NFC to correctly enumerate `https://developer.android.com/reference/android/nfc/tech/Ndef` on NFC Forum Tag Types 1-4, and implement all NDEF operations as defined in this class.

Some vendors have their own well defined specifications for storing NDEF data
on tags that do not fall into the above categories. Android devices with NFC
should enumerate and implement `https://developer.android.com/reference/android/nfc/tech/Ndef` under these vendor specifications
where possible, but it is not mandatory. `https://developer.android.com/reference/android/nfc/tech/Ndef#getType()` returns a String
describing this specification, for example `https://developer.android.com/reference/android/nfc/tech/Ndef#MIFARE_CLASSIC` is
`com.nxp.ndef.mifareclassic`.

Android devices that support MIFARE Classic must also correctly
implement `https://developer.android.com/reference/android/nfc/tech/Ndef` on MIFARE Classic tags formatted to NDEF.

For guaranteed compatibility across all Android devices with NFC, it is
recommended to use NFC Forum Types 1-4 in new deployments of NFC tags
with NDEF payload. Vendor NDEF formats will not work on all Android devices.

**Note:** Methods that perform I/O operations
require the `https://developer.android.com/reference/android/Manifest.permission#NFC` permission.

## Summary

| ### Constants ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/nfc/tech/Ndef#MIFARE_CLASSIC` NDEF on MIFARE Classic |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_1` NFC Forum Tag Type 1 |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_2` NFC Forum Tag Type 2 |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_3` NFC Forum Tag Type 3 |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_4` NFC Forum Tag Type 4 |

| ### Public methods ||
|---|---|
| ` boolean` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#canMakeReadOnly()() ` Indicates whether a tag can be made read-only with `https://developer.android.com/reference/android/nfc/tech/Ndef#makeReadOnly()`. |
| ` void` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#close()() ` Disable I/O operations to the tag from this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object, and release resources. |
| ` void` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#connect()() ` Enable I/O operations to the tag from this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object. |
| ` static https://developer.android.com/reference/android/nfc/tech/Ndef` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#get(android.nfc.Tag)(https://developer.android.com/reference/android/nfc/Tag tag) ` Get an instance of `https://developer.android.com/reference/android/nfc/tech/Ndef` for the given tag. |
| ` https://developer.android.com/reference/android/nfc/NdefMessage` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#getCachedNdefMessage()() ` Get the `https://developer.android.com/reference/android/nfc/NdefMessage` that was read from the tag at discovery time. |
| ` int` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#getMaxSize()() ` Get the maximum NDEF message size in bytes. |
| ` https://developer.android.com/reference/android/nfc/NdefMessage` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#getNdefMessage()() ` Read the current `https://developer.android.com/reference/android/nfc/NdefMessage` on this tag. |
| ` https://developer.android.com/reference/android/nfc/Tag` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#getTag()() ` Get the `https://developer.android.com/reference/android/nfc/Tag` object backing this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#getType()() ` Get the NDEF tag type. |
| ` boolean` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#isConnected()() ` Helper to indicate if I/O operations should be possible. |
| ` boolean` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#isWritable()() ` Determine if the tag is writable. |
| ` boolean` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#makeReadOnly()() ` Make a tag read-only. |
| ` void` | ` https://developer.android.com/reference/android/nfc/tech/Ndef#writeNdefMessage(android.nfc.NdefMessage)(https://developer.android.com/reference/android/nfc/NdefMessage msg) ` Overwrite the `https://developer.android.com/reference/android/nfc/NdefMessage` on this tag. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` 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 https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` 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` | ` 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` | ` 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 ` https://developer.android.com/reference/android/nfc/tech/TagTechnology ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/android/nfc/tech/TagTechnology#close()() ` Disable I/O operations to the tag from this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object, and release resources. | | ` abstract void` | ` https://developer.android.com/reference/android/nfc/tech/TagTechnology#connect()() ` Enable I/O operations to the tag from this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object. | | ` abstract https://developer.android.com/reference/android/nfc/Tag` | ` https://developer.android.com/reference/android/nfc/tech/TagTechnology#getTag()() ` Get the `https://developer.android.com/reference/android/nfc/Tag` object backing this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object. | | ` abstract boolean` | ` https://developer.android.com/reference/android/nfc/tech/TagTechnology#isConnected()() ` Helper to indicate if I/O operations should be possible. | ||
| From interface ` https://developer.android.com/reference/java/io/Closeable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/io/Closeable#close()() ` Closes this stream and releases any system resources associated with it. | ||
| From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() ` Closes this resource, relinquishing any underlying resources. | ||

## Constants

### MIFARE_CLASSIC

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

```
public static final String MIFARE_CLASSIC
```

NDEF on MIFARE Classic

<br />

Constant Value:

"com.nxp.ndef.mifareclassic"


### NFC_FORUM_TYPE_1

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

```
public static final String NFC_FORUM_TYPE_1
```

NFC Forum Tag Type 1

<br />

Constant Value:

"org.nfcforum.ndef.type1"


### NFC_FORUM_TYPE_2

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

```
public static final String NFC_FORUM_TYPE_2
```

NFC Forum Tag Type 2

<br />

Constant Value:

"org.nfcforum.ndef.type2"


### NFC_FORUM_TYPE_3

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

```
public static final String NFC_FORUM_TYPE_3
```

NFC Forum Tag Type 3

<br />

Constant Value:

"org.nfcforum.ndef.type3"


### NFC_FORUM_TYPE_4

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

```
public static final String NFC_FORUM_TYPE_4
```

NFC Forum Tag Type 4

<br />

Constant Value:

"org.nfcforum.ndef.type4"


## Public methods

### canMakeReadOnly

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

```
public boolean canMakeReadOnly ()
```

Indicates whether a tag can be made read-only with `https://developer.android.com/reference/android/nfc/tech/Ndef#makeReadOnly()`.

Does not cause any RF activity and does not block.

<br />

| Returns ||
|---|---|
| `boolean` | true if it is possible to make this tag read-only <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if the tag object is reused after the tag has left the field |

### close

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

```
public void close ()
```

Disable I/O operations to the tag from this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object, and release resources.

Also causes all blocked I/O operations on other thread to be canceled and
return with `https://developer.android.com/reference/java/io/IOException`.

Requires the `https://developer.android.com/reference/android/Manifest.permission#NFC` permission.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### connect

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

```
public void connect ()
```

Enable I/O operations to the tag from this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object.

May cause RF activity and may block. Must not be called
from the main application thread. A blocked call will be canceled with
`https://developer.android.com/reference/java/io/IOException` by calling `https://developer.android.com/reference/android/nfc/tech/TagTechnology#close()` from another thread.

Only one `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object can be connected to a `https://developer.android.com/reference/android/nfc/Tag` at a time.

Applications must call `https://developer.android.com/reference/android/nfc/tech/TagTechnology#close()` when I/O operations are complete.

Requires the `https://developer.android.com/reference/android/Manifest.permission#NFC` permission.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` |   |

### get

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

```
public static Ndef get (Tag tag)
```

Get an instance of `https://developer.android.com/reference/android/nfc/tech/Ndef` for the given tag.

Returns null if `https://developer.android.com/reference/android/nfc/tech/Ndef` was not enumerated in `https://developer.android.com/reference/android/nfc/Tag#getTechList()`.
This indicates the tag is not NDEF formatted, or that this tag
is NDEF formatted but under a vendor specification that this Android
device does not implement.

Does not cause any RF activity and does not block.

<br />

| Parameters ||
|---|---|
| `tag` | `Tag`: an NDEF compatible tag <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/nfc/tech/Ndef` | Ndef object <br /> |

### getCachedNdefMessage

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

```
public NdefMessage getCachedNdefMessage ()
```

Get the `https://developer.android.com/reference/android/nfc/NdefMessage` that was read from the tag at discovery time.

If the NDEF Message is modified by an I/O operation then it
will not be updated here, this function only returns what was discovered
when the tag entered the field.

Note that this method may return null if the tag was in the
INITIALIZED state as defined by NFC Forum, as in this state the
tag is formatted to support NDEF but does not contain a message yet.

Does not cause any RF activity and does not block.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/nfc/NdefMessage` | NDEF Message read from the tag at discovery time, can be null <br /> |

### getMaxSize

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

```
public int getMaxSize ()
```

Get the maximum NDEF message size in bytes.

Does not cause any RF activity and does not block.

<br />

| Returns ||
|---|---|
| `int` | size in bytes <br /> |

### getNdefMessage

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

```
public NdefMessage getNdefMessage ()
```

Read the current `https://developer.android.com/reference/android/nfc/NdefMessage` on this tag.

This always reads the current NDEF Message stored on the tag.

Note that this method may return null if the tag was in the
INITIALIZED state as defined by NFC Forum, as in that state the
tag is formatted to support NDEF but does not contain a message yet.

This is an I/O operation and will block until complete. It must
not be called from the main application thread. A blocked call will be canceled with
`https://developer.android.com/reference/java/io/IOException` if `https://developer.android.com/reference/android/nfc/tech/Ndef#close()` is called from another thread.

Requires the `https://developer.android.com/reference/android/Manifest.permission#NFC` permission.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/nfc/NdefMessage` | the NDEF Message, can be null <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/nfc/FormatException` | if the NDEF Message on the tag is malformed |
| `https://developer.android.com/reference/android/nfc/TagLostException` | if the tag leaves the field |
| `https://developer.android.com/reference/java/io/IOException` | if there is an I/O failure, or the operation is canceled |
| `https://developer.android.com/reference/java/lang/SecurityException` | if the tag object is reused after the tag has left the field |

### getTag

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

```
public Tag getTag ()
```

Get the `https://developer.android.com/reference/android/nfc/Tag` object backing this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/nfc/Tag` | the `https://developer.android.com/reference/android/nfc/Tag` backing this `https://developer.android.com/reference/android/nfc/tech/TagTechnology` object. <br /> |

### getType

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

```
public String getType ()
```

Get the NDEF tag type.

Returns one of `https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_1`, `https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_2`,
`https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_3`, `https://developer.android.com/reference/android/nfc/tech/Ndef#NFC_FORUM_TYPE_4`,
`https://developer.android.com/reference/android/nfc/tech/Ndef#MIFARE_CLASSIC` or another NDEF tag type that has not yet been
formalized in this Android API.

Does not cause any RF activity and does not block.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | a string representing the NDEF tag type <br /> |

### isConnected

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

```
public boolean isConnected ()
```

Helper to indicate if I/O operations should be possible.

Returns true if `https://developer.android.com/reference/android/nfc/tech/TagTechnology#connect()` has completed, and `https://developer.android.com/reference/android/nfc/tech/TagTechnology#close()` has not been
called, and the `https://developer.android.com/reference/android/nfc/Tag` is not known to be out of range.

Does not cause RF activity, and does not block.

<br />

| Returns ||
|---|---|
| `boolean` | true if I/O operations should be possible <br /> |

### isWritable

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

```
public boolean isWritable ()
```

Determine if the tag is writable.

NFC Forum tags can be in read-only or read-write states.

Does not cause any RF activity and does not block.

Requires `https://developer.android.com/reference/android/Manifest.permission#NFC` permission.

<br />

| Returns ||
|---|---|
| `boolean` | true if the tag is writable <br /> |

### makeReadOnly

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

```
public boolean makeReadOnly ()
```

Make a tag read-only.

This sets the CC field to indicate the tag is read-only,
and where possible permanently sets the lock bits to prevent
any further modification of the memory.

This is a one-way process and cannot be reverted!

This is an I/O operation and will block until complete. It must
not be called from the main application thread. A blocked call will be canceled with
`https://developer.android.com/reference/java/io/IOException` if `https://developer.android.com/reference/android/nfc/tech/Ndef#close()` is called from another thread.

Requires the `https://developer.android.com/reference/android/Manifest.permission#NFC` permission.

<br />

| Returns ||
|---|---|
| `boolean` | true on success, false if it is not possible to make this tag read-only <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/nfc/TagLostException` | if the tag leaves the field |
| `https://developer.android.com/reference/java/io/IOException` | if there is an I/O failure, or the operation is canceled |
| `https://developer.android.com/reference/java/lang/SecurityException` | if the tag object is reused after the tag has left the field |

### writeNdefMessage

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

```
public void writeNdefMessage (NdefMessage msg)
```

Overwrite the `https://developer.android.com/reference/android/nfc/NdefMessage` on this tag.

This is an I/O operation and will block until complete. It must
not be called from the main application thread. A blocked call will be canceled with
`https://developer.android.com/reference/java/io/IOException` if `https://developer.android.com/reference/android/nfc/tech/Ndef#close()` is called from another thread.

Requires the `https://developer.android.com/reference/android/Manifest.permission#NFC` permission.

<br />

| Parameters ||
|---|---|
| `msg` | `NdefMessage`: the NDEF Message to write, must not be null <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/android/nfc/FormatException` | if the NDEF Message to write is malformed |
| `https://developer.android.com/reference/android/nfc/TagLostException` | if the tag leaves the field |
| `https://developer.android.com/reference/java/io/IOException` | if there is an I/O failure, or the operation is canceled |
| `https://developer.android.com/reference/java/lang/SecurityException` | if the tag object is reused after the tag has left the field |