Android NFC --- NFC basis (c) (rpm)

NFC filter of Intent

To start your application when you want to deal with the NFC tag is scanned, you can filter for one, two or all three NFC types of Intent in Android manifest your application. Generally, however, you want to control the most common type of Intent ACTION_NDEF_DISCOVERED when the application starts. In the absence of Intent filter ACTION_NDEF_DISCOVERED type of application, or the data load is not NDEF, will be from ACTION_NDEF_DISCOVERED types of Intent to fall back ACTION_TECH_DISCOVERED type of Intent. ACTION_TAB_DISCOVERED is usually the most generalized filter classification. Many applications will be before filtering ACTION_TAG_DISCOVERED, filtration ACTION_NDEF_DISCOVERED or ACTION_TECH_DISCOVERED, this will reduce the likelihood of your application is started. ACTION_TAG_DISCOVERED only in the absence of application processing ACTION_NDEF_DISCOVERED or ACTION_TECH_DISCOVERED type of Intent circumstances before use of last resort.

Because of the variety NFC tag, and many times are not under your control, so when you need to fall back to the other two types of Intent. When you can control the type of data and writing of tags, we recommend that you use NDEF format. The following describes how each type of filter Intent object.

ACTION_NDEF_DISCOVERED

To filter ACTION_NDEF_DISCOVERED type of Intent, we must in the inventory with the data you want to filter together to declare the type of Intent filter. The following statement is a filter type filtration ACTION_NDEF_DISCOVERED text / plain MIME-type:

<intent-filter>

    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>

    <category android:name="android.intent.category.DEFAULT"/>

    <data android:mimeType="text/plain" />

</intent-filter>

The following example uses the URI http://developer.android.com/index.html to filter format:

<intent-filter>

    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>

    <category android:name="android.intent.category.DEFAULT"/>

   <data android:scheme="http"

              android:host="developer.android.com"

              android:pathPrefix="/index.html" />

</intent-filter>

ACTION_TECH_DISCOVERED

If you want to filter ACTION_TECH_DISCOVERED type of Activity of Intent, you must create an XML resource file that specifies the technology supported by your Activity in the tech-list collection. If the tech-list set is a subset of the technologies supported by the tag, then your Activity is considered a match. To obtain a set of technologies supported by the label by calling getTechList () method.

For example, if the scanned label support MifareClassic, NdefFormatable and NFCA, in order to match it with them, tech-list set must specify all three technologies, or specify one or both of them.

The following example defines all of the technologies. You can remove some of the settings as needed. Then save the file to <project-root> / res / xml folder (you can put anything you want to name names):

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <tech-list>

        <tech>android.nfc.tech.IsoDep</tech>

        <tech>android.nfc.tech.NfcA</tech>

        <tech>android.nfc.tech.NfcB</tech>

        <tech>android.nfc.tech.NfcF</tech>

        <tech>android.nfc.tech.NfcV</tech>

        <tech>android.nfc.tech.Ndef</tech>

        <tech>android.nfc.tech.NdefFormatable</tech>

        <tech>android.nfc.tech.MifareClassic</tech>

        <tech>android.nfc.tech.MifareUltralight</tech>

    </tech-list>

</resources>

You can also specify multiple tech-list set, tech-list for each set is considered independent, and if any one tech-list is a subset of technology by getTechList () returns, then you are considered Activity match. The following examples can be matched with and supported NfcA Ndef technical support with the NFC tag or label art NfcB and Ndef:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <tech-list>

        <tech>android.nfc.tech.NfcA</tech>

        <tech>android.nfc.tech.Ndef</tech>

    </tech-list>

</resources>

 

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

    <tech-list>

        <tech>android.nfc.tech.NfcB</tech>

        <tech>android.nfc.tech.Ndef</tech>

    </tech-list>

</resources>

In your AndroidManifest.xml file, the following examples should be like that, <meta-data> element is specified in the resource file you created in the <activity> element:

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

ACTION_TAG_DISCOVERED

Intent following filters to filter ACTION_TAG_DISCOVERED Intent type:

<intent-filter>

    <action android:name="android.nfc.action.TAG_DISCOVERED"/>

</intent-filter>

Guess you like

Origin www.cnblogs.com/Alex80/p/11520053.html