当我尝试删除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
}
发布于 2014-10-15 05:27:15
我认为您可能不理解在NDEF消息的标签上下文中格式化意味着什么。确切的方法因标签而异,但最终“格式化”NDEF的标签只是将NDEF的魔术数字和一些元数据放在某个特定的位置。
对于MIFARE Ultralight,幻数放在一次性可编程内存中,因此在格式化Ultralight for NDEF之后,实际上不可能对其进行格式化,尽管您可以在假定标记未锁定的情况下向其写入不同的NDEF消息。一般来说,安卓设备似乎认识到了这一点,不会让你在已经格式化的NdefFormatable上使用超轻。至于其他标签,即使它是未锁定的黄玉或其他可以再次写入格式位的标签,如果NDEF格式化的标签将枚举NdefFormatable,这是非常不一致的。
如果你想用Android覆盖NDEF数据,你最可靠的选择是使用Ndef用新的Ndef消息覆盖现有的Ndef消息。
发布于 2014-10-15 18:05:01
因为您的标签已经列出了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
https://stackoverflow.com/questions/26364836
复制相似问题