Search code examples
androidnfcintentfilterndefandroid-applicationrecord

How to redirect the AAR on an NFC tag to a new application ID?


I've got an NDEF NFC tag with two records:

  • The first one contains my website URL.
  • The second one is an AAR with the application ID of the my old application.

So far, the old application is started properly when the NFC tag is discovered.

But I would like to create a new application that will handle these NFC tags instead of the old one.

I've added in the manifest:

<android:name=".splash.SplashActivity"
    android:screenOrientation="portrait"
    android:stateNotNeeded="true"
    android:theme="@style/SplashTheme">

    <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
        android:resource="@xml/nfc_tech_filter" />

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

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

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

I've got the new application installed on my phone but when I tap the NFC tag it opens the Play Store and redirects me to the old application.

Is it possible to create an NFC filter that will be triggered when discovering a tag that contains an other application ID?


Solution

  • No, it's not possible to start your new app with that tag. The whole idea of the Android Application Record (AAR) is to prevent exactly that (see NFC Basics):

    [... an AAR] provides a stronger certainty that your application is started when an NFC tag is scanned. [...] AARs are useful if you want to prevent other applications from filtering for the same intent and potentially handling specific tags that you have deployed.

    Hence, if you want to launch your app using the NFC tag, the only option is to replace the data on the tag (or the whole tag, in case it's write locked).

    However, if you only want to read the tag while your (new) app is already open in the foreground, you could use the foreground dispatch system or reader mode to overrride that behavior and force Android to dispatch the tag discovery event to your (new) app. See Using the Foreground Dispatch System.