public final class NdefFormatable extends Object
Tag
.
Acquire a NdefFormatable
object using get(android.nfc.Tag)
.
Android devices with NFC must only enumerate and implement this class for tags for which it can format to NDEF.
Unfortunately the procedures to convert unformated tags to NDEF formatted
tags are not specified by NFC Forum, and are not generally well-known. So
there is no mandatory set of tags for which all Android devices with NFC
must support NdefFormatable
.
Note: Methods that perform I/O operations
require the android.Manifest.permission#NFC
permission.
ISO_DEP, MIFARE_CLASSIC, MIFARE_ULTRALIGHT, NDEF, NDEF_FORMATABLE, NFC_A, NFC_B, NFC_BARCODE, NFC_F, NFC_V
Constructor and Description |
---|
NdefFormatable(Tag tag)
Internal constructor, to be used by NfcAdapter
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Disable I/O operations to the tag from this
TagTechnology object, and release resources. |
void |
connect()
Enable I/O operations to the tag from this
TagTechnology object. |
void |
format(NdefMessage firstMessage)
Format a tag as NDEF, and write a
NdefMessage . |
void |
formatReadOnly(NdefMessage firstMessage)
Formats a tag as NDEF, write a
NdefMessage , and make read-only. |
static NdefFormatable |
get(Tag tag)
Get an instance of
NdefFormatable for the given tag. |
Tag |
getTag()
Get the
Tag object backing this TagTechnology object. |
boolean |
isConnected()
Helper to indicate if I/O operations should be possible.
|
void |
reconnect()
Re-connect to the
Tag associated with this connection. |
public NdefFormatable(Tag tag) throws RemoteException
RemoteException
public static NdefFormatable get(Tag tag)
NdefFormatable
for the given tag.
Does not cause any RF activity and does not block.
Returns null if NdefFormatable
was not enumerated in Tag.getTechList()
.
This indicates the tag is not NDEF formatable by this Android device.
tag
- an NDEF formatable tagpublic void format(NdefMessage firstMessage) throws IOException, FormatException
NdefMessage
.
This is a multi-step process, an IOException is thrown if any one step fails.
The card is left in a read-write state after this operation.
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
IOException
if close()
is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
firstMessage
- the NDEF message to write after formatting, can be nullTagLostException
- if the tag leaves the fieldIOException
- if there is an I/O failure, or the operation is canceledFormatException
- if the NDEF Message to write is malformedpublic void formatReadOnly(NdefMessage firstMessage) throws IOException, FormatException
NdefMessage
, and make read-only.
This is a multi-step process, an IOException is thrown if any one step fails.
The card is left in a read-only state if this method returns successfully.
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
IOException
if close()
is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
firstMessage
- the NDEF message to write after formattingTagLostException
- if the tag leaves the fieldIOException
- if there is an I/O failure, or the operation is canceledFormatException
- if the NDEF Message to write is malformedpublic Tag getTag()
TagTechnology
Tag
object backing this TagTechnology
object.getTag
in interface TagTechnology
Tag
backing this TagTechnology
object.public boolean isConnected()
TagTechnology
Returns true if TagTechnology.connect()
has completed, and TagTechnology.close()
has not been
called, and the Tag
is not known to be out of range.
Does not cause RF activity, and does not block.
isConnected
in interface TagTechnology
public void connect() throws IOException
TagTechnology
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
IOException
by calling TagTechnology.close()
from another thread.
Only one TagTechnology
object can be connected to a Tag
at a time.
Applications must call TagTechnology.close()
when I/O operations are complete.
Requires the android.Manifest.permission#NFC
permission.
connect
in interface TagTechnology
IOException
- if there is an I/O failure, or connect is canceledTagTechnology.close()
public void reconnect() throws IOException
TagTechnology
Tag
associated with this connection. Reconnecting to a tag can be
used to reset the state of the tag itself.
May cause RF activity and may block. Must not be called
from the main application thread. A blocked call will be canceled with
IOException
by calling TagTechnology.close()
from another thread.
Requires the android.Manifest.permission#NFC
permission.
reconnect
in interface TagTechnology
IOException
- if there is an I/O failure, or connect is canceledTagTechnology.connect()
,
TagTechnology.close()
public void close() throws IOException
TagTechnology
TagTechnology
object, and release resources.
Also causes all blocked I/O operations on other thread to be canceled and
return with IOException
.
Requires the android.Manifest.permission#NFC
permission.
close
in interface TagTechnology
close
in interface Closeable
close
in interface AutoCloseable
IOException
TagTechnology.connect()