# Class Tag

java.lang.Object
com.codename1.nfc.Tag

public abstract class Tag extends Object

A tag discovered by `Nfc.readTag(NfcReadOptions)` or `Nfc.addTagListener(NfcListener)`. The lifetime of a `Tag` is tied to the reader session that produced it: once the tag leaves the field (or the caller closes the session) all subsequent calls on this instance fail with `NfcError.TAG_LOST`.

Apps inspect the tag via `getTypes()` to learn which technologies are available and call one of:

- `readNdef()` / `writeNdef(NdefMessage)` for any `TagType.NDEF` tag
- `getIsoDep()` for ISO-7816 smart cards / payment / passport
- `getMifareClassic()` / `getMifareUltralight()` for NXP MIFARE
- `getNfcA()` / `getNfcB()` / `getNfcF()` / `getNfcV()` for raw low-level transceive

A `Tag` may return `null` from a technology accessor when the underlying tag does not support that technology (consult `supports(TagType)` first). Ports subclass `Tag` to provide the native transceive implementation -- application code never instantiates `Tag` directly.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `Tag(Set<TagType> types, byte[] id)`
  Subclasses populate the technology list and (when known) the tag's hardware UID.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `final byte[]`
  `getId()`
  Tag's hardware identifier (`NFCA.uid` on iOS / `Tag.getId()` on Android).
  `IsoDep`
  `getIsoDep()`
  Returns an `IsoDep` view of this tag for ISO 7816 / EMV / passport APDU exchange, or `null` if the tag does not advertise `TagType.ISO_DEP`.
  `int`
  `getMaxNdefSize()`
  Largest NDEF message size (in bytes) that fits on this tag.
  `MifareClassic`
  `getMifareClassic()`
  Returns a `MifareClassic` view of this tag, or `null` when not supported.
  `MifareUltralight`
  `getMifareUltralight()`
  Returns a `MifareUltralight` view, or `null` when not supported.
  `NfcA`
  `getNfcA()`
  Raw NFC-A (ISO 14443-3A) transceive view, or `null`.
  `NfcB`
  `getNfcB()`
  Raw NFC-B (ISO 14443-3B) transceive view, or `null`.
  `NfcF`
  `getNfcF()`
  Raw FeliCa (JIS X 6319-4) transceive view, or `null`.
  `NfcV`
  `getNfcV()`
  Raw ISO 15693 / vicinity transceive view, or `null`.
  `final Set<TagType>`
  `getTypes()`
  Technologies advertised by this tag.
  `boolean`
  `hasNdef()`
  Convenience: `true` when `readNdef()` returns at least one `NdefRecord` right now.
  `boolean`
  `isWritable()`
  `true` when the tag's NDEF area is writable.
  `AsyncResource<Boolean>`
  `makeReadOnly()`
  Permanently locks the tag's NDEF area against future writes.
  `AsyncResource<NdefMessage>`
  `readNdef()`
  Reads the NDEF message currently stored on this tag.
  `final boolean`
  `supports(TagType t)`
  Convenience: `getTypes().contains(t)`.
  `AsyncResource<Boolean>`
  `writeNdef(NdefMessage message)`
  Writes (or overwrites) the NDEF message on this tag.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Tag

    protected Tag(Set<TagType> types, byte[] id)
    Subclasses populate the technology list and (when known) the tag's hardware UID. Pass an empty array for `id` if the platform does not expose one.

- ## Method Details

  - ### getTypes

    public final Set<TagType> getTypes()
    Technologies advertised by this tag. Always at least one entry on a discovered tag; an empty set indicates a tag the platform could not classify (rare).

  - ### supports

    public final boolean supports(TagType t)
    Convenience: `getTypes().contains(t)`.

  - ### getId

    public final byte[] getId()
    Tag's hardware identifier (`NFCA.uid` on iOS / `Tag.getId()` on Android). Defensively copied. Returns an empty array if the platform did not surface a UID.

  - ### readNdef

    public AsyncResource<NdefMessage> readNdef()
    Reads the NDEF message currently stored on this tag. Fails with `NfcError.UNSUPPORTED_TAG` if `supports(TagType)` of `TagType.NDEF` is `false`.

  - ### writeNdef

    public AsyncResource<Boolean> writeNdef(NdefMessage message)
    Writes (or overwrites) the NDEF message on this tag. Fails with `NfcError.READ_ONLY` if the tag has been locked, and with `NfcError.CAPACITY_EXCEEDED` when the serialised message is larger than `getMaxNdefSize()`. Default implementation reports `NfcError.UNSUPPORTED_TAG`.

  - ### makeReadOnly

    public AsyncResource<Boolean> makeReadOnly()
    Permanently locks the tag's NDEF area against future writes. Not all tags expose this operation -- on those the call fails with `NfcError.UNSUPPORTED_TAG`. Irreversible -- a locked tag cannot be re-armed.

  - ### getMaxNdefSize

    public int getMaxNdefSize()
    Largest NDEF message size (in bytes) that fits on this tag. Returns `-1` when the platform does not expose the figure (iOS Core NFC hides it on non-NDEF-formatted tags).

  - ### isWritable

    public boolean isWritable()
    `true` when the tag's NDEF area is writable. Defaults to `false` on the base class.

  - ### hasNdef

    public boolean hasNdef()
    Convenience: `true` when `readNdef()` returns at least one `NdefRecord` right now. Defaults to `supports(TagType.NDEF)`.

  - ### getIsoDep

    public IsoDep getIsoDep()
    Returns an `IsoDep` view of this tag for ISO 7816 / EMV / passport APDU exchange, or `null` if the tag does not advertise `TagType.ISO_DEP`.

  - ### getMifareClassic

    public MifareClassic getMifareClassic()
    Returns a `MifareClassic` view of this tag, or `null` when not supported. iOS always returns `null` for MIFARE Classic.

  - ### getMifareUltralight

    public MifareUltralight getMifareUltralight()
    Returns a `MifareUltralight` view, or `null` when not supported.

  - ### getNfcA

    public NfcA getNfcA()
    Raw NFC-A (ISO 14443-3A) transceive view, or `null`.

  - ### getNfcB

    public NfcB getNfcB()
    Raw NFC-B (ISO 14443-3B) transceive view, or `null`. Android-only.

  - ### getNfcF

    public NfcF getNfcF()
    Raw FeliCa (JIS X 6319-4) transceive view, or `null`.

  - ### getNfcV

    public NfcV getNfcV()
    Raw ISO 15693 / vicinity transceive view, or `null`. Android-only.