Java 如何区分智能卡读卡器错误和智能卡错误

Java 如何区分智能卡读卡器错误和智能卡错误,java,smartcard,apdu,smartcard-reader,pcsc,Java,Smartcard,Apdu,Smartcard Reader,Pcsc,我已经实现了一个Andoid应用程序服务器端应用程序。服务器与智能卡读卡器通信。当用户触摸按钮时 在Android应用程序中,正在为服务器构建连接,以获得用户身份验证。应用程序之间交换的消息 和服务器具有以下格式: <type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 <[data]> 智能卡IO API具有异常的CardException类。我的问题是,我不知道何时发送类型为06或07的消息,因为我无法区分卡生成的错误

我已经实现了一个Andoid应用程序服务器端应用程序。服务器与智能卡读卡器通信。当用户触摸按钮时 在Android应用程序中,正在为服务器构建连接,以获得用户身份验证。应用程序之间交换的消息 和服务器具有以下格式:

<type> 0x00 0x00 0x00 <length> 0x00 0x00 0x00 <[data]>
智能卡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异常。您可以通过检查状态字来测试这些错误(方法
responseADU的
getSW()
):

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)
}
if (r.getSW() == 0x09000) {
    // success indicated by the card
} else {
    // error or warning condition generated by the card
}