android nfc intent-filter to show my application when nfc discover a tag

只谈情不闲聊 提交于 2019-12-05 10:32:22

I had the same issue, and I fixed it base on this sentence in android doc http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech-disc

"If your activity filters for the ACTION_TECH_DISCOVERED intent, you must create an XML resource file that specifies the technologies that your activity supports within a tech-list set. Your activity is considered a match if a tech-list set is a subset of the technologies that are supported by the tag, which you can obtain by calling getTechList().

For example, if the tag that is scanned supports MifareClassic, NdefFormatable, and NfcA, your tech-list set must specify all three, two, or one of the technologies (and nothing else) in order for your activity to be matched."

your nfc_tech_list needs to define a subset of the technogies supported by the current tag.

-define your manifest like this:

      <intent-filter>
         <action android:name="android.nfc.action.TECH_DISCOVERED"/>
             <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>

       <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                         android:resource="@xml/nfc_tech_list" /> 

</activity>

-define the xml nfc_check_list like this:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>

    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>

    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
</resources>

This will work perfectly.

Have you created a tech-list resource?

From: http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech-disc

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

If you filter on android.nfc.action.NDEF_DISCOVERED instead of android.nfc.action.TECH_DISCOVERED, you do not need a tech-list.

What you currently have should be dropping through to the android.nfc.action.TAG_DISCOVERED (see the flow chart on the page referenced).

It is quite likely that the app list is being generated because all of those apps handle NDEF_DISCOVERED. The general intent of the NFC dispatcher is to create an Intent and deliver it to the first app that matches. The app chooser is only shown when multiple apps match the filter. Going by the flow chart it looks like matching stops when a matching action could be dispatched.

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <action android:name="android.nfc.action.TECH_DISCOVERED" />
    <action android:name="android.nfc.action.TAG_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
user7567645

Do this, ALSO REMEMBER TO ADD to add permission

on manifest.xml

<uses-sdk android:minSdkVersion="12" />
<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!