Ndefpublic final class Ndef extends BasicTagTechnology Provides access to NDEF content and operations on a {@link Tag}.
Acquire a {@link Ndef} object using {@link #get}.
NDEF is an NFC Forum data format. The data formats are implemented in
{@link android.nfc.NdefMessage} and
{@link android.nfc.NdefRecord}. This class provides methods to
retrieve and modify the {@link 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 ({@link #NFC_FORUM_TYPE_1}), such as the Innovision Topaz
- NFC Forum Type 2 Tag ({@link #NFC_FORUM_TYPE_2}), such as the NXP MIFARE Ultralight
- NFC Forum Type 3 Tag ({@link #NFC_FORUM_TYPE_3}), such as Sony Felica
- NFC Forum Type 4 Tag ({@link #NFC_FORUM_TYPE_4}), such as NXP MIFARE Desfire
It is mandatory for all Android devices with NFC to correctly enumerate
{@link Ndef} on NFC Forum Tag Types 1-4, and implement all NDEF operations
as defined in this class.
Some vendors have there 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 {@link Ndef} under these vendor specifications
where possible, but it is not mandatory. {@link #getType} returns a String
describing this specification, for example {@link #MIFARE_CLASSIC} is
com.nxp.ndef.mifareclassic .
Android devices that support MIFARE Classic must also correctly
implement {@link 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 {@link android.Manifest.permission#NFC} permission. |
Fields Summary |
---|
private static final String | TAG | public static final int | NDEF_MODE_READ_ONLY | public static final int | NDEF_MODE_READ_WRITE | public static final int | NDEF_MODE_UNKNOWN | public static final String | EXTRA_NDEF_MSG | public static final String | EXTRA_NDEF_MAXLENGTH | public static final String | EXTRA_NDEF_CARDSTATE | public static final String | EXTRA_NDEF_TYPE | public static final int | TYPE_OTHER | public static final int | TYPE_1 | public static final int | TYPE_2 | public static final int | TYPE_3 | public static final int | TYPE_4 | public static final int | TYPE_MIFARE_CLASSIC | public static final int | TYPE_ICODE_SLI | public static final String | UNKNOWN | public static final String | NFC_FORUM_TYPE_1NFC Forum Tag Type 1 | public static final String | NFC_FORUM_TYPE_2NFC Forum Tag Type 2 | public static final String | NFC_FORUM_TYPE_3NFC Forum Tag Type 4 | public static final String | NFC_FORUM_TYPE_4NFC Forum Tag Type 4 | public static final String | MIFARE_CLASSICNDEF on MIFARE Classic | public static final String | ICODE_SLINDEF on iCODE SLI | private final int | mMaxNdefSize | private final int | mCardState | private final android.nfc.NdefMessage | mNdefMsg | private final int | mNdefType |
Constructors Summary |
---|
public Ndef(android.nfc.Tag tag)Internal constructor, to be used by NfcAdapter
super(tag, TagTechnology.NDEF);
Bundle extras = tag.getTechExtras(TagTechnology.NDEF);
if (extras != null) {
mMaxNdefSize = extras.getInt(EXTRA_NDEF_MAXLENGTH);
mCardState = extras.getInt(EXTRA_NDEF_CARDSTATE);
mNdefMsg = extras.getParcelable(EXTRA_NDEF_MSG);
mNdefType = extras.getInt(EXTRA_NDEF_TYPE);
} else {
throw new NullPointerException("NDEF tech extras are null.");
}
|
Methods Summary |
---|
public boolean | canMakeReadOnly()Indicates whether a tag can be made read-only with {@link #makeReadOnly()}.
Does not cause any RF activity and does not block.
INfcTag tagService = mTag.getTagService();
if (tagService == null) {
return false;
}
try {
return tagService.canMakeReadOnly(mNdefType);
} catch (RemoteException e) {
Log.e(TAG, "NFC service dead", e);
return false;
}
| public static android.nfc.tech.Ndef | get(android.nfc.Tag tag)Get an instance of {@link Ndef} for the given tag.
Returns null if {@link Ndef} was not enumerated in {@link 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.
if (!tag.hasTech(TagTechnology.NDEF)) return null;
try {
return new Ndef(tag);
} catch (RemoteException e) {
return null;
}
| public android.nfc.NdefMessage | getCachedNdefMessage()Get the {@link 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.
return mNdefMsg;
| public int | getMaxSize()Get the maximum NDEF message size in bytes.
Does not cause any RF activity and does not block.
return mMaxNdefSize;
| public android.nfc.NdefMessage | getNdefMessage()Read the current {@link 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
{@link IOException} if {@link #close} is called from another thread.
Requires the {@link android.Manifest.permission#NFC} permission.
checkConnected();
try {
INfcTag tagService = mTag.getTagService();
if (tagService == null) {
throw new IOException("Mock tags don't support this operation.");
}
int serviceHandle = mTag.getServiceHandle();
if (tagService.isNdef(serviceHandle)) {
NdefMessage msg = tagService.ndefRead(serviceHandle);
if (msg == null && !tagService.isPresent(serviceHandle)) {
throw new TagLostException();
}
return msg;
} else if (!tagService.isPresent(serviceHandle)) {
throw new TagLostException();
} else {
return null;
}
} catch (RemoteException e) {
Log.e(TAG, "NFC service dead", e);
return null;
}
| public java.lang.String | getType()Get the NDEF tag type.
Returns one of {@link #NFC_FORUM_TYPE_1}, {@link #NFC_FORUM_TYPE_2},
{@link #NFC_FORUM_TYPE_3}, {@link #NFC_FORUM_TYPE_4},
{@link #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.
switch (mNdefType) {
case TYPE_1:
return NFC_FORUM_TYPE_1;
case TYPE_2:
return NFC_FORUM_TYPE_2;
case TYPE_3:
return NFC_FORUM_TYPE_3;
case TYPE_4:
return NFC_FORUM_TYPE_4;
case TYPE_MIFARE_CLASSIC:
return MIFARE_CLASSIC;
case TYPE_ICODE_SLI:
return ICODE_SLI;
default:
return UNKNOWN;
}
| 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 {@link android.Manifest.permission#NFC} permission.
return (mCardState == NDEF_MODE_READ_WRITE);
| 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
{@link IOException} if {@link #close} is called from another thread.
Requires the {@link android.Manifest.permission#NFC} permission.
checkConnected();
try {
INfcTag tagService = mTag.getTagService();
if (tagService == null) {
return false;
}
if (tagService.isNdef(mTag.getServiceHandle())) {
int errorCode = tagService.ndefMakeReadOnly(mTag.getServiceHandle());
switch (errorCode) {
case ErrorCodes.SUCCESS:
return true;
case ErrorCodes.ERROR_IO:
throw new IOException();
case ErrorCodes.ERROR_INVALID_PARAM:
return false;
default:
// Should not happen
throw new IOException();
}
}
else {
throw new IOException("Tag is not ndef");
}
} catch (RemoteException e) {
Log.e(TAG, "NFC service dead", e);
return false;
}
| public void | writeNdefMessage(android.nfc.NdefMessage msg)Overwrite the {@link 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
{@link IOException} if {@link #close} is called from another thread.
Requires the {@link android.Manifest.permission#NFC} permission.
checkConnected();
try {
INfcTag tagService = mTag.getTagService();
if (tagService == null) {
throw new IOException("Mock tags don't support this operation.");
}
int serviceHandle = mTag.getServiceHandle();
if (tagService.isNdef(serviceHandle)) {
int errorCode = tagService.ndefWrite(serviceHandle, msg);
switch (errorCode) {
case ErrorCodes.SUCCESS:
break;
case ErrorCodes.ERROR_IO:
throw new IOException();
case ErrorCodes.ERROR_INVALID_PARAM:
throw new FormatException();
default:
// Should not happen
throw new IOException();
}
}
else {
throw new IOException("Tag is not ndef");
}
} catch (RemoteException e) {
Log.e(TAG, "NFC service dead", e);
}
|
|