PiccManager

##
android.device
Class PiccManager

    java.lang.Object
      android.device.PiccManager

    public class PiccManager

extends java.lang.Object

The PiccManager class is used to initialize and control RF Card reader. Including data transmission, storage, read-write and other operation interfaces.

To read RF Card with this class, according to the following steps,

1. Obtain an instance of RF Card reader with PiccManager picc = new PiccManager().
2. Call `open` to initialize the RF card reader.
3. Start searching the RF card with `request`.
4. Anti-collision and select card with `antisel`.
5. Attach the target before transmitting apdu command with `activate`.
6. Transmit APDU command and get the response with `apduTransmit`.
7. Detach the target. If needn't to send APDU again, use `deactivate` to stop.
8. Close the RF card reader. with `close`.

   For more information about the RF card reader, read PiccManager sample.

Constructor Summary
`PiccManager()` The PiccManager class is used to initialize and control RF Card reader.

Method Summary
`int` | `open()` Open the RF card reader.
`int` | `close()` Close the RF card reader.
`int` | `activate()` Activate card to start sending APDU.
`int` | `deactivate(byte mode)` Deactivate a card.
`int` | `activateEx(byte[] art)` Activate card to start sending APDU.
`int` | `antisel(byte[] sn, byte[] sak)` Anti-collision and select card and returns the serial number of the card.
`int` | `apduTransmit(byte[] sent, int sentLen, byte[] rsp)` Transmit APDU exchange commands.
`int` | `apduTransmit(byte[] cmd, int cmdLen, byte[] rsp, byte[] sw)` Transmit APDU commands.
`int` | `apduTransmit(byte[] cmd, int cmdLen, byte[] rsp, int crc, int speed)` Transmit APDU exchange commands.
`int` | `apduTransmit(int cmd, int num, byte[] sent, int sentLen, byte[] rsp)` Transmit APDU commands for RF Card.
`int` | `apduTransmitFelica(byte[] cmd, int cmdLen, byte[] rsp)` Transfer APDU instruction to felica card.
`void` | `SetTimeOutFelica(int timeOut)` Set the felica APDU instruction timeout. Default is no timeout.
`int` | `enableRATS(byte value)` Request for answer to select.
`int` | `idcard_apduTransmit(byte[] sent, int sentLen, byte[] rsp)` Transmit APDU commands.
`int` | `idcard_init()` Init the ID card.
`int` | `m1_amount(int blkNo)` Read the amount for the block number.
`int` | `m1_decrement(int blkNO, int iMoney)` Decrement the value block by iMoney amount.
`int` | `m1_increment(int blkNo, int iMoney)` Increment the value block by iMoney amount.
`int` | `m1_init(int blkNo, int value)` Init the value block by value.
`int` | `m1_keyAuth(int keyType, int blnNo, int keyLen, byte[] keyBuf, int iSeriNumLen, byte[] seriNum)` Authenticate a key.
`int` | `m1_readBlock(int blkNo, byte[] pReadBuf)` Read a block.
`int` | `m1_restore(int blkNo)` Move the content of a block into temporary data register.
`int` | `m1_transfer(int blkNo)` Transfer from temporary internal data register to value block.
`int` | `m1_writeBlock(int blkNo, int iLenWriteBuf, byte[] pWriteBuf)` Write a block into the card.
`int` | `mifareUlIncrCnt(byte bCntNum, int iCntValue)` Write the Mifare UltraLight counter.
`int` | `mifareUlPageRead(int sectorNum, byte[] rsp)` Read the Mifare UltraLight data.
`int` | `mifareUlPageWrite(int sectorNum, byte[] sent)` Write the Mifare UltraLight data.
`int` | `mifareUlReadCnt(byte bCntNum, int[] pCntValue)` Read Mifare UltraLight counter.
`int` | `primeCard_apduTransmit(byte[] sent, int sentLen, byte[] rsp)` Transmit APDU commands for type B.
`int` | `primeCard_init()` Init for type B prime protocol.
`int` | `request_norats(byte[] mode, byte[] atq)` Request card and without RATS.
`int` | `request_type(byte pollType, byte[] mode, byte[] atq)` Request Specify card type.
`int` | `request(byte[] mode, byte[] atq)` Request card.
`int` | `transactionData(byte bProtocol, byte[] pSendBuf, int bSendLen, byte[] pRecvBuf, int[] pRecvLen)` Transaction command.

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### PiccManager

    public PiccManager()

Method Detail

### open

    public int open()

  Open the RF card reader.

  Parameters:
    none
  Returns:
    0 if successful, and negative number if failed.
  Example:
    `PiccManager piccManager = new PiccManager();
    int ret = piccManager.open();
    if (ret != 0) {
    //open failed
    }`

### enableRATS

    public int enableRATS(byte value)

  Request for answer to select.

  Parameters:
    `value` - Enabled value. 1 is enable; 0 is disable.
  Returns:
    0 if successful, and negative number if failed.

### primeCard_init

    public int primeCard_init()

  Init for type B prime protocol.

  Parameters:
    none
  Returns:
    0 if successful, and negative number if failed.
  Note:This implement work on Android 5.1 FW version 20180508 and later.

### primeCard_apduTransmit

    public int primeCard_apduTransmit(byte[] sent,
                                      int sentLen,
                                      byte[] rsp)

  Transmit APDU commands for type B.

  Parameters:
    `sent` - APDU command to be sent, the command format with ISO7816 transmission protocol.
    `sentLen` - APDU length.
    `rsp` - Response from card.
  Returns:
    The length of APDU commands, negative number if failed.
  Note:This implement work on Android 5.1 FW version 20180508 and later.

### request

    public int request(byte[] mode,
                       byte[] atq)

  Request card.

  Parameters:
    `mode` - The card type, could be 'A', 'B', 'F' or 'V'.
    `atq` - The ATQ.
  Returns:
    The length of ATQ.

### request_norats

    public int request_norats(byte[] mode,
                              byte[] atq)

  Request card and without RATS.

  Parameters:
    `mode` - The card type, could be 'A', 'B', 'F' or 'V'.
    `atq` - The ATQ.
  Returns:
    The length of ATQ.

### request_type

    public int request_type(byte pollType,
                            byte[] mode,
                            byte[] atq)

  Request specify card type.

  Parameters:
    `pollType` - Value:
    bit0 : A card
    bit1 : B card
    bit2 : Felica-212 card
    bit3 : Felica-424 card
    bit4 : 15693 card
    bit5 : 18000 card
    `mode` - The card type:
    'A' : A card
    'B' : B card
    `atq` - The ATQ.
  Returns:
    The length of ATQ.

### antisel

    public int antisel(byte[] sn,
                       byte[] sak)

  Anti-collision and select card and returns the serial number of the card.

  Parameters:
    `sn` - Serial number.
    `sak` - SAK.
  Returns:
    length of sn, negative number if failed.

### activate

    public int activate()

  Activate card to start sending APDU(skip this call for M1 card).

  Parameters:
    none
  Returns:
    0 if successful, negative number if failed.
  Example:
    `piccManager.open();
    int ret = piccManager.activate();
    if (ret != 0) {
    //activate failed
    }`

### activateEx

    public int activateEx(byte[] art)

  Activate card to start sending APDU(skip this call for M1 card).

  Parameters:
    `art` - Store the returned data.
  Returns:
    0 if successful, negative number if failed.
  Example:
    `piccManager.open();
    byte atr[] = new byte[32];
    int ret = piccManager.activateEx(atr);
    if (ret != 0) {
    //activateEx failed
    }`

### apduTransmit

    public int apduTransmit(byte[] cmd,
                            int cmdLen,
                            byte[] rsp,
                            byte[] sw)

  Transmit APDU commands.

  Parameters:
    `cmd` - APDU command to be sent, the command format with ISO7816 transmission protocol.
    `cmdLen` - APDU length.
    `rsp` - Response from card.
    `sw` - Status bytes.
  Returns:
    the APDU length(Length of response data returned by card, excluding 2-byte SW), negative number if failed.

### apduTransmit

    public int apduTransmit(byte[] sent,
                            int sentLen,
                            byte[] rsp)

  Transmit APDU exchange commands.

  Parameters:
    `sent` - APDU command to be sent.
    `sentLen` - APDU length.
    `rsp` - Response from card.
  Returns:
    The length of APDU commands, negative number if failed.

### apduTransmit

    public int apduTransmit(byte[] cmd,
                            int cmdLen,
                            byte[] rsp,
                            int crc,
                            int speed)

  Transmit APDU exchange commands.

  Parameters:
    `cmd` - APDU command to be sent.
    `cmdLen` - APDU length.
    `rsp` - Response from card.
    `crc` - APDU CRC.
    `speed` - APDU speed.
  Returns:
    The length of APDU commands, negative number if failed.

### apduTransmitFelica

    public int apduTransmitFelica(byte[] cmd,
    			      int cmdLen,
    			      byte[] rsp)

  Transfer APDU instruction to felica card.

  Parameters:
    `cmd` - The BCD code of the APDU instruction to be sent to the card is required, and the command format conforms to the 7816 specification.
    `cmdLen` - APDU length.
    `rsp` - Response data returned by card, BCD code.
  Returns:
    Length of response data, negative number if failed.

### SetTimeOutFelica

    public void SetTimeOutFelica(int timeOut)

  Set the felica APDU instruction timeout. Default is no timeout.

  Parameters:
    `timeOut` - Timeout, in MS.
  Returns:
    none

### deactivate

    public int deactivate(byte mode)

  Deavtivate a card.

  Parameters:
    `mode` - value:
    0x00 : imply HALT, deactivate card and return without checking for card removal;
    0x01 : imply REMOVE, deactivate card and check for card removal;
    0x02 : imply EMV compliant card removal, and check for card removal.
  Returns:
    0 if successful, negative number if failed.
    Examples:
    `piccManager.activate();
    public static final byte MODE = 0x00;
    int ret = piccManager.deactivate(MODE);
    if(ret != 0) {
    //deactivate failed
    }`

### close

    public int close()

  Close the RF card reader.

  Parameters:
    none
  Returns:
    0 if successful, negative number if failed.
  Example:
    `int ret = piccManager.open();
    if(ret == 0) {
    piccManager.close();
    }`

### m1_keyAuth

    public int m1_keyAuth(int keyType,
                          int blnNo,
                          int keyLen,
                          byte[] keyBuf,
                          int iSeriNumLen,
                          byte[] seriNum)

  Authenticate a key.

  Parameters:
    `keyType` - Key type:
    0 : A key
    1 : B key
    `blnNo` - Block number.
    `keyLen` - key length.
    `keyBuf` - The key.
    `iSeriNumLen` - Serial number length.
    `seriNum` - Serial number.
  Returns:
    0 if successful, negative number if failed.

### m1_readBlock

    public int m1_readBlock(int blkNo,
                            byte[] pReadBuf)

  Read a block.

  Parameters:
    `blkNo` - Block number.
    `pReadBuf` - To store the read data.
  Returns:
    0 if successful, negative number if failed.

### m1_writeBlock

    public int m1_writeBlock(int blkNo,
                             int iLenWriteBuf,
                             byte[] pWriteBuf)

  Write a block into the card.

  Parameters:
    `blkNo` - The block number.
    `iLenWriteBuf` - Length of buffer to be written.
    `pWriteBuf` - The buffer to be written.
  Returns:
    0 if successful, negative number if failed.

### m1_increment

    public int m1_increment(int blkNo,
                            int iMoney)

  Increment the value block by iMoney amount.

  Parameters:
    `blkNo` - The value block number.
    `iMoney` - The amount to be decremented.
  Returns:
    0 if successful, negative number if failed.

### m1_decrement

    public int m1_decrement(int blkNO,
                            int iMoney)

  Decrement the value block by iMoney amount.

  Parameters:
    `blkNO` - The value block number.
    `iMoney` - The amount to be decremented.
  Returns:
    0 if successful, negative number if failed.

### m1_restore

    public int m1_restore(int blkNo)

  Move the content of a block into temporary data register.

  Parameters:
    `blkNo` - The block number to be operated on.
  Returns:
    0 if successful, negative number if failed.

### m1_transfer

    public int m1_transfer(int blkNo)

  Transfer from temporary internal data register to value block.

  Parameters:
    `blkNo` - The block number to be operated on.
  Returns:
    0 if successful, negative number if failed.

### m1_init

    public int m1_init(int blkNo,
                       int value)

  Init the value block by value.

  Parameters:
    `blkNo` - The value block number.
    `value` - The amount to be init.
  Returns:
    0 if successful, negative number if failed.

### m1_amount

    public int m1_amount(int blkNo)

  Read the amount for the block number.

  Parameters:
    `blkNo` - The value block number.
  Returns:
    The amount, negative number if failed.

### idcard_init

    public int idcard_init()

  Init the ID card.

  Parameters:
    none
  Returns:
    0 if successful, negative number if failed.

### idcard_apduTransmit

    public int idcard_apduTransmit(byte[] sent,
                                   int sentLen,
                                   byte[] rsp)

  Transmit APDU commands.

  Parameters:
    `sent` - APDU command to be sent.
    `sentLen` - APDU length.
    `rsp` - Response from IDCard.
  Returns:
    The response data length, negative number if failed.

### apduTransmit

    public int apduTransmit(int cmd,
                            int num,
                            byte[] sent,
                            int sentLen,
                            byte[] rsp)

  Transmit APDU commands for RF Card.

  Parameters:
    `cmd` - Transmitted commands.
    `num` - Counter.
    `sent` - APDU command to be sent.
    `sendLen` - APDU length.
    `rsp` - Response from RF Card.
  Returns:
    The response data length, negative number if failed.

### mifareUlPageRead

    public int mifareUlPageRead(int sectorNum,
                                byte[] rsp)

  Read the Mifare UltraLight data.

  Parameters:
    `sectorNum` - Read the sector number.
    `rsp` - Output, 4 byte data from card.
  Returns:
    0 if successful, negative number if failed.

### mifareUlPageWrite

    public int mifareUlPageWrite(int sectorNum,
                                 byte[] sent)

  Write the Mifare UltraLight data.

  Parameters:
    `sectorNum` - Write the sector number.
    `sent` - 4 byte data to write.
  Returns:
    0 if successful, negative number if failed.

### mifareUlReadCnt

    public int mifareUlReadCnt(byte bCntNum,
                               int[] pCntValue)

  Read Mifare UltraLight counter.

  Parameters:
    `bCntNum` - Counter number 0 to 2.
    `pCntValue` - Output, the counter value.
  Returns:
    0 if successful, negative number if failed.

### mifareUlIncrCnt

    public int mifareUlIncrCnt(byte bCntNum,
                               int iCntValue)

  Write the Mifare UltraLight counter.

  Parameters:
    `bCntNum` - Counter number 0 to 2.
    `iCntValue` - Count value to write.
  Returns:
    0 if successful, negative number if failed.

### transactionData

    public int transactionData(byte bProtocol,
                               byte[] pSendBuf,
                               int bSendLen,
                               byte[] pRecvBuf,
                               int[] pRecvLen)

  Transaction command.

  Parameters:
    `bProtocol` - Card Type:
    0 : 14443A
    1 : 14443B
    `pSendBuf` - Send the command.
    `bSendLen` - Send command's length.
    `pRecvBuf` - Response data from the card.
    `pRecvLen` - Out data's length.
  Returns:
    0 if successful, negative number if failed.