我正在尝试从 NFC 标签读取数据。如果我使用 Play Store 中的“NfcV-reader”应用程序(由标签制造商 ST Microelectronics 编写),那么我可以确认标签包含带有 MIME 数据的 NDEF 记录。MIME 类型为“application/myapp”,MIME 有效负载为“0123”,如下所示:
我希望我自己的应用程序在这个标签被 Android 识别时启动。我用我认为正确的意图过滤器创建了应用程序。这是 AndroidManifest 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),将设备设置为主屏幕并使用字符串“nfc”的过滤器启动 logcat。这是我得到的:
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.nfc.action.NDEF_DISCOVERED”时,为什么 Android 会调度“android.nfc.action.TECH_DISCOVERED”?
编辑 1 (在收到下面的第一条评论后添加更多信息)
我使用了另一个应用程序“NFC TagInfo”来验证标签的内容。扫描的标签在 NFC TagInfo 中显示如下:
最后是向 Android 设备展示标签的 logcat:
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)
问题出在意图过滤器上。请参阅下面接受的答案。这是现已修复的问题的 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)