All posts
Android, Blog

Building A Relentless Android Alarm Clock. Part 1 – NFC

Radosław Makowiecki

Radosław Makowiecki

Ever feel like falling prey to the well known snooze syndrome in the morning? Do you have a strong resolve in the evening to get up early, and then an even stronger resentment towards actually having to get up? Struggle no more, for the tables are finally turning! The rescue arrives disguised as a relentless Android Alarm Clock that will get you out of bed for good. 

We are going to build an Android Alarm Clock App which requires the user to get up, get to the bathroom and tap the smartphone against an NFC tag, glued to your mirror, or wherever you want it, in order to stop ringing. The first part of this blog is going to concentrate around mere NFC technology handling, while the latter is going to make sure it is almost impossible for the user to make the phone stop ringing in any other manner than expected. Now, how cool is that?

Technical background of the NFC technology

We will start with some theoretical background. Near field communication is based off of Radio Frequency Identification, or RFID, which has been invented in the early 1980s, and is widely used to this day, i.e. in real time location tracking systems, libraries and access control systems, to name but a few. Sony and a company called NXP Semiconductors then invented the new NFC technology in 2002. The first Android device to ever incorporate NFC was the Nexus S, manufactured by Samsung.

In time, more and more manufacturers began conforming to this emerging standard, opening many more opportunities that we experience today, i.e. NFC payments, providing contextual information or bonus content in the form of tags built into posters and various adverts, in much the same way that QR codes have been used for several years now.

Alongside the rising popularity of NFC, came the standards of formatting the accessible information stored in tags. The most widely used of these is the NDEF standard, formed by the NFC Forum. This is also the standard we will conform to.

3 types of intent filters within NFC

Android offers many usages of NFC; it is even capable of emulating a payment card. Due to the many various usages and standards within the NFC itself, there was a necessity of defining three types of intent filters, to take on the intents dispatched by the system. These are:

Our app will incorporate the first and the last of these.

The following chart shows how Android handles incoming NFC intents:

Creating an Android Alarm Clock App using NFC

For more thorough information, please take a look at the official documentation.

 

5 steps to create your Android Alarm Clock with NFC

#1 Setup

Alright, enough of that history and background lesson, let’s get to work.

Let’s dive right into the code. What you are going to need, first and foremost, is to make sure you have a device with an NFC chip on the board. However, there’s no need to fear, as we will also check this during the runtime as well. First off, add these permissions to your AndroidManifest:

The latter one, as you may know, will ensure that only devices equipped with the proper technology will be seeing the app in the Play Store.

#2 Initializing the NFC adapter

Afterwards, an NFC adapter has to be initialized in your alarm Activity’s code.

Next, we are going to make sure that the device is equipped with an NFC chip, turned on via the user’s device, and can also act accordingly if it’s not, redirecting him/her to the system settings. We also check if the device is equipped with the NFC technology here, in case the user obtained the app the other way than from the Play Store:

#3 Intercepting the NFC tag intent

In order for your application to be able to react to the NFC tag intent dispatched by the system, we have to define the following intent filters for our activity:

Then the incoming intent, along with the data it carries, can be intercepted by your Activity like this:

By default, when dispatching the discovered tag, the system will prompt the user to indicate which application should be used to intercept said tag data. This behavior will not make it as seamless an experience as we wish it to be. Fortunately, the Android platform enables us to intercept the incoming intent in the foreground, meaning that the system is informed which activity is specializing in intercepting such intents.

However, there is an impediment to this. The foreground dispatch activity must be in its active state, brought to the foreground for the user, for security purposes. Add the following code to your activity, in order for it to be marked as a prioritized NFC tag intent interceptor:

#4 Reading NFC tag data

After we have tag data intent interception up and running, it`s time to decode it and make use of:

The method above ensures that the right type of tag, with the proper data, has been attached and stops the alarm if done just so. Now, let’s take a look at the actual decoding method, readNfcTagText() :

The bitwise operations make it seem dreadful, though it’s not very complicated and derives directly from how the NDEF data record payload is organized. Having a closer look at the data payload shows us that, in this case, it consists merely out of ASCII characters. ASCII code ‘2’ denotes the start of text, whereas decoding the remainder leaves us with a sequence “enalarm”.

The first two characters are an abbreviation of the language of the data being decoded, while the remaining characters make up the actual message encoded into a tag, which in this case was the word “alarm”. Such a message is then passed to the String constructor, letting us present that data to the user, or use it in another manner.

#5 Writing data to NFC tag

Writing data to a tag comes last, as it’s relatively the most complex topic to cover. We have to handle many cases, such as the technologies supported by tag, its capacity, data format, connection breach during writing and even the fact that the tag might be set to read-only mode.
Writing a plain text data into a tag can be done as follows:

The onNewIntent() method above is pretty self-explanatory. This time, we also have to cover cases in which the tag is not NDEF-formatted, hence the additional intercepted intent type applies. Let’s take a look at the actual writing method, since we already know that our tag is compliant to the most popular NFC tag specifications. The writing method compiles all the edge cases we potentially want to have covered:

Now, it’s time to look at the method which creates the actual NDEF message we want to put in the tag. As mentioned earlier, for more thorough information about the protocol, refer to this site. This however, might not be needed, as the applicable knowledge has been laid out earlier in this post.

Wrap up

NFC is not a particularly complex concept to grasp and implement, yet it can add huge value to your product. To my mind, the technology is pure fun, both during implementation and application usage.

However, there are certain limitations, which have to be kept in mind, i.e. NFC tag capacity. Also, for security purposes, Android only permits NFC scanning when the device is unlocked, unless your user has explicitly set an NFC tag to be a trusted device for SmartLock. Nonetheless, the range of possible NFC applications is still mainly limited by your imagination.

Don’t miss the second part of my guide dedicated to Android Alarm Clock. We will dive deeper into Android background to make your alarm almost impossible to be turned off without using NFC tag.

Sign for our newsletter below and stay tuned!

Newsletter

The post is created by Droids On Roids Team Member.
We would love to take care of your business.

Leave comment