Example usage for javax.smartcardio CardTerminal isCardPresent

List of usage examples for javax.smartcardio CardTerminal isCardPresent

Introduction

In this page you can find the example usage for javax.smartcardio CardTerminal isCardPresent.

Prototype

public abstract boolean isCardPresent() throws CardException;

Source Link

Document

Returns whether a card is present in this terminal.

Usage

From source file:Main.java

/**
 * Establishes the connection with the Smart Card (if present)
 * using the first available terminal./*  w ww  .jav a2s  . co m*/
 */
public static Card connectCard() throws CardException {
    // Get available terminals
    List<CardTerminal> terminals = TerminalFactory.getDefault().terminals().list();
    if (terminals.size() < 1) {
        throw new CardException("No attached Smart Card terminals found");
    }

    // Search for connected cards
    Card card = null;
    for (CardTerminal terminal : terminals) {
        if (terminal.isCardPresent()) {
            card = terminal.connect("*");
            break;
        }
    }
    if (card == null) {
        throw new CardException("No connected Smart Cards found");
    }

    return card;
}

From source file:davmail.util.ClientCertificateTest.java

public void testCardReaders() throws CardException {
    for (CardTerminal terminal : TerminalFactory.getDefault().terminals().list()) {
        System.out.println("Card terminal: " + terminal.getName() + " "
                + (terminal.isCardPresent() ? "Card present" : "No card"));
        terminal.waitForCardPresent(10);
        if (terminal.isCardPresent()) {
            Card c = null;/* w w w . j av  a 2 s. c o  m*/
            try {
                c = terminal.connect("T=0");
            } catch (Exception e) {
                // failover
                c = terminal.connect("T=1");
            }

            ATR atr = c.getATR();

            byte[] bytes = atr.getBytes();
            System.out.print("card:");

            for (byte b : bytes) {
                System.out.print(" " + Integer.toHexString(b & 0xff));
            }
            System.out.println();

        }
    }
}

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  w w  .j  a v  a 2  s  . c om*/
                //~ 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();
            }
        }
    });
}