Things I have learnt about NFC on Android


Some months ago I started one of those projects you start, you hold, you retake, you forgot’em, you go back to… those projects that when you retake them you meet your past self, that self you are ashamed of because you see a technology newbie. You still appreciate him, but you’d have a chat about… “fancy a fifa party?”.

This outstanding project with a thousand breaks has been named NFCar, and it started as a test to begin in the NFC for Android world. The idea was to create a mechanism which would mod the phone to a specially designed desk for the car, from which to have better access to those apps you might use while driving, besides than showing your car’s logo. This is one of those projects you do it for yourself, and gets published at Market just because, without aiming it to get really used. If you got curious:

With this post I’m looking to share with you all I’ve learnt about NFC besides of reminding it to the future Roc, just in case something following is forgotten:

  • In Android 4.0 NCF chips cannot be formatted in NDEF, which is a quite cool standard to write in NFC. This has some important meanings: if you haven’t bought the NDEF pre-formatted tags, forgot about it. And if you hand write a single byte in a NDEF formatted tag, avoiding the standard, you’ll have broke the NDEF format permanently. #congratsson
  • Android apps cannot be proactive and ask if there is a NFC near. Tag connection is acquired via Intent, and you are responsible of saving the provided instance in order to use it further. That is to say, if the phone has sent you a signal telling you’ve got an NFC nearly, and you missed it, is lost. The NFC tag keeps near, and you know it, but you cannot access it. #congratsson
  • If you want to keep a connection with an NFC tag (because you know it will be nearly for a while), you cannot. I mean, you can’t set a listener “enDesconexióDeNfc” “onNfcDisconnected”. You can create yourself a method if needed to keep calling nfc.isConnected() periodically. This will work perfectly at home, with the phone USB connected to the computer… also when you test it with the phone disconnected… But, when the battery is low, quite far from your test results, it is possible to lose the connection with the NFC even with the device touching the phone. So, with the battery lower than 100%, your onNfcDisconnected listener will work for a while… 5 seconds… 30 seconds… even a minute. But there will come a time it will return false, and you won’t understand the reason. It’s simply because your battery isn’t fully charged. And this is not documented anywhere. And I am testing it with the galaxy Nexus, the Google’s beauty. #congratsson
  • The Android Developers Manual explains how to start the Activity through the Intent thrown when you bring the phone closer to a NFC tag. This is rather good, and when your app grows you will see that your main Activity is absolutely docked with the business is run over the NFC. This means that my main Activity where I like to have only view and user interaction, I have a lot of docked code managing the NFC control. Alright Google, the way of implementing NFC detection is sending me an Intent which launches one of my activities… but don’t try to convince me that this is good. Now that the project is finished I see it wouldn’t have been a bad idea to manage all the NFC connections through a see-through Activity which would forward an Intent to my man Activity depending on the case. So, summarizing: Google proposes a… doubtful arquitectre. #congratsson
  • Every NFC chip is written its own way. I don’t mind to write the bytes in 64bit packs to improve efficiency when able, or maybe to write using lines when the tag requires it… but I don’t relish to be obliged to learn how to work with every single new chip appearing. A “writeString” and a “readString”. That’s it. I’m not asking that much, am I? If the String is too long for that NFC tag, a TooLongMEssageException could be thrown. And that’s all. #congratsson
  • The mobile phone makes a sound every time an NFC is reached. The mobile phone turns off the NFC scan when the screen is switched off. The mobile phone triggers the NFC scan when the screen is switched on. These 3 elements which alone are not a mistake, altogether they are a patience attack when we work with my idea: phone near the NFC all the car trip along. Nothing happens when the screen turns off. But when you switch it on again… the sound again. The phone considers the NFC is new, just appeared there. And there is no way from your app to decide if it is new or not. The phone sounds definitively, and you cannot disable this audible notification anyway. Actually, not even user can. #congratsson


Despite these points: good enough. Actually, Android 4.0 has brought a very sexy detail: writing an NFC with an own Android Standard, so you can write there your app’s package. When the phone comes near this NFC no Intent is sent to any application, but the application is launched if installed. Otherwise it goes alone to the Market for it to be downloaded. Pretty good! This functionality brings a bunch of possibilities. Fetch your phone near the Bicing station and download the official app. Or don’t.

When I’ll have a while I’ll upload the code to Google Code for you to see what I’ve written. I hope it to be very useful, and specially to avoid you from failing as I’ve failed. Stay tuned to this post! :·)

Special thanks to Saúl Prior for translating this article to english! :·)