8

According to this answer, and validated by testing, when you use Android Beam to push over an NFC message containing an AAR record, the receiving device will start the MAIN/LAUNCHER activity for the app specified in the AAR.

That MAIN/LAUNCHER Intent does not contain the NfcAdapter.EXTRA_NDEF_MESSAGES extra. Hence, the data that we went through all the trouble to beam over appears to be lost if you use AAR.

Is there some way to get the NFC messages that triggered the app to be started in this scenario?

And if the answer is "no", then what is the use case of AAR? I can see where it might be helpful when the desired app does not exist on the receiving device (brings up Play Store), but then once the app is installed, AAR foils any attempt to deliver data from one device to the other, which is kinda the point behind NFC.

Thanks!

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491

2 Answers2

7

At the risk of answering my own question, one recipe for getting this to work (apparently) is:

  • Have the Beam sender use an NFC message with two NFC records, the first containing something for a unique MIME type, the second being the AAR

  • Have the Beam recipient have an <intent-filter> on the activity that responds to the first NFC record, such as via:

        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    
            <category android:name="android.intent.category.DEFAULT"/>
    
            <data android:mimeType="application/vnd.commonsware.webbeam"/>
        </intent-filter>
    

If the app already exists, the NDEF_DISCOVERED Intent will be used, and the recipient can pick up the NFC message and pull out the data from the initial record. If the app does not exist, the AAR will kick in, bringing up the Play Store (whether your app is distributed through the Play Store or not).

This is the recipe shown in the Android Beam example on the developer site.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Carefully reading the docs : http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#aar Suggest that No matter where your AAR is (first or second) the whole intent will be passed to your application, if you also have a matching MIME type in the intent filter. So I guess your answer would work also if the AAR is the first record. – Taiko May 01 '14 at 10:42
  • Is there any way to get the payload back if the receiving device does not have the app installed? I was hoping that after they do install, that they would somehow still get the intent. – hooby3dfx Nov 08 '15 at 17:06
  • @hooby3dfx: Sorry, I never bothered trying to get the AAR records working, so I have no idea. – CommonsWare Nov 08 '15 at 17:09
0

From a look at the docs it would seem the use-case is guaranteeing that your application is launched from the NFC event. A second use-case would basically providing a way for the phone to know which app it needs to read a tag.

Couple of questions If you use two records: first normal, second AAR how does the activity get launched? I would expect you'd be started via the tag dispatch intent. Does the AAR intent contain anything that gets you the Tag? I'm thinking you could manua

MrChaz
  • 1,065
  • 1
  • 8
  • 17
  • "the use-case is guaranteeing that your application is launched from the NFC event" -- so they don't have to click on a launcher icon? Big whoop. "use-case would basically providing a way for the phone to know which app it needs to read a tag" -- except that it *doesn't* "read the tag" in any meaningful fashion, if we can't get our data out of the tag. "If you use two records: first normal, second AAR how does the activity get launched?" -- that is precisely the scenario I am trying, and `MAIN`/`LAUNCHER` is invoked, even if the activity is already running in the foreground. – CommonsWare Jun 04 '12 at 21:19
  • The first use-case certainly isn't a powerful one. the second one I'm talking where the target phone doesn't have the app in question. I'll try and take a look a bit later in the week when I have access to another 4.0 device – MrChaz Jun 04 '12 at 21:33