android nfc intent-filter在nfc发现标签时显示我的应用程序

时间:2013-05-12 17:27:42

标签: android android-intent nfc mifare intentfilter

我正在编写一个适用于NFC和MIFARE CARD的应用程序。

当我的NFC设备检测到卡时,它会显示可以使用NFC的应用程序列表,但未提及我的应用程序。

我的android清单文件中缺少什么?

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

这是我的tech_filter文件xml:

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

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

</resources>

此处显示我的应用程序不在列表中的图像: enter image description here

3 个答案:

答案 0 :(得分:13)

我有同样的问题,我根据android doc http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech-disc

中的这句话修复了它

“如果您的活动过滤了ACTION_TECH_DISCOVERED意图,则必须创建一个XML资源文件,该文件指定您的活动在技术列表集中支持的技术。如果技术列表集是子集,则您的活动被视为匹配标记支持的技术,您可以通过调用getTechList()获得。

例如,如果扫描的标记支持MifareClassic,NdefFormatable和NfcA,那么您的技术列表集必须指定所有三种,两种或一种技术(而不是其他任何技术),以便您的活动匹配。“

您的nfc_tech_list需要定义当前标记支持的技术子集。

- 像这样定义你的清单:                                                                     

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

- 像这样定义xml nfc_check_list:

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

这将完美无缺。

答案 1 :(得分:1)

您是否创建了技术列表资源?

来自: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>

如果过滤android.nfc.action.NDEF_DISCOVERED而不是android.nfc.action.TECH_DISCOVERED,则不需要技术列表。

您目前所拥有的应该是直到android.nfc.action.TAG_DISCOVERED(请参阅所引用页面上的流程图​​)。

很可能正在生成应用列表,因为所有这些应用都处理NDEF_DISCOVERED。 NFC调度程序的一般意图是创建一个Intent并将其传递给匹配的第一个应用程序。仅当多个应用与过滤器匹配时,才会显示应用选择器。按照流程图,当匹配操作可以调度时,匹配就会停止。

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

答案 2 :(得分:0)

执行此操作,还要记住添加以添加权限

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