【发布时间】:2023-03-19 17:33:01
【问题描述】:
我正在用 Java 开发一个应用程序,我想在其中发送一个 NDEF 消息中的 URL,然后在移动设备上打开该 URL。我可以通过 acr1252u's Card Emulation Mode 和 Mifare Ultralight 为 URL (例如“http://www.google.com”)做到这一点,因为它适合模拟卡的内存,如果我理解正确的话,它是 52 个字节,但我想发送一个更长的 URL(大约 350-550 个字节),这可能无法在此设备上使用 HCE。 acr1252 也支持点对点通信,但不确定如何使用它来完成任务。任何人都可以为我指出一个可以帮助我实现这一目标的方向吗?
我使用 HCE 用于较短 URL 的代码:
public static void main(String[] args) throws CardException {
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
CardTerminal terminal = terminals.get(0);
Card device = terminal.connect("DIRECT");
enterHostCardEmulation(device);
byte[] ndef = new byte[] {(byte) 0xE1, 0x10, 0x06, 0x00,
0x03, 0x0F, (byte) 0xD1, 0x01,
0x0B, 0x55, 0x01, 0x67,
0x6F, 0x6F, 0x67, 0x6C,
0x65, 0x2E, 0x63, 0x6F,
0x6D, (byte) 0xFE, 0x00, 0x00};
byte[] response = writeCardEmulationData(device, (byte) 0x01, (byte) 0x00, ndef);
System.out.println("response: " + byteArrayToHex(response));
}
private static byte[] writeCardEmulationData(Card device, byte nfcMode, byte startOffset, byte[] dataToWrite) {
byte[] command = new byte[9+dataToWrite.length];
command[0] = (byte) 0xE0; // Class
command[1] = 0x00; // INS
command[2] = 0x00; // P1
command[3] = 0x60; // P2
command[4] = (byte) (dataToWrite.length + 0x04); // Length + 4
command[5] = 0x01;
command[6] = nfcMode;
command[7] = startOffset;
command[8] = (byte) dataToWrite.length;
System.arraycopy(dataToWrite, 0, command, 9, dataToWrite.length);
System.out.println(byteArrayToHex(command));
try {
return device.transmitControlCommand(SCARD_CTL_CODE(3500), command);
} catch (CardException e) {
e.printStackTrace();
return null;
}
}
private static void enterHostCardEmulation(Card device) {
try {
System.out.println("NFC device: " + device);
byte[] hceCommand = new byte[] {(byte) 0xE0, 0x00, 0x00, 0x40, 0x03, 0x01, 0x00, 0x00};
byte[] hceResponse = device.transmitControlCommand(SCARD_CTL_CODE(3500), hceCommand);
System.out.println("enter HCE response: " + byteArrayToHex(hceResponse));
} catch (CardException e) {
e.printStackTrace();
}
}
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String byteArrayToHex(byte[] byteArr) {
char[] hexChars = new char[byteArr.length * 2];
for (int j = 0; j < byteArr.length; j++) {
int v = byteArr[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}
public static int SCARD_CTL_CODE(int command) {
boolean isWindows = System.getProperty("os.name").startsWith("Windows");
if (isWindows) {
return 0x00310000 | (command << 2);
} else {
return 0x42000000 | command;
}
}
【问题讨论】:
-
我相信读卡器也支持160字节数据的FeliCa Type 3 Card仿真。见acs.com.hk/download-manual/6402/API-ACR1252U-1.16.pdf
-
是的,没错。谢谢回复。我没有提到我也尝试过这种方法。不过,我的 URL 大约有 350-500 字节长。因此,对于我使用此读卡器的情况,Type 2 和 Type 3 卡仿真都不能令人满意。