MifareClassic
public
final
class
MifareClassic
extends Object
implements
TagTechnology
| java.lang.Object | |
| ↳ | android.nfc.tech.MifareClassic |
Provides access to MIFARE Classic properties and I/O operations on a Tag.
Acquire a MifareClassic object using get(Tag).
MIFARE Classic is also known as MIFARE Standard.
MIFARE Classic tags are divided into sectors, and each sector is sub-divided into
blocks. Block size is always 16 bytes (BLOCK_SIZE. Sector size varies.
- MIFARE Classic Mini are 320 bytes (
SIZE_MINI), with 5 sectors each of 4 blocks. - MIFARE Classic 1k are 1024 bytes (
SIZE_1K), with 16 sectors each of 4 blocks. - MIFARE Classic 2k are 2048 bytes (
SIZE_2K), with 32 sectors each of 4 blocks. - MIFARE Classic 4k are 4096 bytes (
SIZE_4K). The first 32 sectors contain 4 blocks and the last 8 sectors contain 16 blocks.
MIFARE Classic tags require authentication on a per-sector basis before any other I/O operations on that sector can be performed. There are two keys per sector, and ACL bits determine what I/O operations are allowed on that sector after authenticating with a key. and .
Three well-known authentication keys are defined in this class:
KEY_DEFAULT, KEY_MIFARE_APPLICATION_DIRECTORY,
KEY_NFC_FORUM.
KEY_DEFAULTis the default factory key for MIFARE Classic.KEY_MIFARE_APPLICATION_DIRECTORYis the well-known key for MIFARE Classic cards that have been formatted according to the MIFARE Application Directory (MAD) specification.KEY_NFC_FORUMis the well-known key for MIFARE Classic cards that have been formatted according to the NXP specification for NDEF on MIFARE Classic.Implementation of this class on a Android NFC device is optional. If it is not implemented, then
MifareClassicwill never be enumerated inTag.getTechList. If it is enumerated, then allMifareClassicI/O operations will be supported, andNdef.MIFARE_CLASSICNDEF tags will also be supported. In either case,NfcAwill also be enumerated on the tag, because all MIFARE Classic tags are alsoNfcA.Note: Methods that perform I/O operations require the
Manifest.permission.NFCpermission.Summary
Constants
intBLOCK_SIZESize of a MIFARE Classic block (in bytes)
intSIZE_1KTag contains 16 sectors, each with 4 blocks.
intSIZE_2KTag contains 32 sectors, each with 4 blocks.
intSIZE_4KTag contains 40 sectors.
intSIZE_MINITag contains 5 sectors, each with 4 blocks.
intTYPE_CLASSICA MIFARE Classic tag
intTYPE_PLUSA MIFARE Plus tag
intTYPE_PROA MIFARE Pro tag
intTYPE_UNKNOWNA MIFARE Classic compatible card of unknown type
Fields
public static final byte[]KEY_DEFAULTThe default factory key.
public static final byte[]KEY_MIFARE_APPLICATION_DIRECTORYThe well-known key for tags formatted according to the MIFARE Application Directory (MAD) specification.
public static final byte[]KEY_NFC_FORUMThe well-known key for tags formatted according to the NDEF on MIFARE Classic specification.
Public methods
booleanauthenticateSectorWithKeyA(int sectorIndex, byte[] key)Authenticate a sector with key A.
booleanauthenticateSectorWithKeyB(int sectorIndex, byte[] key)Authenticate a sector with key B.
intblockToSector(int blockIndex)Return the sector that contains a given block.
voidclose()Disable I/O operations to the tag from this
TagTechnologyobject, and release resources.voidconnect()Enable I/O operations to the tag from this
TagTechnologyobject.voiddecrement(int blockIndex, int value)Decrement a value block, storing the result in the temporary block on the tag.
static MifareClassicget(Tag tag)Get an instance of
MifareClassicfor the given tag.intgetBlockCount()Return the total number of MIFARE Classic blocks.
intgetBlockCountInSector(int sectorIndex)Return the number of blocks in the given sector.
intgetMaxTransceiveLength()Return the maximum number of bytes that can be sent with
transceive(byte).intgetSectorCount()Return the number of MIFARE Classic sectors.
intgetSize()Return the size of the tag in bytes
TaggetTag()Get the
Tagobject backing thisTagTechnologyobject.intgetTimeout()Get the current
transceive(byte)timeout in milliseconds.intgetType()Return the type of this MIFARE Classic compatible tag.
voidincrement(int blockIndex, int value)Increment a value block, storing the result in the temporary block on the tag.
booleanisConnected()Helper to indicate if I/O operations should be possible.
byte[]readBlock(int blockIndex)Read 16-byte block.
voidrestore(int blockIndex)Copy from a value block to the temporary block.
intsectorToBlock(int sectorIndex)Return the first block of a given sector.
voidsetTimeout(int timeout)Set the
transceive(byte)timeout in milliseconds.byte[]transceive(byte[] data)Send raw NfcA data to a tag and receive the response.
voidtransfer(int blockIndex)Copy from the temporary block to a value block.
voidwriteBlock(int blockIndex, byte[] data)Write 16-byte block.
Inherited methods
Constants
BLOCK_SIZE
Added in API level 10public static final int BLOCK_SIZE
Size of a MIFARE Classic block (in bytes)
Constant Value: 16 (0x00000010)
SIZE_1K
Added in API level 10public static final int SIZE_1K
Tag contains 16 sectors, each with 4 blocks.
Constant Value: 1024 (0x00000400)
SIZE_2K
Added in API level 10public static final int SIZE_2K
Tag contains 32 sectors, each with 4 blocks.
Constant Value: 2048 (0x00000800)
SIZE_4K
Added in API level 10public static final int SIZE_4K
Tag contains 40 sectors. The first 32 sectors contain 4 blocks and the last 8 sectors contain 16 blocks.
Constant Value: 4096 (0x00001000)
SIZE_MINI
Added in API level 10public static final int SIZE_MINI
Tag contains 5 sectors, each with 4 blocks.
Constant Value: 320 (0x00000140)
TYPE_CLASSIC
Added in API level 10public static final int TYPE_CLASSIC
A MIFARE Classic tag
Constant Value: 0 (0x00000000)
TYPE_PLUS
Added in API level 10public static final int TYPE_PLUS
A MIFARE Plus tag
Constant Value: 1 (0x00000001)
TYPE_PRO
Added in API level 10public static final int TYPE_PRO
A MIFARE Pro tag
Constant Value: 2 (0x00000002)
TYPE_UNKNOWN
Added in API level 10public static final int TYPE_UNKNOWN
A MIFARE Classic compatible card of unknown type
Constant Value: -1 (0xffffffff)
Fields
KEY_MIFARE_APPLICATION_DIRECTORY
Added in API level 10public static final byte[] KEY_MIFARE_APPLICATION_DIRECTORY
The well-known key for tags formatted according to the MIFARE Application Directory (MAD) specification.
KEY_NFC_FORUM
Added in API level 10public static final byte[] KEY_NFC_FORUM
The well-known key for tags formatted according to the NDEF on MIFARE Classic specification.
Public methods
authenticateSectorWithKeyA
Added in API level 10public boolean authenticateSectorWithKeyA (int sectorIndex, byte[] key)Authenticate a sector with key A.
Successful authentication of a sector with key A enables other I/O operations on that sector. The set of operations granted by key A key depends on the ACL bits set in that sector. For more information see the MIFARE Classic specification on http://www.nxp.com.
A failed authentication attempt causes an implicit reconnection to the tag, so authentication to other sectors will be lost.
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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters sectorIndexint: index of sector to authenticate, starting from 0keybyte: 6-byte authentication keyReturns booleantrue on success, false on authentication failure Throws TagLostExceptionif the tag leaves the field IOExceptionif there is an I/O failure, or the operation is canceled authenticateSectorWithKeyB
Added in API level 10public boolean authenticateSectorWithKeyB (int sectorIndex, byte[] key)Authenticate a sector with key B.
Successful authentication of a sector with key B enables other I/O operations on that sector. The set of operations granted by key B depends on the ACL bits set in that sector. For more information see the MIFARE Classic specification on http://www.nxp.com.
A failed authentication attempt causes an implicit reconnection to the tag, so authentication to other sectors will be lost.
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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters sectorIndexint: index of sector to authenticate, starting from 0keybyte: 6-byte authentication keyReturns booleantrue on success, false on authentication failure Throws TagLostExceptionif the tag leaves the field IOExceptionif there is an I/O failure, or the operation is canceled blockToSector
Added in API level 10public int blockToSector (int blockIndex)
Return the sector that contains a given block.
Does not cause any RF activity and does not block.
Parameters blockIndexint: index of block to lookup, starting from 0Returns intsector index that contains the block close
Added in API level 10public void close ()
Disable I/O operations to the tag from this
TagTechnologyobject, and release resources.Also causes all blocked I/O operations on other thread to be canceled and return with
IOException.Requires the
Manifest.permission.NFCpermission.Throws IOExceptionconnect
Added in API level 10public void connect ()
Enable I/O operations to the tag from this
TagTechnologyobject.May cause RF activity and may block. Must not be called from the main application thread. A blocked call will be canceled with
IOExceptionby callingclose()from another thread.Only one
TagTechnologyobject can be connected to aTagat a time.Applications must call
close()when I/O operations are complete.Requires the
Manifest.permission.NFCpermission.Throws IOExceptiondecrement
Added in API level 10public void decrement (int blockIndex, int value)Decrement a value block, storing the result in the temporary block on the 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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters blockIndexint: index of block to decrement, starting from 0valueint: non-negative to decrement byThrows TagLostExceptionif the tag leaves the field IOExceptionif there is an I/O failure, or the operation is canceled get
Added in API level 10public static MifareClassic get (Tag tag)
Get an instance of
MifareClassicfor the given tag.Does not cause any RF activity and does not block.
Returns null if
MifareClassicwas not enumerated inTag.getTechList. This indicates the tag is not MIFARE Classic compatible, or this Android device does not support MIFARE Classic.Parameters tagTag: an MIFARE Classic compatible tagReturns MifareClassicMIFARE Classic object getBlockCount
Added in API level 10public int getBlockCount ()
Return the total number of MIFARE Classic blocks.
Does not cause any RF activity and does not block.
Returns inttotal number of blocks getBlockCountInSector
Added in API level 10public int getBlockCountInSector (int sectorIndex)
Return the number of blocks in the given sector.
Does not cause any RF activity and does not block.
Parameters sectorIndexint: index of sector, starting from 0Returns intnumber of blocks in the sector getMaxTransceiveLength
Added in API level 14public int getMaxTransceiveLength ()
Return the maximum number of bytes that can be sent with
transceive(byte).Returns intthe maximum number of bytes that can be sent with transceive(byte).getSectorCount
Added in API level 10public int getSectorCount ()
Return the number of MIFARE Classic sectors.
Does not cause any RF activity and does not block.
Returns intnumber of sectors getSize
Added in API level 10public int getSize ()
Return the size of the tag in bytes
One of
SIZE_MINI,SIZE_1K,SIZE_2K,SIZE_4K. These constants are equal to their respective size in bytes.Does not cause any RF activity and does not block.
Returns intsize in bytes getTag
Added in API level 10public Tag getTag ()
Get the
Tagobject backing thisTagTechnologyobject.Returns Tagthe Tagbacking thisTagTechnologyobject.getTimeout
Added in API level 14public int getTimeout ()
Get the current
transceive(byte)timeout in milliseconds.Requires the
Manifest.permission.NFCpermission.Returns inttimeout value in milliseconds Throws SecurityExceptionif the tag object is reused after the tag has left the field getType
Added in API level 10public int getType ()
Return the type of this MIFARE Classic compatible tag.
One of
TYPE_UNKNOWN,TYPE_CLASSIC,TYPE_PLUSorTYPE_PRO.Does not cause any RF activity and does not block.
Returns inttype increment
Added in API level 10public void increment (int blockIndex, int value)Increment a value block, storing the result in the temporary block on the 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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters blockIndexint: index of block to increment, starting from 0valueint: non-negative to increment byThrows TagLostExceptionif the tag leaves the field IOExceptionif there is an I/O failure, or the operation is canceled isConnected
Added in API level 10public boolean isConnected ()
Helper to indicate if I/O operations should be possible.
Returns true if
connect()has completed, andclose()has not been called, and theTagis not known to be out of range.Does not cause RF activity, and does not block.
Returns booleantrue if I/O operations should be possible readBlock
Added in API level 10public byte[] readBlock (int blockIndex)
Read 16-byte block.
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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters blockIndexint: index of block to read, starting from 0Returns byte[]16 byte block Throws TagLostExceptionif the tag leaves the field IOExceptionif there is an I/O failure, or the operation is canceled restore
Added in API level 10public void restore (int blockIndex)
Copy from a value block to the temporary block.
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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters blockIndexint: index of block to copy fromThrows TagLostExceptionif the tag leaves the field IOExceptionif there is an I/O failure, or the operation is canceled sectorToBlock
Added in API level 10public int sectorToBlock (int sectorIndex)
Return the first block of a given sector.
Does not cause any RF activity and does not block.
Parameters sectorIndexint: index of sector to lookup, starting from 0Returns intblock index of first block in sector setTimeout
Added in API level 14public void setTimeout (int timeout)
Set the
transceive(byte)timeout in milliseconds.The timeout only applies to
transceive(byte)on this object, and is reset to a default value whenclose()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
Manifest.permission.NFCpermission.Parameters timeoutint: timeout value in millisecondsThrows SecurityExceptionif the tag object is reused after the tag has left the field transceive
Added in API level 10public byte[] transceive (byte[] data)
Send raw NfcA data to a tag and receive the response.
This is equivalent to connecting to this tag via
NfcAand callingNfcA.transceive. Note that all MIFARE Classic tags are based onNfcAtechnology.Use
getMaxTransceiveLength()to retrieve the maximum number of bytes that can be sent withtransceive(byte).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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters databyteReturns byte[]Throws IOExceptionSee also:
transfer
Added in API level 10public void transfer (int blockIndex)
Copy from the temporary block to a value block.
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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters blockIndexint: index of block to copy toThrows TagLostExceptionif the tag leaves the field IOExceptionif there is an I/O failure, or the operation is canceled writeBlock
Added in API level 10public void writeBlock (int blockIndex, byte[] data)Write 16-byte block.
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
IOExceptionifclose()is called from another thread.Requires the
Manifest.permission.NFCpermission.Parameters blockIndexint: index of block to write, starting from 0databyte: 16 bytes of data to writeThrows TagLostExceptionif the tag leaves the field IOExceptionif there is an I/O failure, or the operation is canceled