Skip to content

Instantly share code, notes, and snippets.

@cameronjacobson
Last active August 29, 2015 13:56
Show Gist options
  • Save cameronjacobson/8896166 to your computer and use it in GitHub Desktop.
Save cameronjacobson/8896166 to your computer and use it in GitHub Desktop.
Bare-bones code to interface with PCSCD / NFC readers
import java.util.List;
import javax.smartcardio.*;
// VARIATION OF:
// http://ludovicrousseau.blogspot.com/2010/06/pcsc-sample-in-java.html
// The included commands are specific to acr122u reader / writer for Mifare Classic 1k tags
public class pcsc {
public static void main(String[] args) {
try {
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
CardTerminal terminal = terminals.get(0);
if(terminal.waitForCardPresent(5000)){
Card card = terminal.connect("*");
CardChannel channel = card.getBasicChannel();
// AUTH
byte[] blah = {(byte)0xff, (byte)0x86, 0x00, 0x00, 0x05, 0x01, 0x00, 0x04, 0x60, 0x00};
ResponseAPDU answer = channel.transmit(new CommandAPDU(blah));
/*
// WRITE
byte[] blah2 = {(byte)0xFF, (byte)0xD6, 0x00, 0x04, 0x10, 0x09, 0x03, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, (byte)0x0E, 0x01};
answer = channel.transmit(new CommandAPDU(blah2));
*/
// READ
byte[] blah3 = {(byte)0xff, (byte)0xB0, 0x00, 0x04, 0x10};
answer = channel.transmit(new CommandAPDU(blah3));
System.out.println("answer: " + answer.toString());
byte r[] = answer.getData();
//System.out.print(r);
for(int i=0;i<r.length;i++){
System.out.print(String.valueOf(r[i])+" ");
}
System.out.println();
// Disconnect the card
card.disconnect(false);
}
} catch(Exception e) {
System.out.println("Ouch: " + e.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment