【问题标题】:How to get plain text from NFC Tag?如何从 NFC 标签中获取纯文本?
【发布时间】:2021-02-26 05:26:00
【问题描述】:

我已尝试实现 Google 文档中的代码,但我仍然不清楚。我在这里错过了什么?

这是 MainActivity.java 中的方法:

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        Parcelable[] rawMessages =
                intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawMessages != null) {
            NdefMessage[] messages = new NdefMessage[rawMessages.length];
            for (int i = 0; i < rawMessages.length; i++) {
                messages[i] = (NdefMessage) rawMessages[i];
                Log.i("nfcccc",messages[i].toString());
            }
            // Process the messages array.
            Log.i("from tag: ",messages.toString());
            Toast.makeText(this,messages.toString(),Toast.LENGTH_LONG).show();

        }
    }
}

在 onCreated 方法中我还初始化了 nfcAdapter:

        nfcAdapter = NfcAdapter.getDefaultAdapter(this);

在清单文件中我有这个:

<activity android:name=".MainActivity">

        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <data android:mimeType="text/plain" />
        </intent-filter>


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>



    </activity>

我花了几天时间尝试实现它,但它仍然无法正常工作。 我正在做的是我使用 NFC Tools android 应用程序来保存纯文本,并且我想在我自己的应用程序中读取这些数据>

【问题讨论】:

    标签: android android-studio nfc reader


    【解决方案1】:

    您缺少的是与 Activity 的启动模式的交互,因为 NFC 处理是由另一个系统应用程序完成的,然后与 Google 文档中的如何使用 NFC 一样,就像您的应用程序正在从另一个应用程序接收数据一样。

    所以onNewIntent

    这对于在其包中将 launchMode 设置为“singleTop”的活动调用

    https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent)

    您的应用程序的默认启动模式为standard,因此永远不会调用onNewIntent

    将旧的 NFC API 与 Intents 结合使用有 2 个标准部分

    1. Activity 未运行(这通常意味着 App Task 未运行),在 Activity 中设置清单过滤器,然后在 oncreate 中使用 getIntent,然后将 Intent 处理为基于 NFC 的 Intent(根据您的需要继续onNewIntent)

    2. Activity(和应用程序)正在运行并且在前台,enableForegroundDispatchhttps://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc#foreground-dispatch 基本上告诉 NFC 系统应用程序在它是“Top”活动时重新启动这个 Activity,就好像它是一个“singleTop”活动一样,然后 onNewIntent 被传入的 Intent 调用(这有暂停和恢复您的活动的负面影响)

    请注意,有不同的方法可以设置 launchMode 以及堆栈顶部的 Activity 以使其绑定将 Intent 发送到 onNewIntent,但它们不太常见。
    最好在onCreateenableForegroundDispatch 中处理NFC Intents,在onNewIntent 中进行相同的处理

    获得更多控制权的更好方法是使用更新更好的enableReaderMode API https://developer.android.com/reference/android/nfc/NfcAdapter#enableReaderMode(android.app.Activity,%20android.nfc.NfcAdapter.ReaderCallback,%20int,%20android.os.Bundle)

    总结 所以只需按照https://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc#foreground-dispatch添加foregroundDispatch的代码

    【讨论】:

    • 非常感谢! :) 那行得通但是,我在保存的 String 之前收到了垃圾数据。例如:如果标签包含字符串“Hello”,我将得到“_ TenHello”。你知道可能是什么问题吗?也许我应该只提取字节数组的一部分,然后将其转换为字符串?
    • 文本记录是用语言编码的,看我的另一个答案stackoverflow.com/a/59515909/2373819
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多