发现NFC_TECH时打开Android活动 [英] Open Android Activity when NFC_TECH is discovered

查看:115
本文介绍了发现NFC_TECH时打开Android活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用,该应用需要在扫描除ID以外不包含任何数据(?)的NFC标签时打开.

I am developing an app which is required to open upon scanning an NFC tag that doesn't contain any data(?) except for ID.

应该使用该ID来标识数据库中的项目,并且我不应该在这些标签上写任何东西

The items in database are supposed to be identified by that ID and I am not supposed to write anything on these tags

我可以通过致电

enableForegroundDispatch()

,它以包含所需数据EXTRA_ID

但是,当应用程序在后台运行并且我扫描标签时,我可以听到系统声音以完成扫描,但是该应用程序未打开

However when the application is on background and I scan a tag, I can hear the system sound for scan completion but the app is not opened

我的应用清单中有以下意图过滤器

I have the following intent filter on my application manifest

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

        <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_tech_filter" />

nfc_tech_filter.xml包含Android支持的所有标签

nfc_tech_filter.xml contains all the tags supported by Android

<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.tech.MifareUltralight, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable

The tag I am scanning is of type android.nfc.tech.MifareUltralight, android.nfc.tech.NfcA, android.nfc.tech.NdefFormatable

我只对标签ID感兴趣

是否可以在不扫描标签上写任何内容的情况下,在标签扫描时打开/通知我的活动?

Is it possible to get my activity opened/notified upon tag scanning without writing anything on the tag?

推荐答案

您似乎已经发现自己了,可以使用TECH_DISCOVERED意向过滤器:

As you already seem to have found out yourself, that's possible using the TECH_DISCOVERED intent filter:

<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_tech_filter" />

问题是您的技术过滤器XML文件.您指定的技术过滤器会转换为*与IsoDep NfcA NfcB NfcF 等.由于其中一些标记技术(例如Nfc [A | B | F | V])是互斥的,因此没有任何标记会符合此条件.

The problem is your tech-filter XML file. The tech-filter that you specified translates to *match any tag that is IsoDep and NfcA and NfcB and and NfcF and etc. As some of these tag technologies (e.g. Nfc[A|B|F|V]) are mutually exclusive, no tag will ever match this condition.

您可以通过指定一个技术过滤器来解决此问题,该技术过滤器将所有这些技术与逻辑上的相匹配:

You can overcome this by specifying a tech-filter that matches all these technologies with logical or:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
    </tech-list>
    <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.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareClassic</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcBarcode</tech>
    </tech-list>
</resources>

或者您已经发现您的标签是NfcA,您也可以简单地匹配NfcA:

Or as you already found that your tag is NfcA, you could also simply match NfcA:

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

这篇关于发现NFC_TECH时打开Android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆