Filed under Guides, Junction
As NFC finds its way into more and more phones, developers can start thinking about how to use it to make their applications more viral, more engaging, and easier to use. Android’s recent release of Ice Cream Sandwich adds some APIs to help developers make full use of NFC in compelling ways. Building the best possible NFC experience can be tricky, and we offer a few ideas to help get you there. (Full disclosure: I spent a few months interning with Android’s NFC team and got to help develop some of these APIs.)
This post is a quick guide to help you wrap your head around some of the NFC basics, applicable across platforms. For a detailed look at how NFC is implemented on Android, please see the SDK documentation.
NDEF: a primer
NDEF (NFC Data Exchange Format) is the fundamental data unit of an NFC interaction. An NFC event occurs when two devices come within range of each other, and either device may send or receive an NDEF message. NFC events can be between two active devices such as two phones, or between an active and a passive device such as a phone and an nfc tag or sticker.
An NDEF message is composed of one or more NDEF records. These records contain three primary pieces of information: (1) a “type name format” (TNF), indicating how the record’s bytes are to be interpreted, (2) a type field defining the context of that data, and (3) the actual payload of the record.
The TNFs are best understood by enumeration:
- Absolute URI: The record is formatted as a uri
- Well-Known: The format of this record is specified by the NFC Forum
- Mime: The record is of a well-known mime type, defined outside of the NFC Forum
- External: The record is formatted according to some external specification
- Empty: The record contains no data beyond this TNF field
With this field set, the type field further defines how to interpret the bits. For example, if the record is for a mime type, we must know which mime type it represents– text/plain, image/jpeg, etc. Well-known types defined by the NFC forum include “Smart poster”, text, and uri.
Warning! There are overlaps in functionality across these TNFs. A text snippet could be represented as mime type “text/plain” or as the well-known type “text”. The actual bytes representing the text have different formats depending on the language, character codes, etc. A web URl could be represented as an absolute URI, but is better represented as the well-known type “uri”, which includes constants for common schemes like “http://” or “mailto:”.
NFC Interactions
An application on a phone can use NFC in a few different ways:
- Write NDEF messages to an NFC tag
- Read NDEF messages from an NFC tag
- Exchange data with another device
- Emulate an NFC tag (card emulation)
- Leverage the phone’s “secure element”.
Card emulation is available as an API on the Blackberry platform, but not on Android. Programming the secure element in a general way is a challenge and is not currently supported on either platform. Most commonly, applications will read and write tag data and exchange data with other devices.
A few months ago, the NFC Forum released a specification called SNEP. The goal of SNEP is to allow devices to communicate over P2P NFC across different platforms. As of Ice Cream Sandwich, Android devices communicate using the SNEP protocol, and hopefully other platforms will support it soon. In its most basic form, SNEP can be used to send and/or receive a single NDEF message across devices. While sending a single message may seem like a severe limitation, the functionality is extremely powerful and can in itself be complex to master.
Building an NFC Application
So how can you leverage this single NDEF message to do something cool in your application? Say Alice, your user, touches her phone to another device, be it a sticker or smartphone. If we take a snapshot of this scene, we can ask ourselves: what is running on Alice’s phone? Most smartphone operating systems have the user interact with one foreground application at a time. It may be your application, another application, or the platform’s home screen.
If your application is running in the foreground, you should think about how to handle various types of NDEF data. Often, your application will understand a limited set of data types. On Android, you can and should specify the type of data your application understands. If the user interacts with data that your application does not support, the operating system can find an application that does.
You should also think about the types of data your application might expose– A web browser can share its current webpage, a social network could allow for the exchange of contact information. If you cannot think of any appropriate data to share, you may want to share a reference to the application itself. In fact, as of Ice Cream Sandwich, Android applications will share a reference to themselves if no NFC functionality is defined— if Alice runs your application and touches another device, that device will either launch your application or be taken directly to its Market entry.
If your application is not running in the foreground, think about the types of data that should cause your application to be invoked. Again, Android lets you specify the types of data that should trigger your application by supporting the NDEF_DISCOVERED intent in your application’s manifest.
The NFC Forum dictates that the first record in an NDEF message defines the context of that message. In Android, this amounts to the platform dispatching records based on the type of this first record. Subsequent records are used to contain application-specific data payloads.
Educating your users about your application’s NFC functionality is a further challenge. Since NFC is a technology that essentially lives “behind the screen”, it is your responsibility to teach them about the added functionality, lest it remain an easter egg hidden within your application.
Cross-Platform Applications
Say your application is available for a variety of platforms: web, Android, Blackberry, Windows, iOS, etc. As mentioned, the first record is intended to define the NDEF message’s context, yet each platform may require a different type to invoke your application. To mitigate this, as of Ice Cream Sandwich, Android understands an “Android Application Record” (ARR), which can be located anywhere inside an NDEF message. The ARR can also be used to bring the user to your app’s market entry if it is not yet installed.
Extended Interactions
An NDEF exchange lets you send and receive only a single NDEF message. If your goal is to set up a long-lasting session between devices (for example, to run a 2-player multiplayer game), this is still enough information to get you going. We have developed a library for Android called EasyNFC which supports exactly this use case, helping you set up a Bluetooth link across phones. The library helps you get the best possible NFC user experience, launching the game regardless of whether both users have your application in the foreground or if only one does, and regardless of whether the application is installed on both devices or just one.
EasyNFC is an open-source library, and we’re happy to hear feedback and especially take contributions : )
- sich|nicht|auch|werden|nach|wird|einer|sind|noch|einem|haben|oder|aber|mehr|durch|sein|wurde|prozent|hatte|kann|gegen|knnen|schon|wenn|habe|seine|mark|ihre|dann|unter|soll|eines|jahr|zwei|jahren|diese|dieser|wieder|keine|seiner|worden|will|zwischen|immer|
- grossiste machine a glace italienne
- reihe
- Vydox Male Enhancement
- www.eventmap.ca