Saturday, July 16, 2011

Android NFC Sleep/Wake Toggle

I experimented a bit with the NFC chip on my Nexus S to solve a problem I had when going to bed at night. My usual routine was to set the phone ringer to silent mode so that nobody wakes me up with a late night drunk call. Then I deactivated my WiFi connection and after that I activated the alarms on my alarmclock. When I woke up the next day (hopefully after my first set alarm) I had to disable my fallback alarm, switch on my WiFi connection and turn the ringer on the phone to normal mode. As this was kind of tedious, I decided to implement a solution with the help of NFC.

NFC or Near Field Communication is a short range wireless technology for data exchange. Data can be written to and read from NFC tags and cards. Those are passive components, meaning that they don't have a battery to operate but draw their power from the magnetic induction happening when a powered reading device is near them. That's what happening when you touch such a tag with your NFC enabled phone.

There is a great talk from this years Google I/O explaining the benefits of NFC and how to incorporate it into your apps.

What you will need in order to tinker around with NFC are rewritable tags, a NFC enabled phone and an app which can write to the NFC tag. I found the Mifare 1K tags to work just great. The most popular and almost only NFC enabled phone right now would be the Nexus S. To write to a rewriteable tag or card you can use the NXP TagWriter App. NXP is a semiconductor company which has broad experience with wireless technology.

So why the need to write data to the tag? Because we want only our app to react on the tag discovery and not other apps which are registered in the system. This can be done by writing an own URI scheme to the tag which is used as an intent filter later on in the Android app. I just wrote http://marioboehmer.blogspot.com to the tag. The intent filter in my applications Activity looks like that:
<intent-filter>
   <action android:name="android.nfc.action.NDEF_DISCOVERED" />
   <data android:scheme="http"
         android:host="marioboehmer.blogspot.com" />

   <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
So what happens is that the system determines what data is send by the tag and calls out for applications which can handle the NDEF_DISCOVERED event and are registered for that specific URI scheme. If I would have let it out, I would get a chooser window on my device where I had to choose which app I wanted to start. This would destroy the touch and go metaphor of NFC.

As I told in the beginning of the post I used this intent filter to launch my app which does all those tedious tasks for me.
If I launch my app manually I can configure the following settings.

 
If it gets launched by NFC it processes my tasks according to my personal settings and my configured daytime and nighttime thresholds. Here is a short demo video of the app:

video

You can see that I configured day time to be after 06:00 and night time to be after 18:00. Since it was already after 18:00 when the phone discovered the tag, the ringer was turned into silent mode, the WiFi was turned off and an Alarm was configured for 19:21 in this case (normally you would configure an alarm for the next morning). In the second run I configured the night time to be after 20:00, so that the phone was set to day mode on detection. You saw that the ringer was turned back to normal mode and the WiFi was reenabled.

Since it might be of interest for other NFC phone owners as well, I plan to polish it up a bit and upload it to the market for free. Afterwards I will publish the source on github and post a market link here, so stay tuned.

1 comment:

  1. This is my first time visit here. From the huge amounts of remarks on your articles,I surmise I am not just one having all the pleasure right here! RFID tags

    ReplyDelete