【发布时间】:2015-12-17 19:24:54
【问题描述】:
我目前正在为学校制作的项目中遇到 NFC 问题。 这是我第一次尝试在应用程序中使用 NFC,因此可能无法正确理解它的工作原理。
问题是过去一周我们一直在 HTC One M8 上测试应用程序,我们的应用程序在读取我们芯片上的字符串时没有问题。
但是,当我现在尝试使用家庭成员的 LG Nexus 5 时,字符串不会传递给应用程序。
代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
//NFC Adapter
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
}
@Override
protected void onResume() {
super.onResume();
enableForegroundDispatchSystem();
}
@Override
protected void onPause() {
super.onPause();
disableForegroundDispatchSystem();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent.hasExtra(NfcAdapter.EXTRA_TAG)) {
Toast.makeText(this, "NfcIntent!", Toast.LENGTH_SHORT).show();
Parcelable[] parcelables = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (parcelables != null && parcelables.length > 0) {
// this part gets executed on the HTC Once m8
readTextFromMessage((NdefMessage) parcelables[0]);
} else {
// this part gets executed on the LG Nexus 5
Toast.makeText(this, "No NDEF messages found!", Toast.LENGTH_SHORT).show();
}
}
}
private void readTextFromMessage(NdefMessage ndefMessage) {
NdefRecord[] ndefRecords = ndefMessage.getRecords();
if (ndefRecords != null && ndefRecords.length>0) {
NdefRecord ndefRecord = ndefRecords[0];
String tagContent = getTextFromNdefRecord(ndefRecord);
if (tagContent.toString().equals("Kasse")) {
//scanBtn.setVisibility(View.VISIBLE);
} else {
//addItem(tagContent);
//cartArrayDisplay.addView(nameList.get(cartAmount));
//priceArrayDisplay.addView(priceList.get(cartAmount));
//cartAmount++;
}
} else {
Toast.makeText(this, "No NDEF records found!", Toast.LENGTH_SHORT).show();
}
}
private void enableForegroundDispatchSystem() {
Intent intent = new Intent(this, AppActivity.class).addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
IntentFilter[] intentFilters = new IntentFilter[]{};
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, null);
}
private void disableForegroundDispatchSystem() {
nfcAdapter.disableForegroundDispatch(this);
}
public String getTextFromNdefRecord(NdefRecord ndefRecord) {
String tagContent = null;
try {
byte[] payload = ndefRecord.getPayload();
String textEncoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
int languageSize = payload[0] & 0063;
tagContent = new String(payload, languageSize + 1,
payload.length - languageSize - 1, textEncoding);
} catch (UnsupportedEncodingException e) {
Log.e("getTextFromNdefRecord", e.getMessage(), e);
}
return tagContent;
}
在 HTC One m8 上运行应用程序时,它始终会运行此部分:
if (parcelables != null && parcelables.length > 0) {
readTextFromMessage((NdefMessage) parcelables[0]);
但是,当在 LG Nexus 5 上运行该应用程序时,它会运行以下部分:
} else {
Toast.makeText(this, "No NDEF messages found!", Toast.LENGTH_SHORT).show();
有人告诉我,这很可能是因为我们使用的标签与所有设备不兼容。但是,我认为它也可以是编程方式,因为我没有使用 Android 和 NFC 的经验。
标签是学生证。标签阅读器应用程序将其中一个识别为
- 标签类型:Mifare Classic 1k
- NUID[4]:dbc3f12d
- ATQA:0004
- SAK:08
- 目标技术:android.nfc.tech.NfcA
【问题讨论】:
-
为了猜测可能出现的问题,您需要向我们展示您如何将消息写入标签以及您使用的标签类型。更好的是,使用用于分析 NFC 标签的可用应用程序之一(例如 NFC TagInfo)扫描标签,并向我们展示它显示的关于标签类型和存储在其上的 NDEF 消息的内容。
-
我刚做了。标签类型是 Mifare Classic 1k。还有其他信息如 NUID[4] = dbc3f12d, ATQA = 0004, SAK = 08, Target technology = android.nfc.tech.NfcA
标签: android nfc mifare ndef nexus-5