MifareClassic
class MifareClassic : TagTechnology
kotlin.Any | |
↳ | android.nfc.tech.MifareClassic |
Provides access to MIFARE Classic properties and I/O operations on a Tag
.
Acquire a MifareClassic
object using get
.
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. {@see #authenticateSectorWithKeyA} and {@see #authenticateSectorWithKeyB}.
Three well-known authentication keys are defined in this class: KEY_DEFAULT
, KEY_MIFARE_APPLICATION_DIRECTORY
, KEY_NFC_FORUM
.
KEY_DEFAULT
is the default factory key for MIFARE Classic.KEY_MIFARE_APPLICATION_DIRECTORY
is the well-known key for MIFARE Classic cards that have been formatted according to the MIFARE Application Directory (MAD) specification.KEY_NFC_FORUM
is 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
MifareClassic
will never be enumerated inTag.getTechList
. If it is enumerated, then allMifareClassic
I/O operations will be supported, andNdef.MIFARE_CLASSIC
NDEF tags will also be supported. In either case,NfcA
will also be enumerated on the tag, because all MIFARE Classic tags are alsoNfcA
.Note: Methods that perform I/O operations require the
android.Manifest.permission#NFC
permission.
Summary
Constants | |
---|---|
static Int |
Size of a MIFARE Classic block (in bytes) |
static Int |
Tag contains 16 sectors, each with 4 blocks. |
static Int |
Tag contains 32 sectors, each with 4 blocks. |
static Int |
Tag contains 40 sectors. |
static Int |
Tag contains 5 sectors, each with 4 blocks. |
static Int |
A MIFARE Classic tag |
static Int |
A MIFARE Plus tag |
static Int |
A MIFARE Pro tag |
static Int |
A MIFARE Classic compatible card of unknown type |
Public methods | |
---|---|
Boolean |
authenticateSectorWithKeyA(sectorIndex: Int, key: ByteArray!) Authenticate a sector with key A. |
Boolean |
authenticateSectorWithKeyB(sectorIndex: Int, key: ByteArray!) Authenticate a sector with key B. |
Int |
blockToSector(blockIndex: Int) Return the sector that contains a given block. |
Unit |
close() |
Unit |
connect() |
Unit |
Decrement a value block, storing the result in the temporary block on the tag. |
static MifareClassic! |
Get an instance of |
Int |
Return the total number of MIFARE Classic blocks. |
Int |
getBlockCountInSector(sectorIndex: Int) Return the number of blocks in the given sector. |
Int |
Return the maximum number of bytes that can be sent with |
Int |
Return the number of MIFARE Classic sectors. |
Int |
getSize() Return the size of the tag in bytes |
Tag! |
getTag() |
Int |
Get the current |
Int |
getType() Return the type of this MIFARE Classic compatible tag. |
Unit |
Increment a value block, storing the result in the temporary block on the tag. |
Boolean | |
ByteArray! |
Read 16-byte block. |
Unit |
Copy from a value block to the temporary block. |
Int |
sectorToBlock(sectorIndex: Int) Return the first block of a given sector. |
Unit |
setTimeout(timeout: Int) Set the |
ByteArray! |
transceive(data: ByteArray!) Send raw NfcA data to a tag and receive the response. |
Unit |
Copy from the temporary block to a value block. |
Unit |
writeBlock(blockIndex: Int, data: ByteArray!) Write 16-byte block. |
Properties | |
---|---|
static ByteArray! |
The default factory key. |
static ByteArray! |
The well-known key for tags formatted according to the MIFARE Application Directory (MAD) specification. |
static ByteArray! |
The well-known key for tags formatted according to the NDEF on MIFARE Classic specification. |
Constants
BLOCK_SIZE
static val BLOCK_SIZE: Int
Size of a MIFARE Classic block (in bytes)
Value: 16
SIZE_1K
static val SIZE_1K: Int
Tag contains 16 sectors, each with 4 blocks.
Value: 1024
SIZE_2K
static val SIZE_2K: Int
Tag contains 32 sectors, each with 4 blocks.
Value: 2048
SIZE_4K
static val SIZE_4K: Int
Tag contains 40 sectors. The first 32 sectors contain 4 blocks and the last 8 sectors contain 16 blocks.
Value: 4096
SIZE_MINI
static val SIZE_MINI: Int
Tag contains 5 sectors, each with 4 blocks.
Value: 320
TYPE_UNKNOWN
static val TYPE_UNKNOWN: Int
A MIFARE Classic compatible card of unknown type
Value: -1
Public methods
authenticateSectorWithKeyA
fun authenticateSectorWithKeyA(
sectorIndex: Int,
key: ByteArray!
): Boolean
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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
sectorIndex |
Int: index of sector to authenticate, starting from 0 |
key |
ByteArray!: 6-byte authentication key |
Return | |
---|---|
Boolean |
true on success, false on authentication failure |
Exceptions | |
---|---|
android.nfc.TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or the operation is canceled |
authenticateSectorWithKeyB
fun authenticateSectorWithKeyB(
sectorIndex: Int,
key: ByteArray!
): Boolean
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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
sectorIndex |
Int: index of sector to authenticate, starting from 0 |
key |
ByteArray!: 6-byte authentication key |
Return | |
---|---|
Boolean |
true on success, false on authentication failure |
Exceptions | |
---|---|
android.nfc.TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or the operation is canceled |
blockToSector
fun blockToSector(blockIndex: Int): Int
Return the sector that contains a given block.
Does not cause any RF activity and does not block.
Parameters | |
---|---|
blockIndex |
Int: index of block to lookup, starting from 0 |
Return | |
---|---|
Int |
sector index that contains the block |
close
fun close(): Unit
Exceptions | |
---|---|
java.lang.Exception |
if this resource cannot be closed |
java.io.IOException |
if an I/O error occurs |
java.lang.SecurityException |
if the tag object is reused after the tag has left the field |
connect
fun connect(): Unit
Exceptions | |
---|---|
TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or connect is canceled |
java.lang.SecurityException |
if the tag object is reused after the tag has left the field |
decrement
fun decrement(
blockIndex: Int,
value: Int
): Unit
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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
blockIndex |
Int: index of block to decrement, starting from 0 |
value |
Int: non-negative to decrement by |
Exceptions | |
---|---|
android.nfc.TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or the operation is canceled |
get
static fun get(tag: Tag!): MifareClassic!
Get an instance of MifareClassic
for the given tag.
Does not cause any RF activity and does not block.
Returns null if MifareClassic
was not enumerated in Tag.getTechList
. This indicates the tag is not MIFARE Classic compatible, or this Android device does not support MIFARE Classic.
Parameters | |
---|---|
tag |
Tag!: an MIFARE Classic compatible tag |
Return | |
---|---|
MifareClassic! |
MIFARE Classic object |
getBlockCount
fun getBlockCount(): Int
Return the total number of MIFARE Classic blocks.
Does not cause any RF activity and does not block.
Return | |
---|---|
Int |
total number of blocks |
getBlockCountInSector
fun getBlockCountInSector(sectorIndex: Int): Int
Return the number of blocks in the given sector.
Does not cause any RF activity and does not block.
Parameters | |
---|---|
sectorIndex |
Int: index of sector, starting from 0 |
Return | |
---|---|
Int |
number of blocks in the sector |
getMaxTransceiveLength
fun getMaxTransceiveLength(): Int
Return the maximum number of bytes that can be sent with transceive
.
Return | |
---|---|
Int |
the maximum number of bytes that can be sent with transceive . |
getSectorCount
fun getSectorCount(): Int
Return the number of MIFARE Classic sectors.
Does not cause any RF activity and does not block.
Return | |
---|---|
Int |
number of sectors |
getSize
fun getSize(): Int
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.
Return | |
---|---|
Int |
size in bytes |
getTag
fun getTag(): Tag!
Return | |
---|---|
Tag! |
the Tag backing this TagTechnology object. |
getTimeout
fun getTimeout(): Int
Get the current transceive
timeout in milliseconds.
Requires the android.Manifest.permission#NFC
permission.
Return | |
---|---|
Int |
timeout value in milliseconds |
Exceptions | |
---|---|
java.lang.SecurityException |
if the tag object is reused after the tag has left the field |
getType
fun getType(): Int
Return the type of this MIFARE Classic compatible tag.
One of TYPE_UNKNOWN
, TYPE_CLASSIC
, TYPE_PLUS
or TYPE_PRO
.
Does not cause any RF activity and does not block.
Return | |
---|---|
Int |
type |
increment
fun increment(
blockIndex: Int,
value: Int
): Unit
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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
blockIndex |
Int: index of block to increment, starting from 0 |
value |
Int: non-negative to increment by |
Exceptions | |
---|---|
android.nfc.TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or the operation is canceled |
isConnected
fun isConnected(): Boolean
Return | |
---|---|
Boolean |
true if I/O operations should be possible |
readBlock
fun readBlock(blockIndex: Int): ByteArray!
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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
blockIndex |
Int: index of block to read, starting from 0 |
Return | |
---|---|
ByteArray! |
16 byte block |
Exceptions | |
---|---|
android.nfc.TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or the operation is canceled |
restore
fun restore(blockIndex: Int): Unit
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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
blockIndex |
Int: index of block to copy from |
Exceptions | |
---|---|
android.nfc.TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or the operation is canceled |
sectorToBlock
fun sectorToBlock(sectorIndex: Int): Int
Return the first block of a given sector.
Does not cause any RF activity and does not block.
Parameters | |
---|---|
sectorIndex |
Int: index of sector to lookup, starting from 0 |
Return | |
---|---|
Int |
block index of first block in sector |
setTimeout
fun setTimeout(timeout: Int): Unit
Set the transceive
timeout in milliseconds.
The timeout only applies to transceive
on this object, and is reset to a default value when #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 android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
timeout |
Int: timeout value in milliseconds |
Exceptions | |
---|---|
java.lang.SecurityException |
if the tag object is reused after the tag has left the field |
transceive
fun transceive(data: ByteArray!): ByteArray!
Send raw NfcA data to a tag and receive the response.
This is equivalent to connecting to this tag via NfcA
and calling NfcA.transceive
. Note that all MIFARE Classic tags are based on NfcA
technology.
Use getMaxTransceiveLength
to retrieve the maximum number of bytes that can be sent with 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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
See Also
transfer
fun transfer(blockIndex: Int): Unit
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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
blockIndex |
Int: index of block to copy to |
Exceptions | |
---|---|
android.nfc.TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or the operation is canceled |
writeBlock
fun writeBlock(
blockIndex: Int,
data: ByteArray!
): Unit
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 IOException
if #close is called from another thread.
Requires the android.Manifest.permission#NFC
permission.
Parameters | |
---|---|
blockIndex |
Int: index of block to write, starting from 0 |
data |
ByteArray!: 16 bytes of data to write |
Exceptions | |
---|---|
android.nfc.TagLostException |
if the tag leaves the field |
java.io.IOException |
if there is an I/O failure, or the operation is canceled |
Properties
KEY_MIFARE_APPLICATION_DIRECTORY
static val KEY_MIFARE_APPLICATION_DIRECTORY: ByteArray!
The well-known key for tags formatted according to the MIFARE Application Directory (MAD) specification.
KEY_NFC_FORUM
static val KEY_NFC_FORUM: ByteArray!
The well-known key for tags formatted according to the NDEF on MIFARE Classic specification.