java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.internal.rfid.MFRC522
Direct Known Subclasses:
RfidComponent

public class MFRC522 extends Component
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

    Fields
    Modifier and Type
    Field
    Description
    protected final com.pi4j.io.gpio.digital.DigitalOutput
    Pi4J digital output optionally used as reset pin for the MFRC522
    protected final com.pi4j.io.spi.Spi
    Pi4J SPI instance

    Fields inherited from class com.pi4j.crowpi.components.Component

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    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.
    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 Type
    Method
    Description
    Selects a single PICC and transitions it from READY to ACTIVE state, then returns an appropriate RfidCard 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 using PiccCommand.MF_READ.
    protected void
    Stops the encrypted communication towards the PICC, must be called when mifareAuth(MifareKey, byte, RfidCardUid) was used.
    protected void
    mifareWrite(byte blockAddr, byte[] dataBuffer)
    Writes the specified amount of data using PiccCommand.MF_WRITE to a MIFARE PICC.
    void
    Resets the PCD into a well-known state and calls init() to achieve a well-known state.
    protected RfidCardUid
    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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • resetPin

      protected final com.pi4j.io.gpio.digital.DigitalOutput resetPin
      Pi4J digital output optionally used as reset pin for the MFRC522
    • spi

      protected final com.pi4j.io.spi.Spi spi
      Pi4J 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-off
      spi - SPI instance
  • Method Details

    • reset

      public void reset()
      Resets the PCD into a well-known state and calls init() 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

      public RfidCard initializeCard() throws RfidException
      Selects a single PICC and transitions it from READY to ACTIVE state, then returns an appropriate RfidCard instance. Must only be called when a previous call to isNewCardPresent() returned true.
      Returns:
      Card instance for further interaction
      Throws:
      RfidException - Communication with PICC failed or card is unsupported
    • uninitializeCard

      public void uninitializeCard() throws RfidException
      Uninitializes the currently active card by sending it back into HALT state and stopping encrypted communication. Any previously created instance of RfidCard 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

      protected RfidCardUid select() throws RfidException
      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 using requestA(byte[]) or wakeupA(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

      protected void mifareAuth(MifareKey key, byte blockAddr, RfidCardUid uid) throws RfidException
      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 call mifareStopCrypto1() once PICC communication is complete.
      Parameters:
      key - Authentication key to use for this sector
      blockAddr - Block address for which sector gets authenticated
      uid - 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 when mifareAuth(MifareKey, byte, RfidCardUid) was used. Without calling this method, communication with other PICCs is impossible aside from resetting the PCD.
    • mifareRead

      protected byte[] mifareRead(byte blockAddr) throws RfidException
      Reads the specified block from a MIFARE PICC using PiccCommand.MF_READ. The affected sector must be authenticated in advance using mifareAuth(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

      protected void mifareWrite(byte blockAddr, byte[] dataBuffer) throws RfidException
      Writes the specified amount of data using PiccCommand.MF_WRITE to a MIFARE PICC. The affected sector must be authenticated in advance using mifareAuth(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 to
      dataBuffer - Data buffer to write, must have correct length for used PICC
      Throws:
      RfidException - Writing data to specified block has failed