Skip to content Skip to sidebar Skip to footer

Is It Possible To Launch An App When An Nfc Tag Is Scanned?

I have an NFC tag. I want to write an Android application that is automatically launched and gets data from NFC when the NFC tag is scanned with the phone. This should work assumin

Solution 1:

In order to get your app (actually activity) started upon scanning a tag, you need to add an appropriate intent filter to your app manifest.

If you want to start your app for just any tag, the TECH_DISCOVERED intent filter is what you would want to use:

<activity...><intent-filter><actionandroid:name="android.nfc.action.TECH_DISCOVERED" /></intent-filter><meta-dataandroid:name="android.nfc.action.TECH_DISCOVERED"android:resource="@xml/nfc_tech_filter" /></activity>

This intent filter requires an additional XML resource file that defines the tag technologies that your app should listen to (note the <meta-data ... /> tag outside the intent-filter). The available technologies are those in the namespace android.nfc.tech.*, currently:

  • android.nfc.tech.IsoDep
  • android.nfc.tech.MifareClassic
  • android.nfc.tech.MifareUltralight
  • android.nfc.tech.Ndef
  • android.nfc.tech.NdefFormatable
  • android.nfc.tech.NfcA
  • android.nfc.tech.NfcB
  • android.nfc.tech.NfcBarcode
  • android.nfc.tech.NfcF
  • android.nfc.tech.NfcV

To discover just any tag, you would create an XML file like this (create the file as xml/nfc_tech_filter.xml):

<resourcesxmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"><tech-list><tech>android.nfc.tech.NfcA</tech></tech-list><tech-list><tech>android.nfc.tech.NfcB</tech></tech-list><tech-list><tech>android.nfc.tech.NfcBarcode</tech></tech-list><tech-list><tech>android.nfc.tech.NfcF</tech></tech-list><tech-list><tech>android.nfc.tech.NfcV</tech></tech-list></resources>

Note that you do not necessarily need to incude the other technologies as

  • IsoDep implies either NfcA or NfcB,
  • MifareClassic implies NfcA,
  • MifareUltralight implies NfcA, and
  • Ndef / NdefFormatable imply either NfcA, NfcB, NfcF, or NfcV.

The above intent filter will be triggered if there is no other app that has a better matching intent filter. A better match would be a match for the data type used on the tag. So, for instance, if your tag contains a URL (encapsulated in an NDEF message), an app that triggers on URLs will get precedence over your app. If you know the data type(s) used on your tags, you could also add a filter for those data type(s). For instance, to match just any "http://" and "https://" URL, you could use:

<activity...><intent-filter><actionandroid:name="android.nfc.action.NDEF_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:scheme="http" /><dataandroid:scheme="https" /></intent-filter></activity>

Similarly, if your tag contains the MIME type "application/vnd.com.example", you could use:

<activity...><intent-filter><actionandroid:name="android.nfc.action.NDEF_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:mimeType="application/vnd.com.example" /></intent-filter></activity>

You could even combine multiple intent filters for one activity:

<activity...><intent-filter><actionandroid:name="android.nfc.action.TECH_DISCOVERED" /></intent-filter><meta-dataandroid:name="android.nfc.action.TECH_DISCOVERED"android:resource="@xml/nfc_tech_filter" /><intent-filter><actionandroid:name="android.nfc.action.NDEF_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:scheme="http" /><dataandroid:scheme="https" /></intent-filter><intent-filter><actionandroid:name="android.nfc.action.NDEF_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:mimeType="application/vnd.com.example" /></intent-filter></activity>

Finally, there is one more NFC-related intent filter:

<intent-filter><actionandroid:name="android.nfc.action.TAG_DISCOVERED" /><categoryandroid:name="android.intent.category.DEFAULT" /></intent-filter>

However, you would normally not use this intent filter in the manifest. It is meant as a fall-back only and will only ever be triggered if there is no other app triggering on the technology or the data of the scanned tag. So there is no need to add this intent-filter of you already trigger for the above-mentioned TECH_DISCOVERED intent filter.

Solution 2:

Add following intent-filter to your main activity tag in the AndroidManifest.xml file.

<!-- main activity --><activity...>
    ...
    <intent-filter><actionandroid-name="android.nfc.action.TAG_DISCOVERED" /><categoryandroid-name="android.nfc.category.DEFAULT" /></intent-filter>
    ...
</activity>

Now, when you tap your NFC tag to your phone, your application will be called and run.

Post a Comment for "Is It Possible To Launch An App When An Nfc Tag Is Scanned?"