find milan

Guest post by Martin Woolley

Our buddy Rob Williams included some words of wisdom on porting BlackBerry OS Java applications that read or write NFC tags in a blog post that embraced both these aspects of NFC and Bluetooth, a few months back.

John Murray and I would like to circle back on the subject of porting BlackBerry Java NFC applications to BlackBerry 10 and cover the subject in more detail and across all the primary capabilities and use cases of NFC.

To that end, we’ll be publishing a series of six blog posts about porting NFC apps from BlackBerry Java to BlackBerry 10 native applications over the coming weeks, starting with this one. The full list of posts in this series will be:

  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 let’s take it away and discuss reading NFC Tags and how to turn BlackBerry Java apps that do this into BlackBerry 10 native apps.

Key issues for the Java developer:

  1. Registration

The application must implement the NDEFMessageListener interface and register it with ReaderWriterManager.addNDEFMessageListener(…)

  1. Invocation

NDEFMessageListener’s on NDEFMessageDetected method is called directly if the app is already running when tag whose content matches the type registered for is detected.

The application must handle being restarted when a tag is presented as a special case in the code.

  1. Decoding tag content

We decode tag content with classes in net.rim.device.api.io.nfc.ndef.rtd or direct from byte[] payload

Tags contain NDEFMessage and NDEFRecord objects

The BlackBerry 10 Native approach:

1. Registration

Applications must register for particular tag content with an entry in the bar-descriptor.xml file as shown here:

2. Invocation

When a tag containing content that matches the details registered for in the bar-descriptor.xml file is detected, the invocation framework determines which application to dispatch the tag content to and if it is not already running, launches it. Developers should use the InvokeManager class and signals/slots to receive a callback when this happens and extract the tag payload from the InvokeRequest object. Here’s an example:

3. Decoding tag content

There are two APIs to choose from:

3.1 Qt

BlackBerry 10’s APIs includes the QtMobilitySubset API. This contains some useful methods for handling tag data in the NDEF format. For example:

3.2 BPS

The BlackBerry Platform Services (BPS) APIs include methods intended for transforming byte [] format tag payloads into NDEF structures. Code fragments:

That’s it! Reading NFC tags using the BlackBerry 10 APIs is very easy, unarguably easier than it was using BlackBerry Java. Porting this aspect of your application should take very little time and effort indeed.

Resources: Helpful knowledge base articles can be found here.

Sample Code: Tag reading via Invocation Framework and then decoding using the BPS APIs is demonstrated by the NfcTool application can be found here.

Tag reading via Invocation Framework and then decoding using the Qt APIs is demonstrated by the NfcRaceTimeWay application can be found here.

Have questions or comments? Reach out to us on Twitter at @mdwrim, @jcmrim and @robbieDubya.

image

Link to article: 

Porting BlackBerry Java NFC Applications to BlackBerry 10 – Part 1: Reading NFC Tags