androidandroid-intentnfcintentfilterndef

NFC. Start a activity when scanning NDEF message


I'm trying to start a activity when my smartphone scans an NDEF message. This is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidbeam"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.NFC" />

    <uses-feature android:name="android.hardware.nfc" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidbeam.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.androidbeam.SendTextActivity" >

        </activity>
        <activity android:name="com.example.androidbeam.ReceiverNDEFActivity" >
            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="ext"
                    android:pathPrefix="/com.example:externalType"
                    android:scheme="vnd.android.nfc" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Whenever I scan my NDEF message, my main activity launches but I'd like my ReceiverNDEFActivity to launch. I'm not sure why this is the case.

This is my NdefMessage:

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    byte[] payload= new String("Hello");
    String domain = "com.example";
    String type = "externalType"; 
    NdefRecord extRecord = NdefRecord.createExternal(domain, type, payload);

    NdefMessage msg = new NdefMessage(new NdefRecord[] { extRecord });
    return msg;
}

Solution

  • The trick is that NFC Forum external type names are case-insensitive. However, Android's intent filter system is case-SENSITIVE. Therefore, you must always use ALL lower-case external type names in your intent filters:

    <activity android:name="com.example.androidbeam.ReceiverNDEFActivity" >
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />
            <category android:name="android.intent.category.DEFAULT" />
            <data
                android:scheme="vnd.android.nfc"
                android:host="ext"
                android:pathPrefix="/com.example:externaltype" />
        </intent-filter>
    </activity>
    

    Note that the NdefRecord.createExternal(...) method will automatically convert all type names to lower-case spelling.