Is it possible to launch an app when an NFC tag is scanned?

后端 未结 2 1480
执念已碎
执念已碎 2021-01-13 09:32

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 shou

相关标签:
2条回答
  • 2021-01-13 10:05

    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.

    0 讨论(0)
  • 2021-01-13 10:08

    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.

    0 讨论(0)
提交回复
热议问题