java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.internal.rfid.MFRC522
- Direct Known Subclasses:
RfidComponent
Implementation of MFRC522 RFID Reader/Writer used for interacting with RFID cards.
Uses SPI via Pi4J for communication with the PCD (Proximity Coupling Device).
The official name for cards is PICC (Proximity Integrated Circuit Card).
-
Field Summary
Modifier and TypeFieldDescriptionprotected final com.pi4j.io.gpio.digital.DigitalOutput
Pi4J digital output optionally used as reset pin for the MFRC522protected final com.pi4j.io.spi.Spi
Pi4J SPI instance -
Constructor Summary
ConstructorDescriptionMFRC522
(com.pi4j.io.gpio.digital.DigitalOutput resetPin, com.pi4j.io.spi.Spi spi) Creates a new MFRC522 instance using the given reset pin and SPI instance from Pi4J.MFRC522
(com.pi4j.io.spi.Spi spi) Creates a new MFRC522 instance without a reset pin for the given SPI instance from Pi4J. -
Method Summary
Modifier and TypeMethodDescriptionSelects a single PICC and transitions it from READY to ACTIVE state, then returns an appropriateRfidCard
instance.boolean
Returns a boolean if at least one PICC is in the proximity of the PCD.boolean
Returns a boolean if at least one new PICC is in the proximity of the PCD.protected void
mifareAuth
(MifareKey key, byte blockAddr, RfidCardUid uid) Authenticates the sector to which the specified block belongs for MIFARE PICCs.protected byte[]
mifareRead
(byte blockAddr) Reads the specified block from a MIFARE PICC usingPiccCommand.MF_READ
.protected void
Stops the encrypted communication towards the PICC, must be called whenmifareAuth(MifareKey, byte, RfidCardUid)
was used.protected void
mifareWrite
(byte blockAddr, byte[] dataBuffer) Writes the specified amount of data usingPiccCommand.MF_WRITE
to a MIFARE PICC.void
reset()
Resets the PCD into a well-known state and callsinit()
to achieve a well-known state.protected RfidCardUid
select()
Selects a single PICC by executing the ANTICOLLISION and SELECT procedure according to ISO 14443.protected void
setAntennaState
(boolean on) Enables or disables the TX1 and TX2 antennas required for powering the PICCs.void
Uninitializes the currently active card by sending it back into HALT state and stopping encrypted communication.Methods inherited from class com.pi4j.crowpi.components.Component
sleep, triggerSimpleEvent
-
Field Details
-
resetPin
protected final com.pi4j.io.gpio.digital.DigitalOutput resetPinPi4J digital output optionally used as reset pin for the MFRC522 -
spi
protected final com.pi4j.io.spi.Spi spiPi4J SPI instance
-
-
Constructor Details
-
MFRC522
public MFRC522(com.pi4j.io.spi.Spi spi) Creates a new MFRC522 instance without a reset pin for the given SPI instance from Pi4J.- Parameters:
spi
- SPI instance
-
MFRC522
public MFRC522(com.pi4j.io.gpio.digital.DigitalOutput resetPin, com.pi4j.io.spi.Spi spi) Creates a new MFRC522 instance using the given reset pin and SPI instance from Pi4J.- Parameters:
resetPin
- Digital output used as reset pin for MFRC522, high is considered as power-on, low as power-offspi
- SPI instance
-
-
Method Details
-
reset
public void reset()Resets the PCD into a well-known state and callsinit()
to achieve a well-known state. Internally this will either trigger a hard- or soft-reset depending on the current PCD condition. After calling this method, the PCD will be ready for communication with PICCs. -
isNewCardPresent
public boolean isNewCardPresent()Returns a boolean if at least one new PICC is in the proximity of the PCD. This means that only PICCs in IDLE state are considered, meaning a REQA gets sent.- See Also:
-
isCardPresent(boolean)
-
isAnyCardPresent
public boolean isAnyCardPresent()Returns a boolean if at least one PICC is in the proximity of the PCD. This means that al PICCs in either IDLE or HALT state are considered, meaning a WUPA gets sent.- See Also:
-
isCardPresent(boolean)
-
initializeCard
Selects a single PICC and transitions it from READY to ACTIVE state, then returns an appropriateRfidCard
instance. Must only be called when a previous call toisNewCardPresent()
returned true.- Returns:
- Card instance for further interaction
- Throws:
RfidException
- Communication with PICC failed or card is unsupported
-
uninitializeCard
Uninitializes the currently active card by sending it back into HALT state and stopping encrypted communication. Any previously created instance ofRfidCard
will be INVALID and must no longer be used after calling this method.- Throws:
RfidException
-
setAntennaState
protected void setAntennaState(boolean on) Enables or disables the TX1 and TX2 antennas required for powering the PICCs. Must be called after each reset as antennas are disabled by default.- Parameters:
on
- True to enable, false to disable antennas
-
select
Selects a single PICC by executing the ANTICOLLISION and SELECT procedure according to ISO 14443. This method expects at least one PICC in READY state, which can be achieved usingrequestA(byte[])
orwakeupA(byte[])
.Upon successful completion, a single PICC will now be in ACTIVE state and ready for communication. All other PICCs will return to their IDLE or HALT state and no longer conflict with each other. The UID of the targeted PICC will be returned which should be stored for further interaction.
- Returns:
- UID of PICC transitioned into ACTIVE state
- Throws:
RfidException
- Unable to select PICC, e.g. timeout, missing presence, protocol error, ...
-
mifareAuth
Authenticates the sector to which the specified block belongs for MIFARE PICCs. A valid key A or key B must be provided based on the required access privileges. Do not forget to callmifareStopCrypto1()
once PICC communication is complete.- Parameters:
key
- Authentication key to use for this sectorblockAddr
- Block address for which sector gets authenticateduid
- UID of the PICC, required as part of the authentication- Throws:
RfidException
- Authentication against PICC has failed
-
mifareStopCrypto1
protected void mifareStopCrypto1()Stops the encrypted communication towards the PICC, must be called whenmifareAuth(MifareKey, byte, RfidCardUid)
was used. Without calling this method, communication with other PICCs is impossible aside from resetting the PCD. -
mifareRead
Reads the specified block from a MIFARE PICC usingPiccCommand.MF_READ
. The affected sector must be authenticated in advance usingmifareAuth(MifareKey, byte, RfidCardUid)
. WARNING: Not all block contain user-provided data, make sure to pay attention to this when deciding which blocks to read.- Parameters:
blockAddr
- Block address to read from- Returns:
- Data read from PICC, length varies depending on type
- Throws:
RfidException
- Reading data from specified block has failed
-
mifareWrite
Writes the specified amount of data usingPiccCommand.MF_WRITE
to a MIFARE PICC. The affected sector must be authenticated in advance usingmifareAuth(MifareKey, byte, RfidCardUid)
. WARNING: Overwriting critical blocks of data, e.g. the sector trailers, will PERMANENTLY brick the PICC.- Parameters:
blockAddr
- Block address to write todataBuffer
- Data buffer to write, must have correct length for used PICC- Throws:
RfidException
- Writing data to specified block has failed
-