我正在尝试使用Samsung S5阅读2个不同的NFC标签。两个标签都包含NDEF消息,第一个标签包含MIME类型记录作为其第一个记录,第二个标签包含备用载体记录(TNF = TNF_WELL_KNOWN,Type = RTD_ALTERNATIVE_CARRIER)作为其第一个记录。
当我使用ACTION_TECH_DISCOVERED
意图通过前台调度读取标签时。对于第一个标记,技术列表列出了NfcA
,MifareClassic
和Ndef
。对于第二个标记,它会列出NfcA
和Ndef
。
当我尝试使用ACTION_NDEF_DISCOVERED
意图使用数据类型" * / *"读取标记时,第一个标签被发现很好,但根本没有发现第二个标签。
答案 0 :(得分:3)
这里的问题是NDEF_DISCOVERED
意图过滤器的工作原理。使用NDEF_DISCOVERED
,您可以查看某种数据类型(即MIME类型)或某个URI。在所有情况下,匹配将应用于已发现标记的NDEF消息中的 first 记录。
使用数据类型匹配,您可以检测到
使用 URI匹配,您可以检测到
两种匹配类型都是互斥的,因此您可以在一个intent过滤器中匹配数据类型或URI。
对于第二个标记,NDEF意图调度系统不支持第一个记录的类型(TNF_WELL_KNOWN + RTD_ALTERNATIVE_CARRIER)。因此,您无法将NDEF_DISCOVERED
intent过滤器与该标记结合使用。
匹配数据类型:
:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="some/mimetype" />
</intent-filter>
:
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
ndef.addDataType("some/mimetype");
匹配网址:
:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="somehost.example.com"
android:pathPrefix="/somepath" />
</intent-filter>
:
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
ndef.addDataScheme("http");
ndef.addDataAuthority("somehost.example.com", null);
ndef.addDataPath("/somepath", PatternMatcher.PATTERN_PREFIX);
匹配NFC论坛外部类型:
:
<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="/com.example:sometype" />
</intent-filter>
:
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
ndef.addDataScheme("vnd.android.nfc");
ndef.addDataAuthority("ext", null);
ndef.addDataPath("/com.example:sometype", PatternMatcher.PATTERN_PREFIX);