Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 assuming that the device has NFC turned on and that there are no other applications running on the phone. I found some applications which can launch another application, but my application should work without such an additional application running in the background.

Is there any way to solve this task?

like image 662
Jame Avatar asked Oct 11 '15 02:10

Jame


People also ask

Can an NFC tag open an app?

By using an NFC tag, many different operating steps on the smartphone can be shortened, using so-called shortcuts. All it takes is a quick contact of the tag with an NFC-enabled iPhone. An example: Use an NFC tag to open the Maps app directly when contacting the iPhone.

Can you emulate an NFC tag?

Its possible to make Android device behave as an NFC Tag. Such a behaviour is called Card Emulation. In CE, an applet residing in the Secure element responds to the reader. Here, its sufficient to have the NFC controller powered, with rest of the device suspended.

Can iPhone automatically read NFC tags?

On iPhones that support background tag reading, the system scans for and reads NFC data without requiring users to scan tags using an app. The system displays a pop-up notification each time it reads a new tag. After the user taps the notification, the system delivers the tag data to the appropriate app.

Do you need an app to read NFC tags?

If you use an iPhone XS or later, you don't need any specific app to read NFC Tags (such as with an Android device). Instead, if you use an iPhone 7, 8, or X, you need to install and open an application to read NFC Tags.


2 Answers

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>
        <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 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):

<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>

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>
        <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 could 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 multiple intent filters for one activity:

<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 one more NFC-related intent filter:

<intent-filter>
    <action android:name="android.nfc.action.TAG_DISCOVERED" />
    <category android: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.

like image 169
Michael Roland Avatar answered Oct 17 '22 01:10

Michael Roland


Add 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 tap your NFC tag to your phone, your application will be called and run.

like image 43
user3406222 Avatar answered Oct 17 '22 00:10

user3406222