Android NFC:接收 TECH_DISCOVERED,期待 NDEF_DISCOVERED

Android NFC: Receiving TECH_DISCOVERED, expecting NDEF_DISCOVERED

我正在尝试从 NFC 标签读取数据。如果我使用 Play Store 中的 "NfcV-reader" 应用程序(由标签制造商 ST Microelectronics 编写),那么我可以确认该标签包含带有 MIME 数据的 NDEF 记录。 MIME 类型为 "application/myapp",MIME 负载为“0123”,如下所示:

我希望我自己的应用程序在 Android 识别此标签时启动。我用我认为正确的意图过滤器创建了应用程序。这是 Android清单 XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dummy.nfc.reader">

    <uses-permission android:name="android.permission.NFC" />
    <uses-feature android:name="android.hardware.nfc" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".ActivityNfcReader">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.ACTION_NDEF_DISCOVERED"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="application/myapp"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

我已将该应用程序下载到 Nexus 6P 设备(Android 版本 6.0.1),将设备设置为主屏幕并启动 logcat 过滤字符串 "nfc"。这是我得到的:

04-11 16:23:33.108  4050  4050 D NativeNfcTag: Connect to a tech with a different handle
04-11 16:23:33.273  4050  3909 D NativeNfcTag: Starting background presence check
04-11 16:23:33.278   918  4148 I ActivityManager: START u0 {flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras)} from uid 1027 on display 0
04-11 16:23:33.343   918  3469 I ActivityManager: START u0 {act=android.nfc.action.TECH_DISCOVERED cmp=com.google.android.tag/com.android.apps.tag.TagViewer (has extras)} from uid 1027 on display 0
04-11 16:23:35.430  4050  3909 D NativeNfcTag: Tag lost, restarting polling loop
04-11 16:23:35.432  4050  3909 D NfcService: Discovery configuration equal, not updating.
04-11 16:23:35.433  4050  3909 D NativeNfcTag: Stopping background presence check

我的问题是:为什么 Android 在我期待 "android.nfc.action.NDEF_DISCOVERED" 时调度 "android.nfc.action.TECH_DISCOVERED"?

编辑 1 (在收到下面第一条评论后添加更多信息)

我使用了另一个应用程序 "NFC TagInfo" 来验证标签的内容。扫描的标签在 NFC TagInfo 中显示如下:

最后是 logcat 将标签呈现给 Android 设备:

04-12 09:30:48.609  3829  3829 D NativeNfcTag: Connect to a tech with a different handle
04-12 09:30:48.775  3829 10744 D NativeNfcTag: Starting background presence check
04-12 09:30:50.661   917  6053 I ActivityManager: START u0 {cmp=at.mroland.android.apps.nfctaginfo/.TagViewer (has extras)} from uid 10103 on display 0
04-12 09:30:50.834   917   935 I ActivityManager: Displayed at.mroland.android.apps.nfctaginfo/.TagViewer: +160ms

编辑 2 (用接受的答案解决问题后添加正确的logcat)

问题出在 intent 过滤器上。请参阅下面接受的答案。这是 logcat 问题现已修复的行为:

04-12 12:14:48.237  3829  3829 D NativeNfcTag: Connect to a tech with a different handle
04-12 12:14:49.371  3829  4052 E BrcmNfcNfa: rw_i93_process_timeout (): retry_count = 1
04-12 12:14:49.422  3829 14236 D NativeNfcTag: Starting background presence check
04-12 12:14:49.424   917  4427 I ActivityManager: START u0 {flg=0x10008000 cmp=com.android.nfc/.NfcRootActivity (has extras)} from uid 1027 on display 0
04-12 12:14:49.457   917  6053 I ActivityManager: START u0 {act=android.nfc.action.NDEF_DISCOVERED typ=application/myapp cmp=com.dummy.nfc.reader/.ActivityNfcReader (has extras)} from uid 1027 on display 0
04-12 12:14:49.553   917   935 I ActivityManager: Displayed com.dummy.nfc.reader/.ActivityNfcReader: +91ms (total +106ms)

您对 NDEF 消息的意图过滤器有误。您目前有

<intent-filter>
    <action android:name="android.intent.action.ACTION_NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="application/myapp"/>
</intent-filter>

虽然 MIME 类型与您标签上的 MIME 类型匹配,但意图操作不正确。发现 NDEF 消息的正确操作是 "android.nfc.action.NDEF_DISCOVERED"。因此,您需要将您的意图过滤器更改为:

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