jnasmartcardio.tools.WaitForChange Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jnasmartcardio Show documentation
Show all versions of jnasmartcardio Show documentation
A re-implementation of the javax.smartcardio API. It allows you to communicate to a smart card (at the APDU level) from within Java.
package jnasmartcardio.tools;
import java.security.Security;
import java.util.ArrayList;
import java.util.List;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CardTerminals;
import javax.smartcardio.CardTerminals.State;
import javax.smartcardio.TerminalFactory;
import jnasmartcardio.Smartcardio;
/**
* Simple utility to demonstrate {@link CardTerminals#waitForChange()} and
* {@link CardTerminals#list(State))}.
*/
public class WaitForChange {
private static List terminalNames(List terminals) {
ArrayList r = new ArrayList(terminals.size());
for (int i = 0; i < terminals.size(); i++)
r.add(terminals.get(i).getName());
return r;
}
public static void main(String[] args) throws Exception {
TerminalFactory context;
CardTerminals terminals;
if (true) {
Security.addProvider(new Smartcardio());
context = TerminalFactory.getInstance("PC/SC", null, Smartcardio.PROVIDER_NAME);
terminals = context.terminals();
} else {
TerminalFactory terminalFactory = TerminalFactory.getDefault();
terminals = terminalFactory.terminals();
}
List present = terminals.list(State.CARD_PRESENT);
List absent = terminals.list(State.CARD_ABSENT);
System.out.format("Initial: cards are present in %s; cards are absent from %s%n", terminalNames(present), terminalNames(absent));
while (true) {
terminals.waitForChange();
List inserted = terminals.list(State.CARD_INSERTION);
List removed = terminals.list(State.CARD_REMOVAL);
System.out.format("Card inserted in %s; cards removed from %s%n", terminalNames(inserted), terminalNames(removed));
}
}
}