MifareUltralightpublic final class MifareUltralight extends BasicTagTechnology Provides access to MIFARE Ultralight properties and I/O operations on a {@link Tag}.
Acquire a {@link MifareUltralight} object using {@link #get}.
MIFARE Ultralight compatible tags have 4 byte pages {@link #PAGE_SIZE}.
The primary operations on an Ultralight tag are {@link #readPages} and
{@link #writePage}.
The original MIFARE Ultralight consists of a 64 byte EEPROM. The first
4 pages are for the OTP area, manufacturer data, and locking bits. They are
readable and some bits are writable. The final 12 pages are the user
read/write area. For more information see the NXP data sheet MF0ICU1.
The MIFARE Ultralight C consists of a 192 byte EEPROM. The first 4 pages
are for OTP, manufacturer data, and locking bits. The next 36 pages are the
user read/write area. The next 4 pages are additional locking bits, counters
and authentication configuration and are readable. The final 4 pages are for
the authentication key and are not readable. For more information see the
NXP data sheet MF0ICU2.
Implementation of this class on a Android NFC device is optional.
If it is not implemented, then
{@link MifareUltralight} will never be enumerated in {@link Tag#getTechList}.
If it is enumerated, then all {@link MifareUltralight} I/O operations will be supported.
In either case, {@link NfcA} will also be enumerated on the tag,
because all MIFARE Ultralight tags are also {@link NfcA} tags.
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 | TYPE_UNKNOWNA MIFARE Ultralight compatible tag of unknown type | public static final int | TYPE_ULTRALIGHTA MIFARE Ultralight tag | public static final int | TYPE_ULTRALIGHT_CA MIFARE Ultralight C tag | public static final int | PAGE_SIZESize of a MIFARE Ultralight page in bytes | private static final int | NXP_MANUFACTURER_ID | private static final int | MAX_PAGE_COUNT | public static final String | EXTRA_IS_UL_C | private int | mType |
Constructors Summary |
---|
public MifareUltralight(android.nfc.Tag tag)
super(tag, TagTechnology.MIFARE_ULTRALIGHT);
// Check if this could actually be a MIFARE
NfcA a = NfcA.get(tag);
mType = TYPE_UNKNOWN;
if (a.getSak() == 0x00 && tag.getId()[0] == NXP_MANUFACTURER_ID) {
Bundle extras = tag.getTechExtras(TagTechnology.MIFARE_ULTRALIGHT);
if (extras.getBoolean(EXTRA_IS_UL_C)) {
mType = TYPE_ULTRALIGHT_C;
} else {
mType = TYPE_ULTRALIGHT;
}
}
|
Methods Summary |
---|
public static android.nfc.tech.MifareUltralight | get(android.nfc.Tag tag)Get an instance of {@link MifareUltralight} for the given tag.
Returns null if {@link MifareUltralight} was not enumerated in
{@link Tag#getTechList} - this indicates the tag is not MIFARE
Ultralight compatible, or that this Android
device does not implement MIFARE Ultralight.
Does not cause any RF activity and does not block.
if (!tag.hasTech(TagTechnology.MIFARE_ULTRALIGHT)) return null;
try {
return new MifareUltralight(tag);
} catch (RemoteException e) {
return null;
}
| public int | getMaxTransceiveLength()Return the maximum number of bytes that can be sent with {@link #transceive}.
return getMaxTransceiveLengthInternal();
| public int | getTimeout()Get the current {@link #transceive} timeout in milliseconds.
Requires the {@link android.Manifest.permission#NFC} permission.
try {
return mTag.getTagService().getTimeout(TagTechnology.MIFARE_ULTRALIGHT);
} catch (RemoteException e) {
Log.e(TAG, "NFC service dead", e);
return 0;
}
| public int | getType()Return the MIFARE Ultralight type of the tag.
One of {@link #TYPE_ULTRALIGHT} or {@link #TYPE_ULTRALIGHT_C} or
{@link #TYPE_UNKNOWN}.
Depending on how the tag has been formatted, it can be impossible
to accurately classify between original MIFARE Ultralight and
Ultralight C. So treat this method as a hint.
Does not cause any RF activity and does not block.
return mType;
| public byte[] | readPages(int pageOffset)Read 4 pages (16 bytes).
The MIFARE Ultralight protocol always reads 4 pages at a time, to
reduce the number of commands required to read an entire tag.
If a read spans past the last readable block, then the tag will
return pages that have been wrapped back to the first blocks. MIFARE
Ultralight tags have readable blocks 0x00 through 0x0F. So a read to
block offset 0x0E would return blocks 0x0E, 0x0F, 0x00, 0x01. MIFARE
Ultralight C tags have readable blocks 0x00 through 0x2B. So a read to
block 0x2A would return blocks 0x2A, 0x2B, 0x00, 0x01.
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.
validatePageIndex(pageOffset);
checkConnected();
byte[] cmd = { 0x30, (byte) pageOffset};
return transceive(cmd, false);
| public void | setTimeout(int timeout)Set the {@link #transceive} timeout in milliseconds.
The timeout only applies to {@link #transceive} on this object,
and is reset to a default value when {@link #close} is called.
Setting a longer timeout may be useful when performing
transactions that require a long processing time on the tag
such as key generation.
Requires the {@link android.Manifest.permission#NFC} permission.
try {
int err = mTag.getTagService().setTimeout(
TagTechnology.MIFARE_ULTRALIGHT, timeout);
if (err != ErrorCodes.SUCCESS) {
throw new IllegalArgumentException("The supplied timeout is not valid");
}
} catch (RemoteException e) {
Log.e(TAG, "NFC service dead", e);
}
| public byte[] | transceive(byte[] data)Send raw NfcA data to a tag and receive the response.
This is equivalent to connecting to this tag via {@link NfcA}
and calling {@link NfcA#transceive}. Note that all MIFARE Classic
tags are based on {@link NfcA} technology.
Use {@link #getMaxTransceiveLength} to retrieve the maximum number of bytes
that can be sent with {@link #transceive}.
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.
return transceive(data, true);
| private static void | validatePageIndex(int pageIndex)
// Do not be too strict on upper bounds checking, since some cards
// may have more addressable memory than they report.
// Note that issuing a command to an out-of-bounds block is safe - the
// tag will wrap the read to an addressable area. This validation is a
// helper to guard against obvious programming mistakes.
if (pageIndex < 0 || pageIndex >= MAX_PAGE_COUNT) {
throw new IndexOutOfBoundsException("page out of bounds: " + pageIndex);
}
| public void | writePage(int pageOffset, byte[] data)Write 1 page (4 bytes).
The MIFARE Ultralight protocol always writes 1 page at a time, to
minimize EEPROM write cycles.
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.
validatePageIndex(pageOffset);
checkConnected();
byte[] cmd = new byte[data.length + 2];
cmd[0] = (byte) 0xA2;
cmd[1] = (byte) pageOffset;
System.arraycopy(data, 0, cmd, 2, data.length);
transceive(cmd, false);
|
|