ฉันได้รวบรวมตัวอย่างโค้ดต่อไปนี้เพื่อตรวจจับสมาร์ทการ์ด Mifare โดยใช้ไลบรารี javax.smartcardio รหัสนี้ใช้งานได้ดีกับประเภท Mifare ที่ระบุไว้
public class DetectCardTester {
private static final String PROTOCOL = "T=1";
static Map<String,String> KNOWN_CARD_TYPES = ImmutableMap.<String, String>builder()
.put("00-01", "Mifare 1K")
.put("00-02", "Mifare 4K")
.put("00-03", "Mifare Ultralight")
.put("00-26", "Mifare Mini")
.build();
public static void main(String... args) {
try {
final TerminalFactory terminalFactory = SmartcardTerminalFactory.create();
System.out.println("Place card on the reader");
final Card card = awaitCard(terminalFactory, 3, SECONDS);
final ATR atr = card.getATR();
final byte[] bytes = atr.getBytes();
System.out.println("ATR=" + String.valueOf(Hex.encodeHex(bytes, false)));
if (bytes != null && bytes.length > 13) {
String typeCode = String.format("%02X-%02X", bytes[13], bytes[14]);
if (KNOWN_CARD_TYPES.containsKey(typeCode)) {
System.out.println("Known Type:" + KNOWN_CARD_TYPES.get(typeCode));
} else {
System.out.println("Unknown Type:" + typeCode);
}
}
// TODO: Detect Desfire Card
} catch (Throwable t) {
t.printStackTrace();
}
}
public static Card awaitCard(final TerminalFactory terminalFactory, final int qty, final TemporalUnit unit) throws CardException {
LocalDateTime timeout = now().plus(qty, unit);
while (now().isBefore(timeout)) {
Optional<CardTerminal> cardTerminal = getCardPresentTerminal(terminalFactory);
if (cardTerminal.isPresent()) {
return cardTerminal.get().connect(PROTOCOL);
}
}
throw new CardNotPresentException("Timed out waiting for card");
}
private static Optional<CardTerminal> getCardPresentTerminal(final TerminalFactory terminalFactory) throws CardException {
List<CardTerminal> terminals = terminalFactory.terminals().list();
for (CardTerminal cardTerminal : terminals) {
if (cardTerminal.isCardPresent()) {
return Optional.of(cardTerminal);
}
}
for (CardTerminal cardTerminal : terminals) {
// waitForCardPresent / Absent doesn't work with some Jacax.smartcard.io implementations
// i.e. OSX http://bugs.java.com/view_bug.do?bug_id=7195480
// This is why we have the imediate check above
if (cardTerminal.waitForCardPresent(250)) {
return Optional.of(cardTerminal);
}
}
return Optional.empty();
}
}
ฉันใช้ทรัพยากรต่อไปนี้เพื่อรวบรวมโค้ดนี้:
- http://downloads.acs.com.hk/drivers/en/API-ACR122U-2.02.pdf
- วิธีรับ SAK เพื่อระบุประเภทสมาร์ทการ์ดโดยใช้ จาวา?
ฉันต้องการใช้ความคิดเห็น TODO เพื่อตรวจจับการ์ด Desfire ถ้าฉันวางการ์ด Desfire ไว้บน read รหัสนี้เพิ่งส่งออก:
Place card on the reader
ATR=3B8180018080
ฉันพบคำถามนี้ กำหนดประเภทการ์ดจาก ATR ซึ่งช่วยได้นิดหน่อย แต่ฉันขาดอะไรบางอย่างไป . HistoricalByte สำหรับการ์ดที่ต้องการคือ 0x80 ซึ่งฉันไม่สามารถหาข้อมูลใดๆ ได้
ถ้าเป็นไปได้ ฉันขอขอบคุณตัวอย่างโค้ดข้างต้นที่ขยายออกไปเพื่อให้สามารถตรวจจับประเภทการ์ด Desfire ได้