Is it possible to run the application while checking the NFC tag?

I have an NFC tag. I want to write an Android application that starts automatically and receives data from NFC when the NFC tag is scanned using the phone.

This should work, assuming the device has NFC and that there are no other applications on the phone. I found some applications that can run another application, but my application should work without such an additional application running in the background.

Is there any way to solve this problem?

+5
source share
2 answers

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.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 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 and
  • Ndef / 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.

+9
source

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

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

Now, when you click your NFC tag on your phone, your application will be called up and launched.

+1
source

Source: https://habr.com/ru/post/1233452/


All Articles