简体   繁体   English

从 acr1252u 向 iOS/Android 设备发送长 NDEF 消息

[英]Send long NDEF message from acr1252u to iOS/Android device

I'm developing an application in Java, where I want to send a URL in an NDEF message, which then gets opened on a mobile device.我正在用 Java 开发一个应用程序,我想在其中发送一个 NDEF 消息中的 URL,然后在移动设备上打开它。 I was able to do this through the acr1252u's Card Emulation Mode with Mifare Ultralight for a URL for example "http://www.google.com" since this fits in the emulated card's memory, which if I understand correctly is 52 bytes, but I would like to send a much longer URL (around 350-550 bytes), which probably is not possible with HCE on this device.我能够通过带有 Mifare Ultralight 的acr1252u卡模拟模式来执行此操作,例如“http://www.google.com”的 URL,因为它适合模拟卡的内存,如果我理解正确的话,它是 52 个字节,但是我想发送一个更长的 URL(大约 350-550 字节),这在此设备上使用 HCE 可能是不可能的。 The acr1252 supports peer-to-peer comms as well, but not sure how I could use it to serve the task. acr1252 也支持点对点通信,但不确定如何使用它来完成任务。 Could anyone please point me in a direction that could help me in this persuit?任何人都可以请我指出一个可以帮助我进行这种追求的方向吗?

The code I used for shorter URL using HCE:我使用 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;
        }
}

Peer to Peer Nfc mode is not the answer.点对点 Nfc 模式不是答案。

iOS does not support Nfc peer to peer so you won't be able to send data to iPhones. iOS 不支持 Nfc 点对点,因此您将无法向 iPhone 发送数据。

Android did support Nfc Peer to Peer (Called Android Beam) but it has been deprecated and removed from Android 10 onwards, one of the reason it was removed was that is was so unreliable so send even small amounts of data. Android 确实支持 Nfc Peer to Peer(称为 Android Beam),但它已被弃用并从 Android 10 开始被移除,它被移除的原因之一是它非常不可靠,因此即使发送少量数据也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM