Talking With Your Arduino via NFC on BlackBerry

How-to

TITLE_IMAGE

Did you know that it’s easy to exchange data between your NFC enabled smartphone and your Arduino? It can be arbitrary data too – it does not need to be formatted as NDEF tags or ISO 7816-4 APDUs!

What does it take to get an Arduino that talks with BlackBerry running? Here’s what I used to make a very basic color mixing application that shares colors between the Arduino and the BlackBerry over NFC.

  • Some NFC enabled BlackBerry devices. I’m using my Bold 9900s. <3 the keyboard!
  • 1 Arduino. I’m using an Arduino Uno R3 (it’s under the shield)
  • 1 Adafruit Industries PN532 NFC/RFID shield. A great shield available here: http://adafruit.com/products/789
  • 1 BlackBerry application.
  • 1 Arduino sketch
  • A copy of the Adafruit NFCShield library. Note that I have made modifications to get the functionality that I needed; https://github.com/blackberry/Adafruit_NFCShield_I2C (pull request pending…)
  • Imagination. This is the most important ingredient! With these other ingredients you could build… anything! (Hit me up in the comments with your project!)

Arduino Sketch

Here are the guts of my sketch – stripped to the barest functionality… I need to ‘inList’ a passive target and then ‘inDataExchange’. If you can ‘inList’ a target – then you can send data to it…

if (nfc.inListPassiveTarget()) {
    while(true) {
      message[0] = 1; message[1] = r; message[2] = g; message[3] = b;
      uint8_t responseLength = sizeof(message);
      if (nfc.inDataExchange(message,sizeof(message),message,&responseLength)) {
        uint8_t command = message[0];
        switch(command) {
        case 1:
          if (responseLength==4) {
            r = message[1];
            g = message[2];
            b = message[3];
            showColor();
          }
          else {
            //Serial.println("Doesn't seem to be a color...");
          }
          break;
        default:
          ;
        }

The Adafruit samples show you everything you need to know about initializing the class.

The active NFC peer (the Arduino) is responsible for sending data first. You may have to keep on polling your passive peer.

Java

Again – just the basics. You need to create a VirtualISO14443Part4TypeATarget. You have to be aware that the target is only active when your app is in the foreground. The number below is not used by the API. this is an instance of VirtualISO14443Part4TargetCallback.

    emulated = new VirtualISO14443Part4TypeATarget(this, "10451045", null);
    emulated.startEmulation();

Then, in the callback I send the color that has been mixed on the screen (unless the user hasn’t chosen a color recently). The byte array returned from this method is transmitted over NFC.

private byte[] processCommand(byte[] command) {

        if (command == null) {
            return new byte[0];
        }
        int messageType = command[0] & 0xff;
        switch (messageType) {
        case 1:
            if (command.length == 4) {
                int colorIn = (command[1] & 0xff) << 16;
                colorIn |= (command[2] & 0xff) << 8;
                colorIn |= (command[3] & 0xff);

                if (colorIn != color) {
                    if (colorQueued) {

                        byte[] toReturn = new byte[4];
                        toReturn[0] = 1;
                        toReturn[1] = (byte) (color >> 16);
                        toReturn[2] = (byte) (color >> 8);
                        toReturn[3] = (byte) color;

                        return toReturn;

Existing BlackBerry samples and tutorials explain the listener really well.

The Result

Here’s a very short video showing several BlackBerrys sharing colors with the Arduino. I built a paper cube around my LED to try and make it easier to capture on video.

My NFC Slogan

You’re already there! No need to PAIR!

What I’m trying to say in my slogan is this: there are times when you want to be physically at a device and make it do something. To me – NFC feels a little like the crystals that power starship computers in SCI-FI shows. The hero moves them into the right spot; completing the system and saving the day, galaxy and timeline.

There are times that you don’t want to be there either… And there are lots of higher powered RF systems for that…

What Can You Make?

Who will make the first bridge for NFC to XBee that has a cradle form factor?

I’m looking forward to hearing in the comments what you are going to Make!

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus