public synchronized List<CardTerminal> list(State state) throws CardException { // System.out.println("list"); if (state == null) { throw new NullPointerException(); } try { String[] readerNames = SCardListReaders(contextId); List<CardTerminal> list = new ArrayList<CardTerminal>(readerNames.length); if (stateMap == null) { // If waitForChange() has never been called, treat event // queries as status queries. if (state == CARD_INSERTION) { state = CARD_PRESENT; } else if (state == CARD_REMOVAL) { state = CARD_ABSENT; } } for (String readerName : readerNames) { CardTerminal terminal = implGetTerminal(readerName); ReaderState readerState; switch (state) { case ALL: list.add(terminal); break; case CARD_PRESENT: if (terminal.isCardPresent()) { list.add(terminal); } break; case CARD_ABSENT: if (terminal.isCardPresent() == false) { list.add(terminal); } break; case CARD_INSERTION: readerState = stateMap.get(readerName); if ((readerState != null) && readerState.isInsertion()) { list.add(terminal); } break; case CARD_REMOVAL: readerState = stateMap.get(readerName); if ((readerState != null) && readerState.isRemoval()) { list.add(terminal); } break; default: throw new CardException("Unknown state: " + state); } } return Collections.unmodifiableList(list); } catch (PCSCException e) { throw new CardException("list() failed", e); } }
public void ConnectToSmartCard() { try { TerminalFactory factory = TerminalFactory.getDefault(); List<CardTerminal> terminals = factory.terminals().list(); System.out.println("Terminals: " + terminals); // get the first terminal // CardTerminals.State. CardTerminal terminal = terminals.get(0); if (terminal.isCardPresent()) { JOptionPane.showMessageDialog(null, "Card is Present"); this.terminal = terminal; } else { JOptionPane.showMessageDialog(null, "Card is absent.Connnect it in 5 secs\nWaiting..."); terminal.waitForCardPresent(5000); JOptionPane.showMessageDialog(null, "Time is Up!\nBye!!!"); return; } // establish a connection with the card Card card = terminal.connect("*"); this.card1 = card; } catch (CardException ce) { } }
/** * Function to connect to the MSC * * @throws TokenException */ public void connect() throws TokenException { int i, j; try { /* * Get all available terminals.. */ TerminalFactory factory = TerminalFactory.getDefault(); List<CardTerminal> terminals = factory.terminals().list(); if (terminals.isEmpty()) { throw new TokenException("No terminals found"); } /* * Select appropriate terminal */ CardTerminal terminal = null; for (i = 0; i < terminals.size(); i++) { /* * Try the known terminal names the msc-driver uses.. */ for (j = 0; j < TERMINAL_NAMES.length; j++) { if (terminals.get(i).getName().startsWith(TERMINAL_NAMES[j])) { terminal = terminals.get(i); break; } } } if (terminal == null || !terminal.isCardPresent()) { throw new TokenException("No G&D MSC found in terminal(s)"); } /* * Establish a connection with the myCard */ myCard = terminal.connect("T=1"); myChannel = myCard.getBasicChannel(); /* * Of the several AIDs in distribution, try to select one that * actually works.. ;) */ boolean appSelected = false; for (i = 0; i < APPLET_IDS.length; i++) { if (appSelected = selectApplet(APPLET_IDS[i])) { break; } } if (!appSelected) { throw new TokenException("Could not select Applet"); } } catch (TokenException e) { throw e; } catch (Exception e) { throw new TokenException("Some exception occured: " + e.toString()); } }
public boolean IsCardPresent() { boolean Ispresent = false; if (card1 != null && terminal != null) { try { if (terminal.isCardPresent()) { Ispresent = true; } else { Ispresent = false; } } catch (CardException ce) { } } else { // JOptionPane.showMessageDialog(null,"SmartCard Initialization Problem\nBye!!!"); } return Ispresent; }