public abstract class Card
extends java.lang.Object
An abstract class to implement card class. For example, user can extends this class to implement an EMV Card Class. And code the APDU commands to readable methods such as SelectFile(), ReadRecord() and VerifyPin() etc,.
To extend this class, subclass must implement IdentifyCard(byte[] ATR) function, which identify the card is
the correct card by check ATR or select application id.
To implement APDU command function, subclass can choose command name as the function name.
Construct the APDU command as a byte array.
Send the command by Transmit(byte[] apdu, int swExpected, String commandName).
for example:
public byte[] ReadRecord(byte SFI, byte no, int len) throws SCException {
// 0x00,0xb2,0x00,0x00,0x00
byte[] cmd = new byte[] { 0x00, (byte) 0xB2, 0x00, 0x00, 0x00};
if (len > 255)
throw new SCException("max read length > 255");
cmd[2] = no; // record number
cmd[3] = (byte) ((SFI << 3) | 0x04); // file id
cmd[4] = (byte) (len & 0xFF); // read length
byte[] rsp = Transmit(cmd, 0x9000, "READ RECORD");
byte[] data = new byte[rsp.length - 2];
System.arraycopy(rsp, 0, data, 0, rsp.length - 2);
return data;
}
| Modifier and Type | Field and Description |
|---|---|
static byte |
PROTOCOL_ANY |
static byte |
PROTOCOL_RAW |
static byte |
PROTOCOL_T0 |
static byte |
PROTOCOL_T1 |
static byte |
PROTOCOL_T15 |
| Constructor and Description |
|---|
Card() |
| Modifier and Type | Method and Description |
|---|---|
void |
AttachReader(IReader reader)
Not relevant to subclasses.
|
void |
Disconnect()
Terminates a connection previously opened between the calling application and a smart card in the target reader
|
protected void |
finalize()
Not relevant to subclasses.
|
int |
GetSW1SW2()
get card status bytes(SW1 & SW2) as int such as 0x9000
|
byte[] |
GetSW1SW2Bytes()
get card status bytes(SW1 & SW2) as bytes array
|
abstract boolean |
IdentifyCard(byte[] ATR)
Subclasses should override this method to identify the right card in the reader.
|
boolean |
isPresent() |
protected byte[] |
Select(byte[] data)
Performs a SELECT command to select the ICC PSE, DDF, or ADF
|
protected void |
SelectApplication(byte[] aid)
Performs a SELECT command to select an application.
|
byte[] |
Transmit(byte[] apdu)
Sends a APDU command to the smart card and expects to receive data back from the card.
|
byte[] |
Transmit(byte[] data,
int swExpected,
java.lang.String commandName) |
public static final byte PROTOCOL_T0
public static final byte PROTOCOL_T1
public static final byte PROTOCOL_ANY
public static final byte PROTOCOL_RAW
public static final byte PROTOCOL_T15
public void AttachReader(IReader reader)
protected void finalize()
finalize in class java.lang.Objectpublic void Disconnect()
throws SCException
SCException - smart card exception, check the message to see the exception detailpublic byte[] Transmit(byte[] apdu)
throws SCException
apdu - APDU commands data to be written to the cardSCException - smart card exception, check the message to see the exception detailpublic final byte[] Transmit(byte[] data,
int swExpected,
java.lang.String commandName)
throws SCException
SCExceptionpublic boolean isPresent()
throws SCException
SCExceptionpublic int GetSW1SW2()
public byte[] GetSW1SW2Bytes()
protected byte[] Select(byte[] data)
throws SCException
data - file idSCException - smart card exception, check the message to see the exception detailprotected void SelectApplication(byte[] aid)
throws SCException
aid - application idSCException - smart card exception, check the message to see the exception detailpublic abstract boolean IdentifyCard(byte[] ATR)
example:
protected boolean IdentifyCard(byte[] ATR){
myATR = ATR;
if (SelectPSE() != null)
return true;
else
return false;
}
ATR - Answer to reset of the cardCopyright © 2025. All rights reserved.