我正在尝试通过 java smartcardio读取印度政府标准的“斯科斯塔”智能卡 ,我使用的代码是
package com.example.smartcardreader;
import java.util.List;
import javax.smartcardio.ATR;
import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;
public class SmartCardReader {
public static void main(String[] args) {
try{
// show the list of available terminals
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
// get the first terminal
CardTerminal terminal = terminals.get(0);
// establish a connection with the card
Card card = terminal.connect("*");
System.out.println("card: " + card);
// get the ATR
ATR atr = card.getATR();
byte[] baAtr = atr.getBytes();
System.out.print("ATR = 0x");
for(int i = 0; i < baAtr.length; i++ ){
System.out.printf("%02X ",baAtr[i]);
}
CardChannel channel = card.getBasicChannel();
byte[] cmdApduGetCardUid = new byte[]{
(byte)0xFF, (byte)0xCA, (byte)0x00, (byte)0x00, (byte)0x00};
ResponseAPDU respApdu = channel.transmit(
new CommandAPDU(cmdApduGetCardUid));
if(respApdu.getSW1() == 0x90 && respApdu.getSW2() == 0x00){
byte[] baCardUid = respApdu.getData();
System.out.print("Card UID = 0x");
for(int i = 0; i < baCardUid.length; i++ ){
System.out.printf("%02X ", baCardUid [i]);
}
}
card.disconnect(false);
} catch (CardException e) {
e.printStackTrace();
}
}
}
我正在使用 eclipse IDE 在 Mac 机器上进行开发。当我运行此代码时,它给了我异常,因为它无法读取终端。我有一个 USB 读卡器,并且还插入了智能卡。你能指出我到底哪里错了。提前致谢。