如何创建擦除NFC标签的NDEF消息 [英] How to create an NDEF message that erases a NFC tag

查看:809
本文介绍了如何创建擦除NFC标签的NDEF消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建NFC应用程序.我是Android的菜鸟,这是我的第二个应用程序.我想知道是否有人知道如何创建擦除NFC标签的消息.在诸如TagWriter之类的应用程序中可以看到此功能.

I am creating an NFC application. I am a noob at Android this is my 2nd application. I was wondering if anyone knows how to create a message that erases the NFC tag. This function is seen in applications like TagWriter.

推荐答案

您到底想实现什么? NDEF消息是存储在NFC标签上的数据包,因此擦除"与创建NDEF消息"有些矛盾.但是,您可以做的是使用一条空的NDEF记录创建NDEF消息:

What exactly do you want to achieve? An NDEF message is a data packet that you store on an NFC tag, so "erasing" somewhat contradicts "creating an NDEF message". However, what you can do is create an NDEF message with a single empty NDEF record:

NdefMessage msg = new NdefMessage(new NdefRecord[] {
    new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)
});
ndefTag.writeNdefMessage(msg);

但是请记住,将此消息写入标签将隐藏并部分覆盖标签上所有先前存在的NDEF消息,但是不会会擦除标签上的所有数据.例如,如果标签上的现有消息比新消息长,则旧消息数据的某些部分可能仍保留在标签上.

But keep in mind that writing this message to the tag will hide and partially overwrite any pre-existing NDEF messages on the tag, however it will not erase all data on the tag. For instance if a pre-existing message on the tag was longer than the new message, some parts of the old message's data may still remain stored on the tag.

因此,如果您想覆盖标签上的所有数据,则可能需要创建一个虚拟记录,其中包含的数据量可以存储在标签上(您可以使用Ndef.getMaxSize()确定NDEF消息的最大大小,但在创建记录时必须考虑标头字节).写入该伪记录后,您可以如上所述再次写入空的NDEF消息.

So if you want to overwrite all data on the tag you might want to create a dummy record that contains as much data as can be stored on the tag (you can determine the maximum size of an NDEF message using Ndef.getMaxSize(), but you have to account for header bytes when creating your record). After writing that dummy record, you can again write the empty NDEF message as described above.

NdefMessage msg = new NdefMessage(new NdefRecord[] {
    new NdefRecord(NdefRecord.TNF_UNKNOWN, null, null,
                   new byte[ndefTag.getMaxSize() - messageOverhead])
});
ndefTag.writeNdefMessage(msg);

这篇关于如何创建擦除NFC标签的NDEF消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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