Android near Field Communication---NFC Fundamentals (iii)

Source: Internet
Author: User
Tags nfca

Transfer from http://blog.csdn.net/think_soft/article/details/8180203

Intent for filtering NFC

To launch your app when you want to process the scanned NFC tag, you can filter on one, two, or all three types of NFC intent in your app's Android manifest. However, you typically want to control the intent of the most commonly used action_ndef_discovered types when the application starts. An application that does not filter the action_ndef_discovered type of intent, or if the data payload is not NDEF, is returned from the action_ndef_discovered type intent to Action_tech_ Discovered type of intent. Usually the action_tab_discovered is the most generalized filter classification. Many applications filter action_ndef_discovered or action_tech_discovered before filtering action_tag_discovered, which reduces the likelihood that your application will be launched. Action_tag_discovered is only the last resort to use if there are no applications that handle action_ndef_discovered or action_tech_discovered types of intent.

Because of the diversity of NFC tags, and often not under your control, you have to fall back to the other two types of intent when necessary. We recommend that you use the NDEF format when you are able to control the type of labels and the data that is written. The following section describes how to filter each type of intent object.

Action_ndef_discovered

To filter the action_ndef_discovered type of intent, declare the type's intent filter in the list along with the data you want to filter. The following is a declaration of the Action_ndef_discovered type filter that filters the mime of the Text/plain 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 a URI in the http://developer.android.com/index.html format to filter:

<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 your activity is to filter the action_tech_discovered type of intent, you must create an XML resource file that specifies the techniques supported by your activity in the Tech-list collection. If the Tech-list collection is a subset of the technologies supported by the label, then your activity is considered to be a match. Get the collection of technologies supported by the label by calling the Gettechlist () method.

For example, if the scanned labels support mifareclassic, ndefformatable, and NFCA, then to match them, the Tech-list collection must specify all of these three techniques, or specify two or both.

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

<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 collections, each Tech-list collection is considered independent, and if any one tech-list collection is a subset of the technology returned by Gettechlist (), then your activity is considered a match. The following examples can be matched to support NFCA and Ndef technology NFC tags or tags that support NFCB and NDEF technology:

<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, specify the resource file you created in the <meta-data> element within the <activity> element, as in the following example:

<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

Use the following intent filters to filter the intent of the action_tag_discovered type:

<intent-filter>

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

</intent-filter>

Android near Field communication---NFC Basis (iii) (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.