我正在用Java开发一个应用程序,我想在其中发送NDEF消息中的URL,然后在移动设备上打开该消息。我能够做到这一点,通过与Mifare Ultralight的HCE卡仿真模式的网址,例如"http://www.google.com“,因为这适合在模拟卡的内存,如果我正确理解是52字节,但我想发送一个更长的网址(大约350-550字节),这可能是不可能与acr1252u's在这个设备上。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;
}
}发布于 2020-11-21 05:17:28
对等Nfc模式不是解决问题的方法。
iOS不支持Nfc点对点,因此您将无法向iPhones发送数据。
Android确实支持Nfc Peer to Peer (称为Android光束),但它已经被弃用,并从Android 10开始删除,其中一个原因是它太不可靠了,所以即使发送少量数据也是如此。
https://stackoverflow.com/questions/64931100
复制相似问题