7

我正在编写一个适用于 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>

这里显示我的应用程序不在列表中的图像: 在此处输入图像描述

4

3 回答 3

14

我遇到了同样的问题,我根据 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>

这将完美地工作。

于 2013-07-23T22:32:26.233 回答
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>
于 2013-05-12T17:42:01.300 回答
0

这样做,还记得添加以添加权限

在 manifest.xml 上

<uses-sdk android:minSdkVersion="12" />
<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />
于 2017-09-14T15:03:37.567 回答