Search code examples
androidnfcintentfilter

Android NDEF intent-filter with data for http scheme and host


I am trying to define an Intent filter that will only trigger when I receive NDEF messages that contain the URI of a particular web site.

I have it defined like this:

        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http" />
            <data android:host="ta.bcntouch.com" />
        </intent-filter>

But it won't trigger like that. I have also tried:

            <data android:scheme="http"android:host="ta.bcntouch.com" />

With no luck. Also with just DEFAULT. Removing the element will cause it to trigger.

Can this be done? The Android documentation only shows examples using MIME type in the element.....

Any help appreciated.


Solution

  • Here is the filters I finally used, to capture a number of specific known URL combinations.

    The '*' at the start of the host field allows me to use the same filter when testing with test servers that are in a sub-domain, or follow the same format for name.

    The second (View) one captures the same URL formats from web-pages, emails, etc...:

            <intent-filter>
              <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
              <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"
                      android:host="*ta.bcntouch.com" 
                      android:pathPattern="/p/.*" />
                <data android:scheme="http"
                      android:host="*ta.bcntouch.com" 
                      android:pathPattern="/l/.*" />
                <data android:scheme="http"
                      android:host="*ta.bcntouch.com" 
                      android:pathPattern="/a.*" />
                <data android:scheme="http"
                      android:host="*ta.bcntouch.com" 
                      android:pathPattern="/t.*" />
            </intent-filter>
    
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="http"
                      android:host="*ta.bcntouch.com" 
                      android:pathPattern="/p/.*" />
                <data android:scheme="http"
                      android:host="*ta.bcntouch.com" 
                      android:pathPattern="/l/.*" />
                <data android:scheme="http"
                      android:host="*ta.bcntouch.com" 
                      android:pathPattern="/a.*" />
                <data android:scheme="http"
                      android:host="*ta.bcntouch.com" 
                      android:pathPattern="/t/.*" />
            </intent-filter>