从acr1252u向ios/android设备发送长ndef消息

bvk5enib  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(410)

我正在用java开发一个应用程序,我想在ndef消息中发送一个url,然后在移动设备上打开。例如,我可以通过acr1252u的卡模拟模式和mifare ultralight来实现这一点。”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;
        }
}
0sgqnhkj

0sgqnhkj1#

点对点nfc模式不是答案。
ios不支持nfc点对点,因此您将无法向iPhone发送数据。
android确实支持nfc点对点(称为android beam),但它已经被弃用,并从android 10开始被删除,删除它的原因之一是它太不可靠了,所以即使发送少量的数据。

相关问题