What does enableForegroundDispatch and disableForegroundDispatch do?
Asked Answered
B

1

8

I read the documentation and I don't quite understand what either do. Considering Android made the puzzling decision that we now need to use Android Beam to send data from 1 one phone to another and there is no way to simultaneously send data from both to both, I don't see the use.

Can't I just call setNdefPushMessage on one phone, and have an onNewIntent callback in the other phone which does something if NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) is true?

What is the point of enableForegroundDispatch and disableForegroundDispatch?

Blackheart answered 15/11, 2014 at 8:12 Comment(0)
S
17

enableForegroundDispatch gives your current foreground activity priority in receiving NFC events over all other actvities.

For instance, consider the following example:

  • Your activity and another activity (either from the same app or from another app) registered an intent filter for the same NDEF record type in the manifest.

  • Your device receives an NDEF message containing that record type as its first record.

  • If the current foreground activity did not register with the foreground dispatch system, an activity chooser will be shown and the user can choose between the two activites.

  • If the current foreground activity, however, did register with the foreground dispatch system (to receive that type of record), it will get priority over all manifest-registered intent filters and will receive the NDEF message without any additional user interaction.

Some more things:

  • The Android Application Record (AAR), if appended to an NDEF message, will have a similar effect as it will force the NDEF message to be delivered only to a particular app.

  • If an NDEF message contains an AAR for a different app, you could still use the foreground dispatch system to force delivery of the NDEF message to your app. So the foreground dispatch has priority over the AAR.

  • Note that the foreground dispatch system is not only used for peer-to-peer mode data exchange, but also for reading NFC tags. In that case, there exist tags that do not contain NDEF messages and are, therefore, significantly more likely to result into multiple activities being registered for the same tag type. Thus, in that case its again useful to give your activity priority over any other activities that are registered for the same tag type.

Spectacle answered 22/11, 2014 at 8:9 Comment(2)
So what if the activity is not in the foreground, how do we programmatically register an activity to be launched for an NFC tag, without having to use the Manifest way (which I don't think works properly, because Chrome always hijacks my NFC)Rubenrubens
@j.k. Manifest is the way to go. That's how Android works. Chrome would only hijack if your filter were not specific enough. You might want to post a separate question on that...Spectacle

© 2022 - 2024 — McMap. All rights reserved.