Android NFC:我无法捕获空标记意图

时间:2014-03-25 13:11:26

标签: android android-intent nfc

感谢所有花时间阅读本文的人。对不起,我的英语不好。我知道,我保证。

我正在开发使用NFC的Android应用程序。我的空标签有问题。当我读取空标签时,我无法捕捉到意图,它总是启动Android活动以读取ta标签,并提及空标签。对于其他带写东西的标签,我没有问题。

我搜索但我找不到好的东西。我认为我的问题在于清单。

所以,我当然补充一点:

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

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

在我第一次添加的活动中

<intent-filter>
            <action android:name="android.nfc.action.TECH_DISCOVERED" />

            <data android:mimeType="text/plain" />
 </intent-filter>

 <meta-data
            android:name="android.nfc.action.TECH_DISCOVERED"
            android:resource="@xml/nfc" />

xml / nfc.xml资源文件包含以下技术过滤器:

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

我在一些网站上读到添加

可能很重要
<intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="*/*" />
</intent-filter>

所以我做了。

但它没有用。

我知道我可能忘记了什么。但是什么?

2 个答案:

答案 0 :(得分:6)

如果您希望您的活动为不包含NDEF消息的标签(或包含在Android上无法过滤的NDEF消息)启动(或显示活动选择器),android.nfc.action.TECH_DISCOVERED意图是要走的路。

android.nfc.action.TAG_DISCOVERED意图过滤器仅作为后备。因此,只有在没有为与标签匹配的意图过滤器注册其他活动(不仅是您的应用程序)时,它才会触发。

如果标记上的第一个NDEF记录与给定的MIME类型匹配,或者该记录与给定的URI匹配(包括NFC论坛外部的URI映射),android.nfc.action.NDEF_DISCOVERED意图过滤器将触发类型)。

android.nfc.action.TECH_DISCOVERED意图过滤器应使用以下格式:

<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
           android:resource="@xml/nfc_filter" />

不应添加<data ... /><category ... />标记。

包含标记列表(xml/filter_nfc.xml)的资源文件必须包含与您的标记匹配的虚拟技术过滤器。每个<tech>...</tech>条目都指定了您的intent过滤器将触发的标记技术。可以将多个标签技术条目分组以形成逻辑AND或逻辑OR。一个<tech>组中的所有<tech-list>...</tech-list>个条目都与逻辑AND组合在一起。 XML文件中的多个<tech-list>...</tech-list>组与逻辑OR组合在一起。因此,如果您想触发任何NFC标签技术,您的XML文件将如下所示:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2" >
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcF</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcV</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcBarcode</tech>
    </tech-list>
</tech-list>

另一方面,您的XML文件需要标签同时具有所有标签技术,这是不可能的,因为某些技术彼此互斥(例如标签可以& #39; t同时被检测为NfcA和NfcB。)

答案 1 :(得分:4)

好的,我解决了我的问题!这不是清单,而是java代码。

我第一次阅读有关nfc的教程,并使用了部分代码。还有一些代码我没有适应你给我的建议。

这是我的代码的旧部分

IntentFilter[] filters = new IntentFilter[1];
filters[0] = new IntentFilter();
filters[0].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
    filters[0].addDataType(MIME_TEXT_PLAIN);
} catch (MalformedMimeTypeException e) {
    Log.e("App","Wrong mime")
}
adapter.enableForegroundDispatch(activity, pendingIntent, filters, techList);

错误的动作,哑剧类型。很多错误......我换了

final PendingIntent pendingIntent = PendingIntent.getActivity(activity.getApplicationContext(), 0, intent, 0);
adapter.enableForegroundDispatch(activity, pendingIntent, null, null);

它完美无缺!我希望通过你的解释可以帮助像你这样的人帮助我。

再次感谢您的时间!