如何擦除android中的NFC标签数据?

时间:2014-10-14 15:41:40

标签: android nfc nfc-p2p

当我尝试擦除NFC标签中的数据时,我遇到了问题。我正在使用MifareUltralight型NFC标签。请有人帮我找到解决方案。这是我的代码。我从here

获得了此代码
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
    mytag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}

NdefFormatable formatable = NdefFormatable.get(mytag);

if (formatable != null) { // Here I'm getting formatable null
    try {
        formatable.connect();

        try {
            formatable.format(methodGetMsg());
        } catch (Exception e) {
            // let the user know the tag refused to format
        }
    } catch (Exception e) {
        // let the user know the tag refused to connect
    } finally {
        try {
            formatable.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} else {
    // let the user know the tag cannot be formatted
}

3 个答案:

答案 0 :(得分:3)

我认为您可能无法理解标签上NDEF消息上下文中的格式化含义。确切的方法因标签而异,但在一天结束时格式化' NDEF的标签只包括将NDEF幻数和一些元数据放在某个位置。

对于MIFARE Ultralight,幻数存放在一次性可编程存储器中,因此在您已经格式化后,实际上无法为NDEF格式化Ultralight,尽管您假设标签未锁定,可以向其写入不同的NDEF消息。一般来说,Android设备似乎认识到这一点,并且不允许您将NdefFormatable与已经格式化的Ultralights一起使用。对于其他标签,如果NDEF格式的标签将枚举NdefFormatable,即使它是未锁定的Topaz或其他可以再次写入格式化位的标签,它也会高度不一致。

如果您想用Android覆盖NDEF数据,最可靠的选择是使用Ndef覆盖现有的Ndef消息。

答案 1 :(得分:3)

由于您的标记已经列出了android.nfc.tech.Ndef技术,因此它已准备好存储NDEF消息,不需要进一步格式化。您可以使用Ndef对象的writeNdefMessage()方法简单地覆盖(假设标记不是只读的)任何现有的NDEF消息。例如。要将标签“格式化”为空的NDEF消息,您可以执行以下操作:

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

答案来自here感谢Michael Roland

答案 2 :(得分:1)

可能是您的设备不支持MifareUltralight格式化。

开发人员文档中指出,没有强制性标记集,所有具有NFC功能的Android设备都必须支持NdefFormatable 1

这是因为标签格式化使其与NDEF兼容通常需要专有知识2

相关问题