In order for your application (actually activity) to be launched when the tag is scanned, you need to add the appropriate intent filter to the application manifest.
If you want to run the application only for any tag, the TECH_DISCOVERED intent TECH_DISCOVERED is what you would like to use:
<activity ...> <intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED" /> </intent-filter> <meta-data android: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 your application should listen to (note the <meta-data ... /> outside ). Available technologies are those that are in the android.nfc.tech.* Namespace, currently:
android.nfc.tech.IsoDepandroid.nfc.tech.MifareClassicandroid.nfc.tech.MifareUltralightandroid.nfc.tech.Ndefandroid.nfc.tech.NdefFormatableandroid.nfc.tech.NfcAandroid.nfc.tech.NfcBandroid.nfc.tech.NfcBarcodeandroid.nfc.tech.NfcFandroid.nfc.tech.NfcV
To detect only any tag, you must create such an XML file (create a file as xml/nfc_tech_filter.xml ):
<resources xmlns: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>
Please note that you do not have to use other technologies as
IsoDep means either NfcA or NfcB ,MifareClassic means NfcA ,MifareUltralight means NfcA andNdef / NdefFormatable means either NfcA , NfcB , NfcF , or NfcV .
The above intent filter will be triggered if there is no other application that has the best matching filter. The best match will match the data type used in the tag. For example, if your tag contains a URL (encapsulated in an NDEF message), the application that launches the URLs will take precedence over your application. If you know the data types used in your tags, you can also add a filter for these data types. For example, to match only any URLs "http: //" and "https: //", you can use:
<activity ...> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> <data android:scheme="https" /> </intent-filter> </activity>
Similarly, if your tag contains the MIME type "application / vnd.com.example", you can use:
<activity ...> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/vnd.com.example" /> </intent-filter> </activity>
You could even combine several intent filters for one action:
<activity ...> <intent-filter> <action android:name="android.nfc.action.TECH_DISCOVERED" /> </intent-filter> <meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" /> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> <data android:scheme="https" /> </intent-filter> <intent-filter> <action android:name="android.nfc.action.NDEF_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/vnd.com.example" /> </intent-filter> </activity>
Finally, there is another intent filter related to NFC:
<intent-filter> <action android:name="android.nfc.action.TAG_DISCOVERED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
However, you usually do not use this intent filter in the manifest. This is implied only in case of a recession and will work only if there is no other application launch in the technology or data of the scanned tag. Therefore, there is no need to add this intent filter, which is already running for the aforementioned TECH_DISCOVERED filter.