简体   繁体   中英

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

I'm having trouble finding the solution to this problem in my program: I create and personalize a JCOP card with the JMRTD library, but, after I finish, send the close command to the service, and insert the card again, any attempt to do anything just returns me that the card was disconnected. Am I missing someway to reset the flag?

Notes about my code: ControlledDialog as the name suggests is a programatically controlled dialog so i can open and close it in specific parts of the code.

Thanks for any help!

My CardConnection class:

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;

    }

}

I don't really know how to remove a question because there is not enough information here to what was happening. After careful debugging by printing the hashcode of the object calling each function, i discovered that the reason for that problem was the fact that I did not update the "service" field of the object calling these functions, and therefore, using the service with the disconnected card instead of the newly created one. Thanks for the comprehension!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM