Skip to content

Instantly share code, notes, and snippets.

@eliezio
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliezio/cc35c26e8e1b212bfc6c to your computer and use it in GitHub Desktop.
Save eliezio/cc35c26e8e1b212bfc6c to your computer and use it in GitHub Desktop.
Detecta leitora RC700 e cartao
package com.riocard.exemplos;
import javax.smartcardio.*;
import java.util.List;
public class TestaRC700 {
private static final int WAIT_FOR_CARD_TIMEOUT = 30000;
private static final int CLA_MIFARE = 0xFF;
private static final int INS_GET_DATA = 0xCA;
private static final CommandAPDU GET_CARD_UID = new CommandAPDU(CLA_MIFARE, INS_GET_DATA, 0x00, 0x00, 256);
public static void main(String[] args) throws Exception {
final CardTerminal rc700 = findRC700();
if (rc700 == null) {
System.err.println("Nenhuma leitora RC700 foi encontrada!");
return;
}
System.out.println("Aguardando cartao...");
if (!rc700.waitForCardPresent(WAIT_FOR_CARD_TIMEOUT)) {
System.err.println("Nenhum cartao detectado!");
return;
}
final Card card = rc700.connect("T=1");
if (card == null) {
System.err.println("Erro ao conectar no cartao!");
return;
}
final ATR atr = card.getATR();
if (atr != null) {
System.out.printf("ATR : %s%n", toHex(atr.getBytes()));
} else {
System.err.println("Nao conseguiu obter ATR");
return;
}
card.beginExclusive();
try {
final CardChannel channel = card.getBasicChannel();
final ResponseAPDU response = channel.transmit(GET_CARD_UID);
if (response == null) {
System.err.println("Nenhuma resposta ao tentar obter o UID do cartao");
return;
}
System.out.printf("SW : 0x%04X%n", response.getSW());
System.out.printf("UID : %s%n", toHex(response.getData()));
} finally {
card.endExclusive();
}
}
private static CardTerminal findRC700() throws CardException {
List<CardTerminal> terminalList = TerminalFactory.getDefault().terminals().list();
int i = 0;
CardTerminal rc700 = null;
for (CardTerminal terminal : terminalList) {
System.out.printf("Terminal #" + ++i + ": %s%n", terminal.getName());
if ((rc700 == null) && terminal.getName().contains("RC700")) {
rc700 = terminal;
System.out.printf("Selecionando leitora %s%n", terminal.getName());
}
}
return rc700;
}
private static final char[] DIGITS_LOWER = new char[]{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
private static String toHex(byte[] data) {
char[] out = new char[data.length * 3];
int i = 0;
for (int j = 0; i < data.length; ++i) {
out[j++] = DIGITS_LOWER[(data[i] & 0xf0) >>> 4];
out[j++] = DIGITS_LOWER[(data[i] & 0x0f)];
out[j++] = ' ';
}
return new String(out);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment