Example usage for javax.smartcardio Card getProtocol

List of usage examples for javax.smartcardio Card getProtocol

Introduction

In this page you can find the example usage for javax.smartcardio Card getProtocol.

Prototype

public abstract String getProtocol();

Source Link

Document

Returns the protocol in use for this card.

Usage

From source file:src.eidreader.EstEIDUtil.java

public String readCard(final Boolean full) {
    System.err.println("EIDReader.readCard()");
    return AccessController.doPrivileged(new PrivilegedAction<String>() {
        public String run() {
            try {
                TerminalFactory factory = TerminalFactory.getDefault();
                List<CardTerminal> terminals = factory.terminals().list();

                if (terminals.size() == 0) {
                    return "Error: No card reader found";
                }//from  w  ww .  j a va  2  s . c o  m
                //~ throws java.lang.IndexOutOfBoundsException when there is no reader
                CardTerminal terminal = terminals.get(0);

                if (!terminal.isCardPresent()) {
                    return "Error: No card found on terminal";
                    //~ return new EidReaderResponse(new String[] { "No card found on terminal" });
                }
                // "The best way to go is, as explained by Shane, to use the wildcard protocol."
                // https://forums.oracle.com/message/10531935
                Card card = terminal.connect("T=0");
                // Card card = terminal.connect("T=1");
                // Card card = terminal.connect("*");
                System.err.println("Protocol: " + card.getProtocol());
                ATR atr = card.getATR();

                CardChannel channel = card.getBasicChannel();

                if (BelgianReader.matchesEidAtr(atr)) {
                    System.err.println("It's a Belgian card");
                    BelgianReader pf = new BelgianReader(channel);
                    return pf.resultAsString(full);
                }

                System.err.println("It's an Estonian card");
                PersonalFile pf = new PersonalFile(channel);
                return pf.resultAsString(full);
                //~ return new String[] { pf.toString() };
                //~ return new EidReaderResponse(pf.getData());
                //~ return pf.getSurName();
            } catch (Throwable e) {
                //~ return new EidReaderResponse(new String[] { e.toString() });
                e.printStackTrace();
                return "Error: " + e.toString();
            }
        }
    });
}