使用低级NFC通信方法将NDEF数据写入NTAG216标记 [英] Writing NDEF data to NTAG216 tag using low-level NFC communication methods

查看:478
本文介绍了使用低级NFC通信方法将NDEF数据写入NTAG216标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过低级通信方法实现了与NTAG216交互的代码( NTAG212 Mifare Ultralight with Authentication 和NTAG216的数据表。。

I have implemented the code to interact with an NTAG216 by means of low-level communication methods (following NTAG212 Mifare Ultralight with Authentication and the datasheet of NTAG216).


  • 如果未设置,则设置NTAG216上的密码写保护或新建/空白标记。

  • 如果已设置密码,请使用PWD_AUTH进行身份验证并比较PACK。

  • 读取数据。

  • 写入/覆盖数据。

  • Set password write protection on NTAG216 if not set or if new/blank tag.
  • If password is already set, authenticate using PWD_AUTH and comparing PACK.
  • Read data.
  • Write/overwrite data.

  • 检测我在其他应用中写入标签的NDEF消息。换句话说,我可以使用 writePage()方法写入标签,并使用 readPage()。但是,在编写页面时,我将 NdefMessage 转换为一个可以读取和写入的字节数组。但是,在其他应用程序中未检测到此NDEF消息。

  • Detect NDEF messages that I write to the tag in other apps. In other words I'm able to write to the tag using writePage() method and also read tags using readPage(). However, on time of writing pages I convert an NdefMessage into a byte array which can be read as well as written. However, this NDEF message is not detected in other applications.

为了能够检测NDEF消息,我需要做什么我是从其他应用程序写的吗?

What do I need to do in order to be able to detect NDEF messages I write from other applications?

推荐答案

NTAG216是一个NFC论坛类型2标签。因此,您必须关注NFC论坛 Type 2 Tag将数据写入此类NFC标签时的操作规范

NTAG216 is an NFC Forum Type 2 tag. Consequently, you have to follow the NFC Forum Type 2 Tag Operation specification when writing data to this type of NFC tag.

因此,您需要遵循一些规则才能将标签作为NDEF标签发现(类型2标记):

Therefore, you will need to follow a few rules for a tag to be discoverable as NDEF tag (Type 2 tag):

首先,需要配置功能容器(位于块3中)。

First, the capability container (located in block 3) needs to be configured.


  • 字节0必须设置为魔术值 0xE1

  • 字节1必须是设置为 0x10 以指示映射版本1.0。

  • 字节2必须设置为 0x6D 表示NTAG216的内存大小。

  • 字节3可以设置为 0x00 ,表示对...的读/写访问权限NDEF数据或 0x0F 表示只读访问权限(请注意这些是应用程序层的权限)。

  • Byte 0 must be set to the "magic" value 0xE1.
  • Byte 1 must be set to 0x10 to indicate mapping version 1.0.
  • Byte 2 must be set to 0x6D to indicate the memory size of NTAG216.
  • Byte 3 can be set to 0x00 to indicate read/write access to the NDEF data or to 0x0F to indicate read-only access (note that these are permissions on the application layer).

所以你可以写出能力公司ntainer as:

So you can write the capability container as:

byte[] response = nfc.transceive(new byte[] {
    (byte)0xA2, // WRITE
    (byte)3,    // block address
    (byte)0xE1, (byte)0x10, (byte)0x6D, (byte)0x00
});

NTAG216已附带正确配置的功能容器,因此无需手动执行此操作。另请注意,块3是一次性可编程的,这意味着位只能设置为1,但不能再次清零。因此,如果您已使用不同的值覆盖功能容器,则该标记很可能不再用作NDEF标记。

NTAG216 already ships with a properly configured capability container so there is no need to do this by hand. Also note that block 3 is one-time-programmable, which means that bits can only be set to one but cannot be cleared to zero again. So if you did already overwrite the capability container with a different value, the tag can most likely no longer be used as an NDEF tag.

必须将数据写入从块4开始的数据块。必须将NDEF消息包装成NDEF消息TLV(tag-length-value)结构。此TLV的标记是 0x03 。长度可以是单字节格式(对于长度在0到254字节之间的NDEF消息)或三字节格式(对于长度为255或更多字节的NDEF消息)。此TLV块的数据是实际的NDEF消息(您可以从 ndefMessage.toByteArray()获得。)

The data must be written to the data blocks starting at block 4. NDEF messages must be wrapped into an NDEF message TLV (tag-length-value) structure. The tag for this TLV is 0x03. The length can be either in one-byte format (for NDEF messages with a length between 0 and 254 bytes) or in three-byte format (for NDEF messages with a length of 255 or more bytes). The data of this TLV block is the actual NDEF message (that you can obtain from ndefMessage.toByteArray()).

例如,对于NDEF消息 D1 01 0C 55 01 65 78 61 6D 70 6C 65 2E 63 6F 6D 2F (这是一个带有URL<的URI记录a href =http://www.example.com/ =nofollow noreferrer> http://www.example.com/ ),您将获得以下TLV结构:

For example, for the NDEF message D1 01 0C 55 01 65 78 61 6D 70 6C 65 2E 63 6F 6D 2F (which is a URI record with the URL "http://www.example.com/"), you would get the following TLV structure:


03 11 D1010C55016578616D706C652E636F6D2F

如果您有更长的NDEF消息(例如一个259字节),你将使用三字节长度格式:

If you have alonger NDEF message (e.g. one with 259 bytes), you would use the three-byte length format:


03 FF0103 D101FF5501...

此外,您应该使用Terminator TLV标记标签上数据的结尾(标记 0xFE ,没有长度和数据字段):

Further, you should mark the end of the data on the tag with a Terminator TLV (tag 0xFE, no length and data fields):


FE

然后您可以将此数据写入标签:

You could then write this data to the tag as:

byte[] ndefMessage = new byte[] {
    (byte)0xD1, (byte)0x01, (byte)0x0C, (byte)0x55, (byte)0x01, (byte)0x65, (byte)0x78, (byte)0x61, (byte)0x6D, (byte)0x70, (byte)0x6C, (byte)0x65, (byte)0x2E, (byte)0x63, (byte)0x6F, (byte)0x6D, (byte)0x2F
};

// wrap into TLV structure
byte[] tlvEncodedData = null;
if (ndefMessage.length < 255) {
    tlvEncodedData = new byte[ndefMessage.length + 3];
    tlvEncodedData[0] = (byte)0x03;  // NDEF TLV tag
    tlvEncodedData[1] = (byte)(ndefMessage.length & 0x0FF);  // NDEF TLV length (1 byte)
    System.arraycopy(ndefMessage, 0, tlvEncodedData, 2, ndefMessage.length);
    tlvEncodedData[2 + ndefMessage.length] = (byte)0xFE;  // Terminator TLV tag
} else {
    tlvEncodedData = new byte[ndefMessage.length + 5];
    tlvEncodedData[0] = (byte)0x03;  // NDEF TLV tag
    tlvEncodedData[1] = (byte)0xFF;  // NDEF TLV length (3 byte, marker)
    tlvEncodedData[2] = (byte)((ndefMessage.length >>> 8) & 0x0FF);  // NDEF TLV length (3 byte, hi)
    tlvEncodedData[3] = (byte)(ndefMessage.length & 0x0FF);          // NDEF TLV length (3 byte, lo)
    System.arraycopy(ndefMessage, 0, tlvEncodedData, 4, ndefMessage.length);
    tlvEncodedData[4 + ndefMessage.length] = (byte)0xFE;  // Terminator TLV tag
}

// fill up with zeros to block boundary:
tlvEncodedData = Arrays.copyOf(tlvEncodedData, (tlvEncodedData.length / 4 + 1) * 4);
for (int i = 0; i < tlvEncodedData.length; i += 4) {
    byte[] command = new byte[] {
        (byte)0xA2, // WRITE
        (byte)((4 + i / 4) & 0x0FF), // block address
        0, 0, 0, 0
    };
    System.arraycopy(tlvEncodedData, i, command, 2, 4);
    byte[] response = nfc.transceive(command);
}

最后,请注意您不能将标签用作NDEF标签您在NDEF数据区域设置了读取密码,因为NFC论坛类型2标签操作规范要求标签可以自由读取。

Finally, be aware that you can't use the tag as NDEF tag if you have read-password set on the NDEF data area since the NFC Forum Type 2 Tag Operation specification requires the tag to be freely readable.

这篇关于使用低级NFC通信方法将NDEF数据写入NTAG216标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆