Introduction
This article is intended to act as a short and basic introduction to the world of NFC as it applies to BlackBerry® smartphones, such as the BlackBerry 9900 smartphone, and is aimed at developers who wish to take advantage of this exciting new technology. No prior knowledge of NFC is assumed but we do assume that the reader is familiar with Java® in some places.
The Authors
This article was co-authored by Martin Woolley and John Murray both of whom work in the RIM Developer Relations team. Both Martin and John specialise in NFC applications development (amongst other things).
What is NFC?
"NFC" stands for "Near Field Communications" and from one perspective is simply a very short range radio communications technology. The range of NFC communications is typically no more than about 4cm whereas in contrast, the range of Bluetooth® is several metres, and Wi-Fi® potentially hundreds of metres. NFC is a key element in something referred to as "contactless card technology" which allows small devices such as smart cards to interact with other devices merely by being moved into sufficiently close proximity with the other device.
Looking at NFC systems more closely however, reveals that this radio capability sits within an overall system architecture which consists of a variety of components and which has the capability to make possible an interesting and exciting range of types of applications. We'll explore this in the next section.
What can you do with NFC and BlackBerry devices?
There are all manner of use cases which NFC could support. Here are some examples:
The variety of application types that are possible is wide but on closer inspection it turns out that there basically are two classes of NFC Devices supporting these use-cases:
These two NFC Device types determine a set of permutations to which there corresponds three possible "modes of operation" in which NFC can be used:
The words "Card" and "Tag" tend to be used interchangeably. Technically they are both the same; however, contactless cards used in ticketing and payment today include additional technology to store secure data whilst the word Tag is generally used to refer to a passive devices such as a Smart Poster Tag.
How do users work with NFC?
To engage in an NFC use case users will hover their smartphone near another device (another NFC capable smartphone, a "smart poster", a contactless card reader or some other kind of NFC enabled device such as a Wi-Fi router). It's just possible however that while not strictly necessary, they will grow accustomed to actually tapping their device against the other device. There's no need to touch the other device from a technical point of view, but this very pro-active gesture of tapping one device against another has a very reassuring and satisfying feel to it and it is likely that this will catch on.
What is the architecture of a BlackBerry NFC solution?
The following diagram shows a somewhat simplified schematic of the architecture of the NFC system within applicable BlackBerry smart phones:
The key components depicted here are:
There are other acronyms shown on the diagram above and a few that have been deliberately excluded. Let's not worry about them for now. You'll find most of them defined in the glossary at the end of this article.
ISO Standards
A number of ISO standards apply to the world of NFC. Whether you need to know anything about them, a little about them or a great deal will depend very much on what kind of application you are involved with developing and what your role is. It's worth having an awareness that these standards apply at the very least though. Two key ISO standards to be aware of are ISO 7816 and ISO 14443. Both define various layers of a stack from the physical layer upwards and therefore break down into a number of parts. ISO 7816 is concerned with "electronic identification cards with contacts" and ISO 14443 is concerned with contactless cards. As you can see from the following diagram, ISO 7816 part 4 defines the APDUs used to communicate with a card or in this case an applet running in an SE via the "with contacts interface" and ISO 14443 defines the interface from the contactless side to external entities such as NFC point of sale readers.
How do I code my NFC application?
It depends on what you want to do. What you want to do will correspond to one of the three modes of operation mentioned above. There are also some differences between BlackBerry applications and J2ME MIDlets. We won't get into too much detail in this article but we will be following up with other articles which explore typical use cases and the associated APIs in more depth soon.
Working with smart posters / tags
Data stored in NFC tags must be structured according to something known as NDEF. NDEF stands for "NFC Data Exchange Format" and it defines message types, record types and encoding rules for various types of NFC tag content, such as telephone numbers, text, or URLs. You need to have some familiarity with NDEF to be able to work with the data structure. Ideally you should work with the appropriate NDEF specification, available from http://www.nfc-forum.org/specs/spec_list/ .
The BlackBerry Java APIs include a package called net.rim.device.api.io.nfc.ndef and this contains most of the classes and interfaces you will need. To have your application receive particular types of NFC tags when they enter the device's NFC field, you must implement and register an instance of the NDEFMessageListener interface. To register your application for a given type of NDEF message, you must call the addNDEFMessageListener method of the net.rim.device.api.io.nfc.readerwriter.ReaderWrite
public void addNDEFMessageListener(NDEFMessageListener listener,
int typeNameFormat,
String recortType,
boolean autoStart)
throws NFCException
Registers a listener to receive notifications when an NDEF message is read by the BlackBerry® device
Parameters:
listener - The NDEFMessageListener object
typeNameformat - An NDEF type name format.
recordType - An NDEF record types.
autoStart -Indicates whether the application should be started if not running.
Throws:
NFCException - When the listener cannot be added.
SecurityException - If an IT policy disallows NFC or NFC read/write.
ControlledAccessException - If read/write is disabled by application permissions.
Since:
BlackBerry API 7.0.0
Note however that while your NDEFMessageListener application will be automatically started if necessary when autoStart was set to true when registering, it must re-establish itself as a listener (by calling addNDEFMessageListener) again on starting otherwise even though it has been started, it will not receive a callback to the interface.
When your application receives a callback to the NDEFMessageListener's onNDEFMessageDetected method it will receive an NDEFMessage object as a parameter. You'll need to parse this message per the NDEF specifications and then act upon the message as you require.
Working with NDEF tags does not require use of either of the SEs by the way and is the simplest of the NFC modes to work with.
Card emulation
This is the most challenging of the three NFC modes to work with. Typically, card emulation is achieved by installing an applet in one of the two SEs. The applet will, amongst other things, emulate a given type of smart card. So for example, if your aim is to implement support for a given physical access system then you will need to establish details of the protocol used to "talk" between a physical access smart card and the readers attached to doors and their associated physical access control systems. You'll also need to establish whether or not the NFC system in your BlackBerry smartphone is compatible with the specific contactless communications protocols in use since there are many variations. We won't go into this in detail here but examples include ISO14443A, ISO14443B and ISO15693. Working directly with the supplier of the physical cards which you wish to emulate may be the only way to acquire this information.
Once you have an applet in an SE however, as an application developer (as opposed to *applet* developer) you will be concerned with a number of issues. Again, we'll be publishing articles which look at these issues in more detail, but to get you started, here are some key examples:
1. Talking to the applet on an SE
This breaks down into two problems in fact; how to select the SE which contains the applet you wish to talk to and then how to talk to that applet.
The first issue is dealt with using RIM APIs such as the SecureElementManager and SecureElement classes, both of which can be found in the net.rim.device.api.io.nfc.se package. Selecting an SE is achieved using code such as the following fragment:
SecureElementManager sem = SecureElementManager.getInstance(); SecureElement se = null; try
{ se = sem.getSecureElement(SecureElement.EMBEDDED); }
catch (NFCException e1)
{ // handle Exception }
Since only one SE may be active at a time, you also need to route particular protocols to your selected SE according to the various states the SE can be in. We won't go into detail on what all this means in this introductory article but this should give you the flavour of what is involved:
try
{ // Route ISO14443A to the Secure Element for battery on mode se.setTechnologyTypes(SecureElement.BATTERY_ON_MODE, TechnologyType.ISO14443A);
}
catch (NFCException e)
{ // handle the Exception }
Talking to an applet on an SE involves the use of a protocol defined in specification ISO7816 part 4. This specification defines APDUs (Application Protocol Data Units) or message types which can be sent as requests to or received as responses from applets stored on a smart card. Since an SE is essentially a smart card, it applies here too. As a BlackBerry Java application developer, you use the JSR 177 APIs to establish a connection to an applet on the SE and then exchange request and response APDUs with it. The key package is javax.microedition.apdu which defines the APDU protocol handler for ISO7816-4 communication to a smart card device and includes the interface APDUConnection. Here's a simple example:
// The JSR177 connection will use the APDUConnection object APDUConnection apduConn = null; // The connection string for the JSR177 connection String conStr = "apdu:0;target=41.5F.50.5F.44.5F.55.5F.41.70.71.01"; // Define the command to send to the Smart Card applet byte[] command = { (byte) 0x00, (byte) 0x04, (byte) 0x00, (byte) 0x00 }; try
{ // Open a logical channel to the application apduConn = (APDUConnection) Connector.open( conStr ); // Send an APDU and wait for a response byte[] ret = apduConn.exchangeAPDU( command ); // Close the logical channel connection apduConn.close(); }
catch( Exception e )
{ // handle the Exception }
2. Being informed of activity in an applet
An applet in an SE may function quite independently of any application on the BlackBerry smartphone. For example, exchanging messages via an adjacent external reader does not require an application's participation usually. The applet takes care of this on its own. But you may wish your application to be notified when certain events take place. The BlackBerry APIs include the TransactionListener interface in the net.rim.device.api.io.nfc.se package which you may implement and register for this purpose. Registration is achieved by calling the addTransactionListener method on a SecureElement object. An application which acts as a TransactionListener will receive call backs to its onTransactionDetected method when events take place within the SE or HCI but must be running to receive such call backs. It may be useful to implement a background application which auto-starts to act as a TransactionListener.
void onTransactionDetected (byte[][] aids)
Invoked when an external reader completes a transaction with an application stored on a Secure Element.
Parameters:
aids - Array of Application IDs used in the last transaction between an external reader and a secure
element.
Category:
Signed: This element is only accessible by signed applications. If you intend to use this element, please
visit http://www.blackberry.com/go/codesigning to obtain a set of code signing keys. Code signing is only
required for appplications running on BlackBerry smartphones; development on BlackBerry Smartphone
Simulators can occur without code signing.
Since:
BlackBerry API 7.0.0
3. Being informed of events in the NFC field
You may wish your application to be notified whenever something happens within the NFC radio field. For example, a reader or tag entering the field could be of interest to your application. To that end, the BlackBerry APIs include the net.rim.device.api.io.nfc.readerwriter.DetectionLi
void onTargetDeteted(Target target)
Invoked when a connection to an NFC target is detected. The connection to an NFC target is retrieved by the
following code: Connection.open(target.getUri(Target.NDEF_TAG)); Make sure that any
communication with the Target occurs on the main event loop. The BlackBerry® deice doses the
connection to the Target when the onTargetDetected method finishes.
Parameters:
target - An NFC target.
Since:
BlackBerry API 7.0.0
Peer to peer mode
Peer-to-peer mode NFC is not currently supported by the BlackBerry Java APIs as of version 7.0.
Code Signing
Your application will need to be signed in the usual way per any typical BlackBerry Java application. We have however introduced a couple of new signing keys, NFCR and RESE. You will need to sign your application with the NFCR key if you are using any of the classes or interfaces in the net.rim.device.api.io.nfc.se package with the UICC resident SE (SIM). Similarly you will need to sign with the RESE key if your application uses the net.rim.device.api.io.nfc.se package with the device embedded SE. Note that these signing keys are only available by application to RIM at this stage.
Summary
Hopefully this article has given you some insight into what NFC is about in general and the kinds of exciting things you can do with NFC and a suitable BlackBerry smartphone. The APIs are available today so download the latest BlackBerry JDK from
http://us.blackberry.com/developers/blackberry7/
APIs are documented here if you'd like to browse further: http://www.blackberry.com/developers/docs/7.0.0api
Other BlackBerry developer NFC articles
See the NFC Article Index for the list of other articles in this series
Glossary of NFC Terms