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) { } }
/** * What is doing the thread. * * <p>Will check if there is a reader available containing the wanted name (will call {@link * TerminalListener#cardReaderRemoved()} ()} if a listener is removed or added). If it is the case * it will wait for a card placed, call {@link TerminalListener#cardAdded(RFIDCard)}, wait for the * card to be removed then call {@link TerminalListener#cardRemoved()} */ @Override public void run() { while (!Thread.interrupted()) { if (terminalFactory == null) try { Thread.sleep(500); } catch (InterruptedException e) { } boolean lastPresent = this.isPresent; try { final CardTerminals terminalList = terminalFactory.terminals(); CardTerminal cardTerminal = null; try { for (CardTerminal terminal : terminalList.list()) if (terminal.getName().equals(this.terminalName)) { cardTerminal = terminal; this.isPresent = true; break; } } catch (CardException exception) { } if (cardTerminal == null) this.isPresent = false; if (this.isPresent != lastPresent) { if (this.isPresent) logger.log(Level.INFO, "Starting listening terminal " + cardTerminal.getName()); else logger.log(Level.INFO, "Stopped listening"); for (TerminalListener listener : this.listenersTerminal) if (this.isPresent) listener.cardReaderAdded(); else listener.cardReaderRemoved(); } if (!this.isPresent) continue; logger.log(Level.INFO, "Waiting for card..."); cardTerminal.waitForCardPresent(0); logger.log(Level.INFO, "Card detected"); this.lastCard = getCardInfos(cardTerminal.connect("*")); for (TerminalListener listener : this.listenersTerminal) listener.cardAdded(this.lastCard); cardTerminal.waitForCardAbsent(0); this.lastCard = null; logger.log(Level.INFO, "Card removed"); for (TerminalListener listener : this.listenersTerminal) listener.cardRemoved(); } catch (Exception exception) { logger.log(Level.WARNING, "", exception); } } }
/** * openChannel * * @param applet * @return Channel * @throws Exception */ private CardChannel openChannel(AppletModel applet) throws Exception { if (channel != null) { try { channel.close(); } catch (Exception e) { // } channel = null; } if (card != null) { try { card.disconnect(true); } catch (Exception e) { // } card = null; } TerminalFactory factory = TerminalFactory.getDefault(); CardTerminals cardterminals = factory.terminals(); try { List<CardTerminal> terminals = cardterminals.list(); System.out.println("Terminals: " + terminals); for (CardTerminal terminal : terminals) { terminal.waitForCardPresent(1000); if (terminal.isCardPresent()) { System.out.println(terminal.getName() + ": Card present!"); card = terminal.connect("*"); channel = card.getBasicChannel(); return channel; } } throw new WolfException(MSG_READER_TIME_OUT); } catch (Exception e) { throw e; } }