Android NFC:接收TECH_DISCOVERED,期待NDEF_DISCOVERED

时间:2017-04-11 15:53:49

标签: android nfc intentfilter ndef iso-15693

我试图从NFC标签读取数据。如果我使用" NfcV-reader" Play商店的应用程序(由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),将设备设置为主屏幕,并使用字符串&#34; nfc&#34;的过滤器启动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调度&#34; android.nfc.action.TECH_DISCOVERED&#34;当我期待&#34; android.nfc.action.NDEF_DISCOVERED&#34;?

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

我已使用其他应用程序&#34; NFC TagInfo&#34;来验证代码的内容。扫描的标签在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)

问题出在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)

1 个答案:

答案 0 :(得分:1)

您对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>