Post Format

How does the NFC-Hunt App from Google works?

Das Android-Maskottchen: ein grüner Roboter

Google organized a kind of scavenger hunt for the developers conference IO 2013 to demonstrate the practical use of NFC. Participants needed an NFC-enabled Android and then installed an app called “NFC Hunt”. The app gives clues where to find specific NFC tags in the real world. The user has to find and scan these tags, and then the app give clues for the next station.

Google has published the source code of NFC Hunt app. So we developers have a fine example of a complete NFC application. But when I tried to understand the source code I noticed gaps in the documentation. Just the interesting parts are not explicitly explained: how can I make my app listen to NFC tags and how do I respond to the detected tags. Here now is the result of my research:

Diagram with Manifest, Android-OS, NFC-Tag, NFCShimActivity and ClueActivity

Step 0: The app uses the manifest to tell the system that it wants to handle NFC messages. This happens without the app running. The manifest defines which Activity gets the NFC data:

<activity android:name=".NFCShimActivity"
            android:theme="@android:style/Theme.NoDisplay">      
<intent-filter>
  <action android:name="android.nfc.action.NDEF_DISCOVERED" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="https" android:host="nfchunt.appspot.com"
                    android:pathPrefix="/f" />
</intent-filter>
	...
</activity>

In addition, we need to request the permission to use NFC at all:

 <uses-permission android:name="android.permission.NFC" />

For some Apps NFC is only an optional feature. But our app does not make sense without NFC-capable hardware, therefore there is the following entry:

<uses-feature android:name="android.hardware.nfc" android:required="true" />

Step 1: The NFC tag contains data in the NDEF format. In our case, this is a “bookmark”, a URL. The URL has a pattern: the front part is always the same and the end defines different data, just like web developers use it for HTTP GET.

http://nfchunt.appspot.com/f?c=CLUENAME

The system recognizes the NFC tag, analyses the type of message and determines which app is responsible. In case that our NFC-Hunt is not installed yet, the browser would start, then load the specified URL and display a page with the description of the game.

The system recognizes the NFC tag, analyses the type of message and determines which app is responsible. In case that our NFC-Hunt is not installed yet, the browser would start, load the specified URL and displays a page with the description of the game.

Step 2: The system calls our Activity as specified in the manifest. In our app a minimal activity without UI is defined: the NFCShimActivity.

This Activity analyses the received data. It puts the data in an Intent. The Intent is now sent to the actual Activity with the UI. (Step 3)

This intermediate step allows to decouple the external NFC data and formats from all what is happening in the rest of the app. We even have the option to send the Intent to an entirely different app to give this app new NFC capabilities.

Step 4: The system displays the actual Activity, in our case the ClueActivity. There is something we need to care of: If the Activity is already visible, then Android will start a second instance of Actvity and you see it pop up in a second window.

In our case an already visible screen should remain visible, it should just be updated with the newly received data. Therefore, the Activity gets in the manifest attribute

android:launchMode="singleTask"

Usually we get access to the data of an Intent in onCreate () – method, now onCreate () is not called a second time. We must therefore overriding the method onNewIntent (). We store the transferred data there.

public void onNewIntent(Intent intent) {....}

In the following Android calls the onResume () method again. In the method we finally respond to the data, change the game status and show the found tags with updateTagDisplay(). (Step 6)

Conclusion

The approach demonstrated in NFC-Hunt is only one way in which you can interact with NFC tags. For many applications it provides a useful framework.

Author: Karsten Meier

Weil ich gerne Probleme löse bin ich Informatiker geworden. Für meine Kunden berate und konzeptioniere ich und entwickle mit fast allem, was einen Prozessor hat. Sie finden mich auch auf Twitter

Leave a Reply