【问题标题】:Running NFC app on different devices results in inconsistency parsing EXTRA_NDEF_MESSAGE在不同设备上运行 NFC 应用程序导致解析不一致 EXTRA_NDEF_MESSAGE
【发布时间】: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


【解决方案1】:

您遇到的问题是由于您设备中的 NFC 芯片组与 MIFARE Classic 不兼容。通常,只有配备 NXP NFC 芯片组的设备才能读取 MIFARE Classic(NXP 是 MIFARE 技术的所有者)。

MIFARE Classic 使用 ISO/IEC 14443 A 类(以及 ISO/IEC 18092)中定义的防冲突和激活机制。因此,该部分与其他非接触式智能卡和 NFC 标签的工作方式相匹配。因此,all(大多数!)Android NFC 设备将检测到此类卡片/标签。但是,通信协议和框架与为其他 NFC 标签和非接触式智能卡定义的不同。这种非标准协议在 NXP 以外的其他芯片组上不可用,因此无法从配备非 NXP NFC 控制器的设备上的 MIFARE Classic 卡中读取数据。

HTC One m8 包含 NXP 的 PN544 NFC 控制器,因此可以发现您的标签并从中读取数据。 LG Nexus 5 包含 Broadcom 的 BCM20793 NFC 控制器,因此可以发现您的标签,但无法从中读取数据。

【讨论】:

    【解决方案2】:

    除了迈克尔所说的,您可以检查您的设备是否支持 Mifare Classic

    PackageManager.hasSystemFeature("com.nxp.mifare");
    

    【讨论】:

    • 请注意,尽管设备支持 MIFARE Classic,但仍有一些设备制造商“忘记”声明该系统功能。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 2021-11-16
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多