Android near-field communication-nfc basics (III)

Source: Internet
Author: User
Tags nfca

Filter NFC intent

To start your application when you want to process the scanned NFC tag, you can filter out one, two, or all of the three NFC intents in your app's Android list. However, you usually want to control the most commonly used intent of the action_ndef_discovered type when the application starts. The intent of action_ndef_discovered type is rolled back from the intent of action_ndef_discovered type to the intent of action_tech_discovered type only when the intent of action_ndef_discovered type is not filtered or the data load is not ndef. Generally, action_tab_discovered is the most general filtering type. Many applications will filter action_ndef_discovered or action_tech_discovered before filtering action_tag_discovered, which will reduce the possibility of your application being started. Action_tag_discovered is the final method used only when no application processes the intent of the action_ndef_discovered or action_tech_discovered type.

Because of the diversity of NFC tags and many times you are not under your control, you need to roll back to the other two types of intent when necessary. We recommend that you use the ndef format when you can control the tag type and data written. The following describes how to filter intent objects of each type.

Action_ndef_discovered

To filter intent of the action_ndef_discovered type, declare the intent filter of this type in the list together with the data you want to filter. The following statements are used to filter the action_ndef_discovered Type filters of text/plain MIME types:

<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 URL format of http://developer.android.com/index.html 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 needs to filter intent of the action_tech_discovered type, you must create an XML resource file that specifies the technologies supported by your activity in the tech-list collection. If the tech-list set is a subset of the technologies supported by tags, your activity is considered to be matched. Call the gettechlist () method to obtain the technical set supported by tags.

For example, if the scanned tags support mifareclassic, ndefformatable, and nfca, the tech-list set must specify all three technologies to match them, or specify either of them.

The following example defines all related technologies. You can delete some settings as needed. Save the file to the <project-root>/RES/XML folder (you can name it any name 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 sets. Each tech-list set is considered independent. If any tech-list set is a subset of the technology returned by gettechlist, then your activity is considered to be a match. The following example can be matched with NFC tags that support nfca and ndef technologies or tags that support nfcb and ndef technologies:

<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 of the <activity> element as shown 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 filter to filter intent of the action_tag_discovered type:

<Intent-filter>


<Action Android: Name = "android. NFC. Action. tag_discovered"/>

</Intent-filter>

 

Related Article

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.