简体   繁体   English

NFC和Intent过滤器

[英]Nfc and Intent-filter

I wrote a nfc text tag like this 我写了这样的NFC文本标签

myscheme://company?page=2&poiId=140

Then i've created an Intent-filter like this in my manifest file in order to open my app (and then call a WS with informations from tag) 然后,我在清单文件中创建了一个这样的Intent过滤器,以打开我的应用程序(然后使用标记信息来调用WS)

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain" android:scheme="myscheme" android:host="company"/>
</intent-filter>

If i remove scheme and host params from data tag so everything works but by adding filtering it does not work anymore. 如果我从数据标签中删除schemehost参数,那么一切正常,但是通过添加过滤,它不再起作用。

Why ? 为什么呢

Is there a solution ? 有解决方案吗?

As corvairjo correctly wrote, " you are mixing the record type MIME type (here text/plain) with the record type URI into one intent filter. You should use only one. " 正如corvairjo正确地写道,“ 您正在将记录类型MIME类型(此处为文本/纯文本)与记录类型URI混合到一个意图过滤器中。您应该仅使用一个。

The point is that an NDEF record consists of a type information and a data payload (actually there's more than just those two fields, but those two should be enough to understand the concept behind it): 关键是NDEF记录由类型信息和数据有效载荷组成(实际上不仅仅有这两个字段,但是那两个字段应该足以理解其背后的概念):

+------+---------+
| TYPE | PAYLOAD |
+------+---------+

If you create a text record containing your URI, you will get something like this: 如果创建包含URI的文本记录,则会得到以下内容:

+------------+-------------------------------------+
| text/plain | myscheme://company?page=2&poiId=140 |
+------------+-------------------------------------+

The receiving device will interpret the record according to its type field. 接收设备将根据其类型字段解释记录。 Therefore, it will treat the payload ("myscheme://company?page=2&poiId=140") as a piece of human-readable text -- and not as a URI! 因此,它将把有效载荷(“ myscheme:// company?page = 2&poiId = 140”)视为一段人类可读的文本-而不是 URI!

On Android, this means that the record is detected as a piece of human-readable information with the MIME media type "text/plain". 在Android上,这意味着该记录被检测为MIME媒体类型为“ text / plain”的一条人类可读信息。 Hence, you can only catch it with an intent filter like this: 因此,您只能使用以下意图过滤器捕获它:

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

As the data payload is human-readable text, the intent filter cannot filter on any specific parts of that text (intent filters on Android can only match type information and URIs). 由于数据有效载荷是人类可读的文本,因此意图过滤器无法对文本的任何特定部分进行过滤(Android上的意图过滤器只能匹配类型信息和URI)。

So if you want to have an intent filter that matches on the URI, you have to wrap the URI into a record type that specifically identifies the data payload as URI. 因此,如果您想要一个与URI匹配的意图过滤器,则必须将URI包装到一个记录类型中,该记录类型专门将数据有效载荷标识为URI。 This is typically done using the NFC Forum URI record type: 通常使用NFC论坛URI记录类型完成此操作:

+---------------+-------------------------------------+
| urn:nfc:wkt:U | myscheme://company?page=2&poiId=140 |
+---------------+-------------------------------------+

Tag writer apps typically offer an option to write URIs/URLs onto tags. 标记编写器应用程序通常提供将URI / URL写入标记的选项。 The Android NFC API provides the method NdefRecord.createUri() to create a URI record. Android NFC API提供了NdefRecord.createUri()方法来创建URI记录。

You can then use an intent filter like this to trigger upon a tag that contains the record: 然后,您可以使用这样的意图过滤器来触发包含记录的标签:

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

You are mixing the record type MIME type (here text/plain) with the record type URI into one intent filter. 您正在将记录类型MIME类型(此处为文本/纯文本)与记录类型URI混合到一个意图过滤器中。 You should use only one. 您只能使用一个。

You can drop the MIME type definition and just go with the URI definition: 您可以删除MIME类型定义,而只需使用URI定义即可:

<data android:scheme="myscheme" android:host="company"/>

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

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