In part 10 we’re going to cover two wireless connectivity capabilities – NFC and 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.
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):
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:
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:
- Having an invocation target that receives NDEF tags.
- 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: