NFC tags

Guest post by John Murray

This is the second part of a six-part series by Martin Woolley and me on porting BlackBerry Java applications that use NFC to BlackBerry 10. In our last post, we looked at Tag Reading. This time, we’ll review the way in which you’d port code that writes to NFC tags.

Here’s where we are in the series as a whole:

  1. Reading NFC Tags
  2. Writing NFC Tags
  3. Peer to Peer Mode
  4. Reading NFC Contactless Cards
  5. NFC Virtual Tag and Card Emulation
  6. NFC Card Emulation

So without further ado, let’s take a look at the aspects you need to deal with:

Key issues for the Java developer

The general pattern you’ll have followed in your Java application will have been to address the following tasks:

  1. Detecting the tag: The application must implement the DetectionListener interface and register it with ReaderWriterManager.addDetectionListener(…). When a tag comes into proximity with the device, there will be a call-back to the DetectionListener.
  2. Creating tag content: To create tag content, we create an NDEFMessage object using methods in NDEFMessageUtils such as createUriNdefMessage (available from BlackBerry 7 device software version 7.1), or by manually create the byte[] payload and building NDEFRecord and NDEFMessage objects.
  3. Writing to the tag: When a tag is found to be in proximity of the BlackBerry device, as indicated by a call-back to the DetectionListener interface, we create an NDEFTagConnection to the Target object indicated in the call-back and then write the tag content to it that we’ve already prepared.

The BlackBerry 10 Native approach

To perform the corresponding tasks in BlackBerry 10, you can take one of two approaches. You may either:

a) Use the Invocation Framework. That is, find another application that can provide a “tag writing service” that your application can use, or:

b) Use BPS (BlackBerry Platform Services) to detect and then write data to the tag.

Writing tags using the Invocation Framework

The easiest way to do this is directly from QML using the QML InvokeActionItem component. The code fragment below shows how to do this. Note that the tag content itself must still be constructed, as an array of bytes, from C++ and, in this example we’re calling our own application code via _ndefFactory.getNdefTextMessage() to accomplish this. We’ll explain this in more detail below.

In our example, we construct an invocation framework query whereby we express our need for an application that can “SHARE” data of MIME type “application/vnd.rim.nfc.ndef”. This means we’re looking for an application that can write tags on our behalf. When the action is invoked, we call our _ndefFactory C++ method to create the payload and assign the result to the data attribute of the InvokeActionItem component. Our C++ code is shown below and you can see that the QtMobilitySubset API was used to help create the tag content in the NDEF format.

Writing tags using BPS

BPS processing takes place inside an event loop. Detection of a tag is indicated by delivery to the event loop of a particular type of event object. To receive such events, we must first register our interest by calling:

On receiving an event with the event code NFC_TAG_READWRITE_EVENT we proceed to construct our tag content in NDEF format and then write it to the tag via a Target object. The following code fragment illustrates this.

So there you have it. Hopefully our exploration of this topic will make it nice and easy for you to port your BlackBerry Java application over to the world of BlackBerry 10 native apps. Good luck!


Knowledge Base Articles:

Sample Code:



View article – 

Porting BlackBerry Java NFC Applications to BlackBerry 10 Part 2: Tag Writing