enableReaderMode和enableForegroundDispatch有什么区别? [英] What's the difference between enableReaderMode and enableForegroundDispatch?

查看:1012
本文介绍了enableReaderMode和enableForegroundDispatch有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了两种让Android应用检测和处理NFC标签的方法:

I found two approaches to let an Android app detect and process NFC tags:

  1. NfcAdapter.enableReaderMode(activity, callback, flags, extras),然后在回调中接收标签信息.

  1. NfcAdapter.enableReaderMode(activity, callback, flags, extras) and then receive the tag info in the callback.

NfcAdapter.enableForegroundDispatch(activity, intent, filters, techLists),然后通过onNewIntent(intent)活动方法接收标签信息.

NfcAdapter.enableForegroundDispatch(activity, intent, filters, techLists) and then receive the tag info in the onNewIntent(intent) activity method.

我目前正在使用第二种方法,但是最近我发现了enableReaderMode方法,并想知道使用该方法来处理NFC标签是否更好.

I currently use the second approach, however, I recently discovered the enableReaderMode approach and wonder if it's better to use that to process NFC tags.

那么enableReaderModeenableForegroundDispatch有什么区别?

推荐答案

前台调度系统

从Android 2.3.3(基本上是Android NFC的开始)开始就存在前台调度系统(NfcAdapter.enableForegroundDispatch()).因此,所有具有NFC功能的Android设备都支持此方法.

Foreground dispatch system

The foreground dispatch system (NfcAdapter.enableForegroundDispatch()) exists since Android 2.3.3 (which is basically the beginning of Android NFC). Hence, this method is supported on all Android devices with NFC capabilities.

前台分派系统用于在处理NFC发现事件(即从对等设备接收到的发现的NFC标签和NDEF消息)中提供当前处于前台优先级的活动.这意味着即使为特定标签类型或NDEF数据注册了另一个应用程序(通过AndroidManifest.xml中的意图过滤器),NFC事件仍将传递给前台活动,而不是该其他活动.因此,该方法不会更改Android侦听NFC设备(NFC标签,P2P设备)的方式,而只会更改处理发现的设备的优先级.

The foreground dispatch system is used to give an activity that is currently in the foreground precedence in handling NFC discovery events (i.e. discovered NFC tags and NDEF messages received from peer-to-peer devices). This means that even if another app is registered (by means of an intent filter in the AndroidManifest.xml) for a specific tag type or NDEF data, the NFC event will still be passed to the foreground activity instead of that other activity. Consequently, the method does not change the way Android listens for NFC devices (NFC tags, P2P devices), it only changes the priority for handling discovered devices.

阅读器模式API(NfcAdapter.enableReaderMode())在Android 4.4中引入.因此,并非所有具有NFC功能的Android设备都支持此方法.

The reader-mode API (NfcAdapter.enableReaderMode()) was introduced in Android 4.4. Consequently, not all Android devices with NFC capabilities support this method.

与前台调度系统相反,读取器模式API确实改变了Android侦听NFC设备的方式.读取器模式API禁用对等模式.例如,这允许您发现同时启用对等模式和卡仿真模式的其他设备的卡仿真模式(与Android HCE一样). (通常,这样的设备会被发现为对等设备,而Android应用将无法访问卡仿真功能.)

As opposed to the foreground dispatch system, the reader-mode API does change the way Android listens for NFC devices. The reader-mode API disables peer-to-peer mode. This, for instance, permits you to discover card emulation mode of other devices that have peer-to-peer mode and card emulation mode enabled simultaneously (as is the case with Android HCE). (Normally, such a device would be discovered as a peer-to-peer device and an Android app would not be able to access the card emulation functionality.)

此外,您可以更改NFC读取器模式的特定参数,例如您可以:

Moreover, you can change specific parameters of the NFC reader mode, e.g. you can:

  • 定义NFC读取器轮询的标签技术,
  • 通过向标签发送特定命令序列并检查是否仍收到响应,来定义Android测试标签是否仍然存在的时间间隔
  • 阻止Android自动向标签发送命令,以测试标签是否包含NDEF消息,
  • 在发现标签后阻止Android播放声音.

根据 Adam Johns 的评论,以上内容在Android 10上可能不再适用(他已经测试过在像素2上).当仅使用enableReaderMode()(不使用其他enableForegroundDispatch())时,即使将标签正确分配给已注册的读取器模式回调方法(onTagDiscovered()),设备似乎也会显示没有支持此NFC标签的应用程序"的祝酒词.

According to a comment by Adam Johns, the above may no longer be true on Android 10 (he tested on a Pixel 2). When using only enableReaderMode() (without an additional enableForegroundDispatch()), the devices seems to show a toast "No supported application for this NFC tag" eventhough tags are correctly dispatched to the registered reader-mode callback method (onTagDiscovered()).

这篇关于enableReaderMode和enableForegroundDispatch有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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