【问题标题】:android nfc intent-filter to show my application when nfc discover a tagandroid nfc intent-filter 在 nfc 发​​现标签时显示我的应用程序
【发布时间】:2013-05-12 17:27:42
【问题描述】:

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

这里显示我的应用程序不在列表中的图像:

【问题讨论】:

    标签: android android-intent nfc mifare intentfilter


    【解决方案1】:

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

    这将完美运行。

    【讨论】:

    • 这对我有帮助,我花了 2 个小时尝试解决它!
    • 对于其他人,如果您想知道您的标签支持的“技术列表”,您可以使用名为“NFC TagInfo”的应用程序来查看。
    【解决方案2】:

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

    发件人: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>
    

    【讨论】:

    • 尝试为 android.nfc.action.NDEF_DISCOVERED 添加一个操作,您可能有多个应用程序正在为此过滤,并且列表仅基于这些应用程序。
    • 嗯,我该怎么做?我不确定我是否理解。
    • 将其添加到答案中。我对自己的决议感兴趣。本周刚收到我所有的 NFC 标签和一个 Arduino Shield(另外我还有一个 Nexus 7)。我有一个需要所有这些的项目。 :)
    【解决方案3】:

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

    在 manifest.xml 上

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

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多