3

I use NFC to send text records from my app on device Nr.1 to my app on device Nr.2. Then the device is in touch, on both devices popup "touch to beam" UI. If I touch and beam message on device Nr.1 (sending part ot the pair) - message sent, everything working is just fine. But if I touch "touch to beam" UI on device Nr.2 (which should receive message) - then my app on device Nr. 1 is reloaded. Logcat is show nothing wrong, no errors. Seems, is that case app get some kind of empty NFC message? But why it's just do nothing on that? Why reload? I'm using ForegroundDispatch, create "text/plain" filters programatically, without manifest.

How I can handle this situation, then the users press wrong side of "touch to beam" UI??

Michael Roland
  • 39,663
  • 10
  • 99
  • 206

1 Answers1

0

If you do not set up Android Beam in your app, Android's default behavior upon beaming is to instruct the other device to either open that same app (first activity that filters for MAIN/LAUNCHER intents of an app with the same package name as the app on the source device), or if your app is not installed, open the Play Store listing. This is done by sending an AAR (+ a Play Store link?) over Beam.

So in your case, clicking the Beam UI on device 2 causes the AAR to be sent to device 1. As a result, your app will be (re-)opened on device 1. In order to overcome this, you can do the following:

  1. Enable foreground dispatch for the activity on device 1, so that the received NDEF message is immediately processed by your app. I'm not sure if this collides with the Beam UI though.
  2. Change the NDEF message sent by device 2 to something that does not contain an AAR using the Beam API.
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • I found one more "fun".. In my app manifest no activity filter for NFC messages.. But still - making beam from any aktivity (which have absolutly nothig common with NFC) of my app on both devices - one of app forces reload anover.. It means, if no definded NFC messages at all, app still send AAR message on beam? How can i "setup Android Beam" to avoid this?? – user3337390 Apr 24 '14 at 12:58
  • Correct, every activity that does not explicitly register an NDEF message for Beam automatically has a default NDEF message consisting of Play Store URL and AAR that is sent upon clicking the Beam UI. Also an AAR will trigger the first activity of your app that has an intent filter for MAIN/LAUNCHER if there is no activity that filters for an NFC intent or if there is no activity that filters for the first NDEF record of the received NDEF message. – Michael Roland Apr 24 '14 at 13:13
  • See my answer on how you can avoid this. – Michael Roland Apr 24 '14 at 13:14
  • Yes, thanks, for now i just make fake NFC message, and read it, if wrog press is accure. Not very nice way, but for now be good. – user3337390 Apr 25 '14 at 05:10