What's the difference between enableReaderMode and enableForegroundDispatch?
Asked Answered
B

2

20

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

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

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

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.

So what is the difference between enableReaderMode and enableForegroundDispatch?

Buseck answered 10/11, 2015 at 15:36 Comment(1)
Did you end up ever using enableReaderMode?Stelmach
E
26

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.

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.

Reader-mode API

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

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.)

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

  • define the tag technologies that the NFC reader polls for,
  • define the interval in which Android tests if a tag is still present by sending a certain command sequence to the tag and checking if a response is still received,
  • stop Android from automatically sending commands to the tag in order to test if the tag contains an NDEF message,
  • stop Android from playing a sound upon tag discovery.

Note: Behavior may differ on Android 10

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()).

Evelunn answered 18/11, 2015 at 19:55 Comment(8)
Hey Michael, very useful info. Thanks! One more q : If I enable reader-mode in my foreground activity, does this means that it will also override the existing tag dispatch system as the foreground dispatch system does ?Whatnot
@Yashasvi Yes, reader-mode also overrides all other tag dispatch.Evelunn
@MichaelRoland, on Android 10 it doesn't seem to be true anymore that enableReaderMode overrides all other tag dispatch. I'm getting a toast message from Android OS when scanning a tag after calling enableReaderMode that says "No supported application for this NFC tag". However, I'm still receiving the tag in the onTagDiscovered callback and processing it as normal. Using enableForegroundDispatch prevents the Android OS toast from being shown though.Eiten
@AdamJohns Is that on a Google Pixel device or on a device by some third-party OEM?Evelunn
@MichaelRoland, Google Pixel 2Eiten
More details in my question here: https://mcmap.net/q/663130/-should-nfcadapter-enablereadermode-in-foreground-activity-override-the-intent-tag-dispatch-system/1438339Eiten
@MichaelRoland In my use case, the app should read NFC tags as well as android phones (by HCE). I'm currently using foreground dispatch system and it is detecting both the types. But the detection of android phones is intermittent across devices. On Nexus 9, it works 9/10 times, but on some Huawei and LG phones, detection is really bad. Should I switch to enableReaderMode or use both FDS and readerMode?Winze
@igorganapolsky yes. An NDEF message is only one possible specific data structure that you can retrieve from a tag.Evelunn
C
7

enableReaderMode: Limit the NFC controller to reader mode while this Activity is in the foreground.

enableForegroundDispatch: This will give priority to the foreground activity when dispatching a discovered Tag to an application.

So basically you can use both for the same purpose, which is reading/writing a tag. enableReaderMode is used by Android phones in combination with a Broadcom NFC controller, because there is a bug on the presence check. As far as I know, only the enableReaderMode can dodge this by increasing EXTRA_READER_PRESENCE_CHECK_DELAY.

Caesarean answered 11/11, 2015 at 9:36 Comment(4)
Is there still the mentioned bug in Android R?Stelmach
Can both be used at the same time reliably? I.e. using enableForegroundDispatch to give priority to the current activity (and thereby also blocking all other applications), but not actually listening to the intent. Then at the same time use enableReaderMode for the actual reading?Metatarsal
@Metatarsal did you end up using both of these? And for what purpose?Stimulate
@JohanFerreira No. After some testing it became clear that using only enableReaderMode is sufficientMetatarsal

© 2022 - 2024 — McMap. All rights reserved.