我已经实现了一个 Andoid 应用程序 - 服务器端应用程序。服务器与智能卡读卡器通信。当用户触摸按钮时 在 Android 应用程序中,正在建立到服务器的连接以对用户进行身份验证。应用程序之间交换的消息 和服务器具有以下格式:
<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 <[data]>
- 如果消息的类型值
06
表示智能卡读卡器出错。 - 如果消息的类型值
07
表示智能卡中有错误。
我正在使用如下代码与智能卡读卡器通信:
// 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("T=0");
System.out.println("card: " + card);
CardChannel channel = card.getBasicChannel();
ResponseAPDU r = channel.transmit(new CommandAPDU(c1));
System.out.println("response: " + toString(r.getBytes()));
// disconnect
card.disconnect(false);
智能卡 IO API 具有用于异常的 CardException
类。我的问题是我不知道什么时候发送06
或07
类型的消息,因为我无法区分卡生成的错误和生成的错误当 CardException
被抛出时,由读者读取。我该如何管理?
最佳答案
transmit()
方法,用于
ResponseAPDU r = channel.transmit(new CommandAPDU(c1));
只会在与智能卡读卡器错误和读卡器与智能卡之间的通信问题相关的情况下抛出异常。当卡本身指示错误时,它不会抛出异常。
因此您可以通过捕获异常来捕获所有与阅读器相关的错误:
try {
ResponseAPDU r = channel.transmit(new CommandAPDU(c1));
} catch (IllegalStateException e) {
// channel has been closed or if the corresponding card has been disconnected
} catch (CardException e) {
// errors occured during communication with the smartcard stack or the card itself (e.g. no card present)
}
相反,卡生成的错误表示为在响应状态字中编码的错误代码。这些错误不会生成 Java 异常。您可以通过检查状态字(ResponseAPDU
的方法 getSW()
)来测试这些错误:
if (r.getSW() == 0x09000) {
// success indicated by the card
} else {
// error or warning condition generated by the card
}
关于java - 如何区分智能卡读卡器错误和智能卡错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564224/