如何通过扫描NDEF NFC标签启动应用程序? [英] How to launch app by scanning NDEF NFC tag?

查看:135
本文介绍了如何通过扫描NDEF NFC标签启动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过扫描NFC标签来启动我的应用程序.我有两个标签可供测试,

I am trying to launch my app by scanning an NFC tag. I have two tags to test with,

  1. 具有一个URI数据类型为" http://panasonic.net 的一个(我们称其为标签A")."和
  2. 另一个(称为标签B")具有两种数据类型-URI(具有TNF:TNF_WELL_KNOWN和RTD:RTD_URI)"urn:nfc:testing.com/ecm/ecap"和一个URN(TNF:TNF_EXTERNAL_TYPE)"urn:nfc:ext:testing.com:ecm".
  1. one (lets call this "tag A") with one URI data type "http://panasonic.net" and
  2. the other (lets call this "tag B") with two data types -- a URI (with TNF: TNF_WELL_KNOWN and RTD: RTD_URI) "urn:nfc:testing.com/ecm/ecap" and a URN (TNF: TNF_EXTERNAL_TYPE) "urn:nfc:ext:testing.com:ecm".

注意:我的主要目标是使标签B正常工作.标签A是我的测试标签,不需要与此应用一起使用.

Note: My main goal is to get tag B to work. Tag A is my test tag and it does not need to work with this app.

在清单中,我授予了NFC的权限,并且在清单中添加了技术发现列表的XML.

In my manifest, I give the permissions for NFC, and I have added the XML for the tech-discovered list in the manifest.

在intent-filter标签中,我有以下内容:

In the intent-filter tag, I have the following:

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

没有 TECH_DISCOVERED ,标签A显示在要自动启动的应用程序列表中,但未显示标签B.使用 TECH_DISCOVERED ,两个标签都显示在列表中.
更正:如果没有 TECH_DISCOVERED ,则对于标签A和标签B,该应用程序不会显示在自动启动列表中.但是,如果没有 TECH_DISCOVERED < data android:scheme ="http" android:host ="panasonic.net"/> ,标签A确实会显示该应用在自动启动列表中.对于标签A,这是正确的行为,因为当< data ... android:host ="panasonic.net"/> 不存在时,Chrome会接管并自动启动.

Without TECH_DISCOVERED, tag A is shown in the list of apps to launch automatically but tag B is not shown. With TECH_DISCOVERED, both tags are shown on the list.
Correction: Without TECH_DISCOVERED, for tag A and tag B, the app does not show up in the auto-launch list. But without TECH_DISCOVERED and <data android:scheme="http" android:host="panasonic.net"/>, tag A does get the app to show up in the auto-launch list. For tag A this is correct behavior because Chrome takes over and launches automatically when <data ... android:host="panasonic.net"/> is not there.

接下来,我在意图过滤器中指定了一些数据标签:

Next, I have specified some data tags in the intent-filter:

<data android:scheme="http" android:host="panasonic.net"/>
<data android:scheme="http" android:host="fake.com"/>

扫描标签A时,它将使该应用程序显示在列表中.扫描标签B时,该应用未显示在列表中.这是正确的行为.

When scanning tag A, it gets the app to show up on the list. When scanning tag B, the app is not shown in the list. This is correct behavior.

然后我将数据标签添加到标签B的意图过滤器中:

Then I add a data tag to the intent-filter for tag B:

<data android:scheme="vnd.android.nfc"
      android:host="ext"
      android:pathPrefix="/com.informationmediary:ecm" />

这是我开始遇到麻烦的地方.我扫描了标签A和标签B,但该应用程序均未显示在两者的自动启动列表中.当我删除HTTP数据标签时,仅留下"vnd.android.nfc"一个,然后再次扫描标签B,该应用仍然没有显示.

This is where I start having trouble. I scan tag A and tag B, and the app does not show up in the auto-launch list for both. When I remove the HTTP data tags leaving only the "vnd.android.nfc" one and scan tag B again the app still does not show up.

我也尝试了以下变体,但无济于事:

I also tried the following variations to no avail:

在这一点上,我删除了以下希望使标签B仅工作的方法:

at this point I have removed the following hoping to get tag B to work only:

<data android:scheme="http" android:host="panasonic.net"/>
<data android:scheme="http" android:host="fake.com"/>

1.

    <data android:scheme="vnd.android.nfc"
          android:host="ext"
          android:pathPrefix="/com.informationmediary:ecm" />

2.

    <data android:scheme="vnd.android.nfc"
          android:host="ext"
          android:pathPrefix="/informationmediary.com:ecm" />

3.

    <data android:scheme="urn:nfc"
          android:host="ext"
          android:pathPrefix="/com.informationmediary:ecm" />

4.

    <data android:scheme="urn:nfc"
          android:host="ext"
          android:pathPrefix="/informationmediary.com:ecm" />

5.

    <data android:scheme="vnd.android.nfc"
          android:host="informationmediary.com"
          android:pathPrefix="/ecm/ecap"/>

6.

    <data android:scheme="urn:nfc"
          android:host="informationmediary.com"
          android:pathPrefix="/ecm/ecap"/>

我尝试了前四名与后四名的所有组合:1& 5、1& 6、2& 5、2& 6、3& 5、3& 6、4& 5和4& 6

I have tried all combinations of two of the top four with the bottom two: 1&5, 1&6, 2&5, 2&6, 3&5, 3&6, 4&5 and 4&6

我怀疑scheme ="urn:nfc",但还是尝试了一下,这次是抓稻草".

I doubted scheme="urn:nfc" but tried it anyways, "grasping at straws" by this time.

标签B是我需要工作的标签,标签A是我进行的测试.

Tag B is the one I need to work, tag A is a test one I have.

我已阅读文档 https://developer.android.com/guide/topics/connectivity/nfc/nfc#ext-type 并且 https://developer.android.com/guide/topics/manifest/data-element#mime 和论坛上的其他几篇文章,基本上都说出了与developer.android景点相同的内容.

I have read the documentation https://developer.android.com/guide/topics/connectivity/nfc/nfc#ext-type And https://developer.android.com/guide/topics/manifest/data-element#mime and several other posts on forums that basically say the same thing as in the developer.android sights.

推荐答案

为标签B添加意图过滤器会导致不再拾取标签A

添加数据元素时

Adding the intent filter for Tag B results in Tag A no longer being picked up

When you add the data element

<data android:scheme="vnd.android.nfc"
      android:host="ext"
      android:pathPrefix="/com.informationmediary:ecm" />

添加到您现有的意图过滤器

to your existing intent filter

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" android:host="panasonic.net" />
    <data android:scheme="http" android:host="fake.com" />
</intent-filter>

Android会将所有数据元素合并为一个.因此,您的意图过滤器形式为

Android will merge all data elements into a single one. Consequently, your intent filter of the form

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" android:host="panasonic.net" />
    <data android:scheme="http" android:host="fake.com" />
    <data android:scheme="vnd.android.nfc"
          android:host="ext"
          android:pathPrefix="/informationmediary.com:ecm" />
</intent-filter>

意味着Android会尝试匹配标记上的URL,如下所示:

means that Android will try to match the URL on the tag like this:

(scheme == "http" OR scheme == "vnd.android.nfc") AND
(host == "panasonic.net" OR host == "fake.com" OR host == "ext") AND
(path startsWith "/informationmediary.com:ecm")

因此,即使未在其他数据元素上明确指定android:pathPrefix属性,该URL也必须与URL匹配.

Therefore, the android:pathPrefix attribute must match th URL even if it is not explicitly specified on the other data elements.

通过指定两个单独的Intent过滤器,您可以轻松摆脱该问题:

You can easily get rid of that issue by specifying two separate intent filters:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" android:host="panasonic.net" />
    <data android:scheme="http" android:host="fake.com" />
</intent-filter>
<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="vnd.android.nfc"
          android:host="ext"
          android:pathPrefix="/informationmediary.com:ecm" />
</intent-filter>

标签B从不匹配意图过滤器

只有标记上的第一条记录才与意图过滤器匹配.因此,为了匹配外部类型"记录,您需要使其成为标签上的第一条记录.然后,如果您的外部类型"记录包含URI"urn:nfc:ext:informationmediary.com:ecm",它将与上面的意图过滤器匹配.请注意,由于前缀是隐式的,因此记录实际上必须仅包含"informationmediary.com:ecm"部分.

Tag B never matches the intent filter

Only the first record on a tag is matched against intent filters. Hence, in order to match the External Type record, you need to make it the first record on the tag. Then, if your External Type record contains the URI "urn:nfc:ext:informationmediary.com:ecm" it will match the above intent filter. Note that the record actually must contain only the part "informationmediary.com:ecm" since the prefix is implicit.

但是,如果您不能更改标签B的内容,则需要将该标签上的URI记录与意图过滤器进行匹配.不幸的是,您在标签B上的URI在Android上不易使用.问题是Android只能匹配形式为"scheme://host/path"的URL的主机和路径(请注意斜杠!).由于使用的是URN,因此它没有主机或路径组件.在这种情况下,您只能尝试自行匹配方案(即缸"):

If you can't change the content of tag B though, you would need to match the URI record on that tag with the intent filter. Unfortunately, your URI on tag B is not easily usable with Android. The problem is that Android can only match host and path for URLs of the form "scheme://host/path" (note the slashes!). Since you use a URN instead, that does not have a host or path component. In this case, you could only try to match the scheme (i.e. "urn") on its own:

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

尽管如此,您无法区分URN的其余组件(即,主机或路径组件没有提供这种区分).

You can't differentiate between the remaining components of the URN though (i.e. there is no such differentiation as provided by host or path components).

这篇关于如何通过扫描NDEF NFC标签启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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