What does enableForegroundDispatch and disableForegroundDispatch do?

6.7k views Asked by At

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?

1

There are 1 answers

2
Michael Roland On BEST ANSWER

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.