BlackBerry Java to BlackBerry 10 Cascades Porting Series – Part 10: NFC and Bluetooth

Native SDK Development

In part 10 we’re going to cover two wireless connectivity capabilities – NFC and Bluetooth.

TITLE_IMAGE

Bluetooth

Serial Port Profile
Serial Port Profile [SPP] lets you transmit/receive arbitrary data – you can talk to many peripherals, including printers, signature capture pads, or even other phones.

Handling SPP communications is easy on BlackBerry 10. As you’d expect on the POSIX system – it’s done through files. You can read and write to an established SPP connection as you would any other file.

Creating an SPP connection to a server is as hard as:

int bt_spp_open(char *addr, char *service_uuid, bool nonblock);

Creating an SPP server is as hard as:

int bt_spp_open_server(char *service_name, char *service_uuid, bool nonblock, void (*callback)(long param,int fd), long param);

Oooh! Callbacks! C’s “idea” of events… That function is called as soon as a peer connects to you.

However, there is some initialization to take care of. I strongly recommend looking at the demo the Bluetooth development team built here.

Object Sharing
The invocation framework is a huge help here. By sharing, you get the data sent to a Bluetooth peer with very little work.

  InvokeManager * im = new InvokeManager(this);

  InvokeRequest request;
  request.setAction("bb.action.SHARE");
  request.setTarget("sys.btviewer");
  request.setUri("file:///tmp/test.png"); //  not recommended location!
  im->invoke(request);

Bluetooth Low Energy
While BlackBerry Java devices didn’t support Bluetooth Low Energy, BlackBerry 10 does. If you’re working with some LE peripherals or just interested, have a look at this great primer (and demo):

http://supportforums.blackberry.com/t5/Native-Development/BlackBerry-10-Bluetooth-LE-primer-for-developers/ta-p/2287377

NFC

NDEF Tags:

Reading

Reading tags is now handled by the invocation framework. The registration in your bar-descriptor.xml is all you need to have configured – you don’t need any NFC/NDEF specific code to get the payload of a tag. Reading tags is much simpler in BlackBerry 10 than previously.

Let’s break down the invocation target that was used for the Scavenger Hunt application used at some recent BlackBerry Live events:

   
  <invoke-target id="com.rngs.BlackBerryJam.Americas.ScavengerHunt">
     <type>APPLICATION </type>
     <filter>
       <action>bb.action.OPEN </action>
       <mime-type>application/vnd.rim.nfc.ndef </mime-type>
       <property var="uris" 
          value="ndef://2/application/com.rim.devr.2012.bbja" />
     </filter>
   </invoke-target>

invoke-target
	The target id has to be globally unique… 

mime-type
	This is the type we have set internally to specify NDEF tag reading events.

property - value

The value here is where your control comes from. If your URI is specific (like the one above) you’ll only get that content. If your URI is just ndef:// – you’ll get any and all NDEF tags read by the device (assuming the user chooses your application – instead of some other built in one).

The 2 in the URI is the NDEF TNF! In this case, 2=MEDIA. (Some purists may argue that I should’ve used type 4=EXTERNAL – but I didn’t )

The TNF for WELL_KNOWN is 1. The type for Smart Posters is Sp – given this, the URI for accepting Smart Poster tags is:

ndef://1/Sp

Easy!

Want to keep all of your tag reading (and creating) in QML? Then all you need is a JavaScript library to process the received payload from the invocation event, which can be found here.

Writing a NDEF Tag
There are a few ways to write tags. I believe that the quickest is through sharing with the invocation framework.

You can build an NDEF tag duplicator by:

  1. Having an invocation target that receives NDEF tags.
  2. Sharing the NDEF tag payload – to keep on writing tags.

From an aesthetic point of view, sharing interrupts your application when the system card takes control – you can write using low level functionality too.

Other Tech (Peer 2 Peer, SNEP, Card Emulation…)
Want more details – or to see some samples? For NFC on BlackBerry there is only one place to go:

http://supportforums.blackberry.com/t5/Java-Development/NFC-Article-and-Code-Index/ta-p/1538775

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus