[英]How to Read NFC Tag?
Hi I am trying to read from NFC Tag.嗨,我正在尝试读取 NFC 标签。 But I am getting an exception.
但我得到了一个例外。
I have put this condition to detect the tag?我已经把这个条件来检测标签?
if(NfcAdapter.ACTION_TAG_DISCOVERED != null)
Whether this condition is correct?这个条件是否正确?
First of all you have to initialize the NFC adapter and define Pending Intent in onCreate callback:首先,您必须初始化 NFC 适配器并在 onCreate 回调中定义 Pending Intent:
NfcAdapter mAdapter;
PendingIntent mPendingIntent;
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
//nfc not support your device.
return;
}
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
In onResume() Call back enable the Foreground Dispatch to detect NFC intent.在 onResume() 回调中启用前台调度以检测 NFC 意图。
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
In onPause() callback you have to disable the forground dispatch:在 onPause() 回调中,您必须禁用前台调度:
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
}
In onNewIntent() call back method you will get the new Nfc Intent.在 onNewIntent() 回调方法中,您将获得新的 Nfc Intent。 After getting The Intent , you have to parse the intent to detect the card:
获得 The Intent 后,您必须解析 Intent 以检测卡片:
@Override
protected void onNewIntent(Intent intent) {
getTagInfo(intent)
}
private void getTagInfo(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String[] techList = tag.getTechList();
for (int i = 0; i<techList.length; i++) {
if (techList[i].equals(MifareClassic.class.getName())) {
MifareClassic mifareClassicTag = MifareClassic.get(tag);
switch (mifareClassicTag.getType()) {
case MifareClassic.TYPE_CLASSIC:
//Type Clssic
break;
case MifareClassic.TYPE_PLUS:
//Type Plus
break;
case MifareClassic.TYPE_PRO:
//Type Pro
break;
}
} else if (techList[i].equals(MifareUltralight.class.getName())) {
//For Mifare Ultralight
MifareUltralight mifareUlTag = MifareUltralight.get(tag);
switch (mifareUlTag.getType()) {
case MifareUltralight.TYPE_ULTRALIGHT:
break;
case MifareUltralight.TYPE_ULTRALIGHT_C:
break;
}
} else if (techList[i].equals(IsoDep.class.getName())) {
// info[1] = "IsoDep";
IsoDep isoDepTag = IsoDep.get(tag);
} else if (techList[i].equals(Ndef.class.getName())) {
Ndef.get(tag);
} else if (techList[i].equals(NdefFormatable.class.getName())) {
NdefFormatable ndefFormatableTag = NdefFormatable.get(tag);
}
}
}
To answer you question about the code -回答你关于代码的问题 -
That will always be true - NfcAdapter.ACTION_TAG_DISCOVERED
is a constant value - you need to use:这将永远是正确的 -
NfcAdapter.ACTION_TAG_DISCOVERED
是一个常量值 - 你需要使用:
getIntent().getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)
to compare it.比较一下。
But, that probably has nothing to do with your exception -但是,这可能与您的例外无关-
try out below working code.试试下面的工作代码。
/**
* this method is used for read nfc data from tag.
*
* @param ndef Ndef
*/
private void readFromNFC(Ndef ndef) {
try {
ndef.connect();
NdefMessage ndefMessage = ndef.getNdefMessage();
NdefRecord[] e = ndefMessage.getRecords();
for (NdefRecord s : e) {
String message = new String(s.getPayload());
if (!message.equals("")) {
CustomLog.info(TAG, "readFromNFC: " + message);
mTvMessage.setText(message);
} else {
mTvMessage.setText("Tag is empty!");
}
}
ndef.close();
} catch (IOException | FormatException e) {
e.printStackTrace();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.