简体   繁体   English

如何从 NFC 标签中删除 NDEF 消息?

[英]How to erase NDEF messages from NFC tag?

I am working on NFC.我正在研究 NFC。 I am able to write message on tag.我可以在标签上写消息。 Everything is working fine but when I try to erase the tag, it does not work and I don't know why.一切正常,但是当我尝试擦除标签时,它不起作用,我不知道为什么。 Here is my code:这是我的代码:

Tag mytag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefFormatable formatable = NdefFormatable.get(mytag);

if (formatable != null) {
        formatable.connect();
        formatable.format(methodGetMsg());
        formatable.close();
}

I'm always getting formatable value null .我总是得到可格式化的值null

As your tag already lists the android.nfc.tech.Ndef technology, it is already prepared to store an NDEF message and does not need further formatting.由于您的标签已经列出了 android.nfc.tech.Ndef 技术,它已经准备好存储 NDEF 消息并且不需要进一步格式化。 You can simply overwrite (given that the tag is not read-only) any existing NDEF message by using the writeNdefMessage() method of the Ndef object.您可以使用 Ndef 对象的 writeNdefMessage() 方法简单地覆盖(假设标签不是只读的)任何现有的 NDEF 消息。 Eg to "format" the tag to an empty NDEF message, you could do something like:例如,要将标签“格式化”为空 NDEF 消息,您可以执行以下操作:

Ndef ndefTag = Ndef.get(tag);
ndefTag.writeNdefMessage(new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)));

Taken from here取自这里

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

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