簡體   English   中英

Javacard 如何在不從 CardImpl.class 中的 checkState() 中引發異常的情況下移除和重新插入卡

[英]Javacard how to remove and reinsert card without raising exception from checkState() in CardImpl.class

我在我的程序中找不到這個問題的解決方案:我使用 JMRTD 庫創建並個性化了 JCOP 卡,但是,在我完成后,將關閉命令發送到服務,然后再次插入卡,任何嘗試做任何事情都會告訴我卡已斷開連接。 我錯過了重置標志的方法嗎?

關於我的代碼的說明: ControlledDialog 顧名思義是一個程序控制的對話框,因此我可以在代碼的特定部分打開和關閉它。

謝謝你的幫助!

我的 CardConnection 類:

package util;

import UI.MainPanel;
import UI.VerifyPanel;
import java.security.Security;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.smartcardio.CardException;
import javax.smartcardio.CardNotPresentException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.TerminalFactory;
import net.sf.scuba.smartcards.CardServiceException;
import net.sf.scuba.smartcards.TerminalCardService;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jmrtd.PassportService;

public class CardConnection {

    static CardTerminal reader;
    static PassportService service;

    public static PassportService connectPassportService() throws CardException, CardServiceException {

        //Encontra-se a factory
        TerminalFactory terminal = TerminalFactory.getDefault();

        //listam-se os terminais
        List<CardTerminal> readers = terminal.terminals().list();

        if (readers.isEmpty()) {
            System.out.println("No Readers Found");
            System.exit(-1);
        }

        //escolhe-se o primeiro
        reader = readers.get(0);

        if (GlobalFlags.DEBUG) {
            System.err.println("Reader: " + reader);

            System.err.println("Por favor insira um cartão");
        }

        for (int i = 0; i < 10 && !reader.isCardPresent(); i++) {
            ControlledDialog.showMessageDialog("Por favor insira um cartão " + i, "Início");
            reader.waitForCardPresent(1000);
            System.err.println("Cartão " + (reader.isCardPresent() ? "" : "não ") + "conectado " + i);
        }
        try {
            if (reader.isCardPresent()) {

                service = new PassportService(new TerminalCardService(reader));
                service.open();
                service.sendSelectApplet(false);
                BouncyCastleProvider provider = new BouncyCastleProvider();
                Security.addProvider(provider);

            } else {
                throw new CardNotPresentException("Cartão não encontrado");
            }
        } finally {
            ControlledDialog.closeMessageDialog();
        }
        return service;
    }

    public static void closeConnection() {

        if (service == null) {
            return;
        }
        service.close();

        try {

            if (reader.isCardPresent()) {
                if (GlobalFlags.DEBUG) {
                    System.err.println("Por favor retire o cartão");
                }
                ControlledDialog.showMessageDialog("Por favor retire o cartão", "Fim");
            }

            while (reader.isCardPresent()) {
                System.out.println("Remova o cartão atual");
                reader.waitForCardAbsent(100000);
            }

            ControlledDialog.closeMessageDialog();

        } catch (CardException ex) {
            Logger.getLogger(VerifyPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        reader = null;
        service = null;

    }

}

我真的不知道如何刪除一個問題,因為這里沒有足夠的信息來說明正在發生的事情。 通過打印調用每個函數的對象的哈希碼進行仔細調試后,我發現該問題的原因是我沒有更新調用這些函數的對象的“服務”字段,因此,將服務與斷開連接的卡而不是新創建的卡。 謝謝理解!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM