Added in API level 10

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 in Tag#getTechList. If it is enumerated, then all MifareClassic I/O operations will be supported, and Ndef#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 also NfcA.

    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

Authenticate a sector with key A.

Boolean

Authenticate a sector with key B.

Int
blockToSector(blockIndex: Int)

Return the sector that contains a given block.

Unit

Unit

Unit
decrement(blockIndex: Int, value: Int)

Decrement a value block, storing the result in the temporary block on the tag.

static MifareClassic!
get(tag: Tag!)

Get an instance of MifareClassic for the given tag.

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 transceive.

Int

Return the number of MIFARE Classic sectors.

Int

Return the size of the tag in bytes

Tag!

Int

Get the current transceive timeout in milliseconds.

Int

Return the type of this MIFARE Classic compatible tag.

Unit
increment(blockIndex: Int, value: Int)

Increment a value block, storing the result in the temporary block on the tag.

Boolean

ByteArray!
readBlock(blockIndex: Int)

Read 16-byte block.

Unit
restore(blockIndex: Int)

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 transceive timeout in milliseconds.

ByteArray!

Send raw NfcA data to a tag and receive the response.

Unit
transfer(blockIndex: Int)

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

Added in API level 10
static val BLOCK_SIZE: Int

Size of a MIFARE Classic block (in bytes)

Value: 16

SIZE_1K

Added in API level 10
static val SIZE_1K: Int

Tag contains 16 sectors, each with 4 blocks.

Value: 1024

SIZE_2K

Added in API level 10
static val SIZE_2K: Int

Tag contains 32 sectors, each with 4 blocks.

Value: 2048

SIZE_4K

Added in API level 10
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

Added in API level 10
static val SIZE_MINI: Int

Tag contains 5 sectors, each with 4 blocks.

Value: 320

TYPE_CLASSIC

Added in API level 10
static val TYPE_CLASSIC: Int

A MIFARE Classic tag

Value: 0

TYPE_PLUS

Added in API level 10
static val TYPE_PLUS: Int

A MIFARE Plus tag

Value: 1

TYPE_PRO

Added in API level 10
static val TYPE_PRO: Int

A MIFARE Pro tag

Value: 2

TYPE_UNKNOWN

Added in API level 10
static val TYPE_UNKNOWN: Int

A MIFARE Classic compatible card of unknown type

Value: -1

Public methods

authenticateSectorWithKeyA

Added in API level 10
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

Added in API level 10
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

Added in API level 10
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

Added in API level 10
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

Added in API level 10
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

Added in API level 10
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

Added in API level 10
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

Added in API level 10
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

Added in API level 10
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

Added in API level 14
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

Added in API level 10
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

Added in API level 10
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

Added in API level 10
fun getTag(): Tag!
Return
Tag! the Tag backing this TagTechnology object.

getTimeout

Added in API level 14
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

Added in API level 10
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

Added in API level 10
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

Added in API level 10
fun isConnected(): Boolean
Return
Boolean true if I/O operations should be possible

readBlock

Added in API level 10
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

Added in API level 10
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

Added in API level 10
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

Added in API level 14
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

Added in API level 10
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.

transfer

Added in API level 10
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

Added in API level 10
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_DEFAULT

Added in API level 10
static val KEY_DEFAULT: ByteArray!

The default factory key.

KEY_MIFARE_APPLICATION_DIRECTORY

Added in API level 10
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

Added in API level 10
static val KEY_NFC_FORUM: ByteArray!

The well-known key for tags formatted according to the NDEF on MIFARE Classic specification.