How to read detected NFC tag (NDEF content) details in android?

感情迁移 提交于 2019-12-29 06:29:50

问题


I want to read NDEF content contained within a detected NFC tag (i.e., Tag id, Tag Size, Tag Type, Is tag Writable, Target Type, and message types).


回答1:


I assume that you are talking about tags with NDEF content? In that case, you can do:

Tag myTag = (Tag) nfcintent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

// get NDEF tag details
Ndef ndefTag = Ndef.get(myTag);
int size = ndefTag.getMaxSize();         // tag size
boolean writable = ndefTag.isWritable(); // is tag writable?
String type = ndefTag.getType();         // tag type

// get NDEF message details
NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
NdefRecord[] ndefRecords = ndefMesg.getRecords();
int len = ndefRecords.length;
String[] recTypes = new String[len];     // will contain the NDEF record types
for (int i = 0; i < len; i++) {
  recTypes[i] = new String(ndefRecords[i].getType());
}


来源:https://stackoverflow.com/questions/9971820/how-to-read-detected-nfc-tag-ndef-content-details-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!