我有一个 ACR122 NFC 阅读器并试图查看是否可以读取 Apple Pay 生成的令牌。下面是我正在尝试的一些 Java 代码,但每次只返回一个 4 个字母数字字符。Apple Pay iphone 感应到 nfc 阅读器 - 我需要发送特定的 apdu 命令来检索令牌吗?
import java.io.*;
import java.util.*;
import javax.smartcardio.*;
public class CardTest {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
int v;
for ( int j = 0; j < bytes.length; j++ ) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static void main(String[] args) throws Exception {
TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
System.out.println(factory);
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
if (terminals.isEmpty()) {
throw new Exception("No card terminals available");
}
CardTerminal terminal = terminals.get(0);
// Keep looping looking for cards until the application is closed
while( true )
{
terminal.waitForCardPresent( 0 );
try {
Card card = terminal.connect("*");
CardChannel channel = card.getBasicChannel();
CommandAPDU command = new CommandAPDU(new byte[]{(byte)0xFF,(byte)0xCA,(byte)0x00,(byte)0x00,(byte)0x04});
ResponseAPDU response = channel.transmit(command);
byte[] byteArray = response.getBytes();
System.out.println( bytesToHex( byteArray ) );
Thread.sleep(1000);
} catch (CardException e) {
e.printStackTrace();
}
}
}
}