android nfc intent-filter to show my application w

2019-05-02 00:44发布

问题:

I am writing an app that works with NFC and MIFARE CARD.

When my NFC device detect a card, it shows me the list of application that can use NFC, but my application is not mentioned.

What I am missing on my android manifest file?

<uses-permission android:name="android.permission.NFC" />

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />

<application
    android:icon="@drawable/ic_launcher"         android:allowBackup="true"
    android:label="@string/app_name" android:theme="@style/AppTheme" >
    <activity android:uiOptions="splitActionBarWhenNarrow" 
        android:name="it.namespace.app.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <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>
     <meta-data
            android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/tech_filter" />
    </activity>
</application>

And this is my tech_filter file xml:

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

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

</resources>

Here the image that shows that my application is not in the list:

回答1:

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.



回答2:

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>


回答3:

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