简体   繁体   English

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

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

I am writing an app that works with NFC and MIFARE CARD. 我正在编写一个适用于NFC和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. 当我的NFC设备检测到卡时,它会显示可以使用NFC的应用程序列表,但我的应用程序未被提及。

What I am missing on my android manifest file? 我的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>

And this is my tech_filter file xml: 这是我的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>

Here the image that shows that my application is not in the list: 这里的图像显示我的应用程序不在列表中: 在此输入图像描述

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 我有同样的问题,我根据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(). “如果您的活动过滤了ACTION_TECH_DISCOVERED意图,则必须创建一个XML资源文件,该文件指定您的活动在技术列表集中支持的技术。如果技术列表集是技术的子集,则您的活动被视为匹配标记支持,您可以通过调用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." 例如,如果扫描的标记支持MifareClassic,NdefFormatable和NfcA,则您的技术列表集必须指定所有三种,两种或一种技术(而不是其他任何技术),以便匹配您的活动。

your nfc_tech_list needs to define a subset of the technogies supported by the current tag. 您的nfc_tech_list需要定义当前标记支持的技术子集。

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

This will work perfectly. 这将完美地工作。

Have you created a tech-list resource? 您是否创建了技术列表资源?

From: http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#tech-disc 来自: 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. 如果您在android.nfc.action.NDEF_DISCOVERED而不是android.nfc.action.TECH_DISCOVERED上过滤,则不需要技术列表。

What you currently have should be dropping through to the android.nfc.action.TAG_DISCOVERED (see the flow chart on the page referenced). 您目前所拥有的应该是直接访问android.nfc.action.TAG_DISCOVERED(请参阅所引用页面上的流程图​​)。

It is quite likely that the app list is being generated because all of those apps handle NDEF_DISCOVERED. 很可能正在生成应用列表,因为所有这些应用都处理NDEF_DISCOVERED。 The general intent of the NFC dispatcher is to create an Intent and deliver it to the first app that matches. NFC调度程序的一般意图是创建一个Intent并将其传递给匹配的第一个应用程序。 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>

Do this, ALSO REMEMBER TO ADD to add permission 这样做,还记得添加以添加权限

on manifest.xml 在manifest.xml上

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM