galaxy S3 NFC

You probably found some use cases with NFC tags with your app ?
( or maybe you just intend to build an Android NFC Application )

My practical example :

I have developed an Android application for la minut’rit ( available on Google Play France only though ) and as an application giving the opportunity to buy service directly from your mobile phone.
I purposed to add some « flashy » feature : create a big poster with nfc tags under prints so that users can get right on the correct screen while putting their phone on the picture’s item.
I find it « cool » and quite user-friendly for people that want to have see everything on a wall and then just pick the right one with their phone.

What you need :

  • An android phone with NFC chip ( Samsung Galaxy S3 or a Google Nexus  for example)
  • some nfc tags ! ( just google « buy NFC tags » and you will find websites to buy some )
    Be careful, crappy nfc tags doesn’t store a lot of bytes and might not support locking feature ( or even authentication )

So How Did I do that ? :

First, start by declaring NFC permission in your AndroidManifest.xml :

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

Ok, as an example, here are the 2 activities I use in my app :

You might have already guessed that the first one is for reading your NFC Tags whereas the second is for writing & locking them.
I personally don’t use the writing one for the public version as you don’t want people to write your stuff anywhere ( even they could with some others apps but that’s not your problem ).

Mime type, the way to have your app recognize only your tags :

One thing that is usually really annoying is to have your nfc tags recognized by others applications but also having your have in a selector when the user tap a random nfc tag…
So the way to « fix » that is to use mime type on your nfc tags. Practically, it’s just a prefix in your data field.

The filter is done by Android through the AndroidManifest and the activity declaration :

android:name= ».nfctag.NFCTagReadingActivity »
android:label= »@string/app_name »
android:launchMode= »singleTop »
android:screenOrientation= »portrait » >
<action android:name= »android.nfc.action.NDEF_DISCOVERED » />

<data android:mimeType= »application/fr.laminutrit » />

<category android:name= »android.intent.category.DEFAULT » />

I think you noticed the bold line : That’s how I declare that my app receive only nfc tags starting by application/fr.laminutrit. You aren’t forced to use your package name as your « identifier » but I find it quite convenient.

Closer look at the NFC Tag reading activity :

The activity will :

  • start and listen for an Intent
  • read the Intent that contains the NFC Tag data
  • do the interpretation
  • start the next Activity through providing the correct intent with the data found in the NFC Tag

It’s as simple as that. ( isn’t it ? )

As you can see I just used a simple integer that is in fact the reference of the product in our system.

Now the question would be … how to write it ?

Closer look at the NFC Tag writing activity :

NFC Tag writing activity is a little bit « harder » than its reading counterpart. Indeed, I used an AlertDialog to allow to lock after writing ( something you might want when you’re doing some production nfc tags ! ).

Nothing special in the activity declaration :

android:name= ».nfctag.NFCTagWritingActivity »
android:label= »@string/app_name »
android:launchMode= »singleTop »
android:screenOrientation= »portrait » >

To start the activity, use the following lines :

Intent intent = new Intent( myContext, NFCTagWritingActivity.class )
intent.putExtra(« lock », false); //put true if you just want to lock and not write
intent.putExtra( « nfc_id », itemId); //itemId is your integer you want to write, you can set whatever you want if you put « lock » on true

Write something else than a simple ID :

Writing a single field isn’t that convenient for a more complex setting.
So check the getIdAsNdef function and put anything you want to write on your NFC Tag into the String msg.
Always remember that your message can be too long if your NFC Tag is too « small » ( some are only 170 bytes long ! ).
You can try to use some JSON representation for the mapping but it will often be too long for your nfc tags…
I suggest you to go the « dirty way » with some chained key/value stuff if you can.

That’s all for now, have fun tinkering !

If you like that « tutorial », you might want to read the one on SlidingMenu & Google Maps & ActionBarSherlock & maven.

If you have any issue, just comment, I’ll try to advice you 🙂
Feel free to share & translate ( just mention the source ^^ )