2
votes

I'm trying to read data from an NFC tag. If I use the "NfcV-reader" application from Play Store (written by ST Microelectronics, the tag manufacturer) then I can confirm that the tag contains an NDEF record with MIME data. The MIME type is "application/myapp" and the MIME payload is "0123" as shown below:

I want my own app to start whenever this tag is recognised by Android. I've created the app with what I think is the correct intent filter. Here's the 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>

I've downloaded the app to a Nexus 6P device (Android version 6.0.1), set the device to the home screen and started logcat with a filter for the string "nfc". Here's what I get:

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

My question is: Why is Android dispatching "android.nfc.action.TECH_DISCOVERED" when I'm expecting "android.nfc.action.NDEF_DISCOVERED"?

EDIT 1 (adding more information after receiving the first comment below)

I've used another application, "NFC TagInfo", to verify the contents of the tag. The scanned tag appears as follows in NFC TagInfo:

Finally here's the logcat of presenting the tag to the Android device:

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

EDIT 2 (adding correct logcat after solving the problem with the accepted answer)

The problem was with the intent filter. See the accepted answer below. Here's the logcat behaviour with the problem now fixed:

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
Did you verify (e.g. with an app like NFC TagInfo) that Android actually exposes the Ndef tag technology for that tag on your device? Note that not all Android devices support NDEF on all NFC-V tags since the Type 5 Tag technology was only recently specified by the NFC Forum.Michael Roland
Thanks Michael. I've updated the question with screenshots from the NFC TagInfo app. The app suggests that Android recognises the tag as NfcV (android.nfc.tech.NfcV) and supports NDEF (android.nfc.tech.Ndef).pfp

1 Answers

1
votes

Your intent filter for the NDEF message is wrong. You currently have

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

While the MIME type matches the MIME type on your tag, the intent action is not correct. The correct action for discovering NDEF messages is "android.nfc.action.NDEF_DISCOVERED". Hence, you need to change your intent filter to this:

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