我已经实现了一个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有用于异常的CardException类。我的问题是,我不知道何时发送06或07类型的消息,因为我无法区分卡生成的错误和抛出CardException时由读取器生成的错误。我怎么能做到这一点?
发布于 2017-06-19 13:50:40
中使用的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异常。您可以通过检查状态词(方法getSW() of ResponseAPDU)来测试这些错误:
if (r.getSW() == 0x09000) {
// success indicated by the card
} else {
// error or warning condition generated by the card
}https://stackoverflow.com/questions/44564224
复制相似问题