Question

Is there a way to construct a NdefMessage in such way that:

  • If my app is installed on the receiver device, open the link in it (using a custom URI scheme, for example)
  • If my app isn't installed, open the default web browser with the link to the same page on the mobile website

I got these working separately but when I put two NdefRecord into a NdefMessage and set it to be pushed via Android Beam, the receiver device displays a chooser with these two records. You can click one of these and it will take you to the appropriate app (my app or Chrome). Is there a way to bypass this chooser and make it launch first supported link automatically?

Was it helpful?

Solution 2

If you don't need to use AndroidApplicationRecords, you can simply use a single NDEF record that points to your mobile page.

For instance if your mobile page is http://www.example.com/mypage, you would store this as a URI record in the NDEF message:

+-----------------------------------------+
| WKT:URI | http://www.example.com/mypage |
+-----------------------------------------+

Then, if your application is not installed, this will trigger the webbrowser on the mobile device.

To get this record to start you app, you would need to add a proper intent filter to your app's manifest:

<activity ...>
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:scheme="http"
              android:host="www.example.com"
              android:pathPrefix="/mypage" />
    </intent-filter>
</activity>

If you want to pass additional data to your application, you can -- of course -- add a second record (after the URI record) that contains you custom data. In that case, you NDEF message could look like this:

+-----------------------------------------+
| WKT:URI | http://www.example.com/mypage |
+-----------------------------------------+
| EXT:example.com:mytype | myparameters   |
+-----------------------------------------+
| ...                                     |

As the first record in that message is still your URI, the activity will continue to trigger on the existing intent filter. Within the activity, you can then retrieve the NDEF message from the intent and process the parameters from the second record/other records.

OTHER TIPS

No, there isn't.

But you have two workarounds here:

  1. Android supports so called ApplicationRecords which point to some specific package. If it's not installed, Android will open the play store to install the app.
  2. Just push the http:// uri via beam and let your app resolve this uris as well. You will get the chooser when "opening" the link the first time asking you to choose between browser and your app.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top