Connecting Arduino & Phones with Bluetooth & Cordova

ApacheCon North America - April 7, 2014

Presented by Don Coleman / @doncoleman

Phone <===> Device

Bluetooth

Alternatives

  • WiFi
  • XBee ZigBee
  • ZWave
  • 433 MHz
  • Other 2.4 GHz

Arduino


  #include <SoftwareSerial.h>

  #define RxD 6
  #define TxD 7

  SoftwareSerial bluetooth(RxD,TxD);
  int counter = 0;
					

  void setup() {
    Serial.begin(9600);
    bluetooth.begin(9600);
    Serial.println("Bluetooth Counter\n");
  }
	

  void loop() {
    Serial.println(counter);
    bluetooth.println(counter);
    counter++;
    delay(1000);
  }
	

Bluetooth Serial

Cordova Plugin

https://github.com/don/BluetoothSerial

Installing


  $ cordova plugin install \
        com.megster.cordova.bluetoothserial

                    

Connecting

  
  var macAddress = "00:00:AA:BB:CC:DD";

  bluetoothSerial.connect(
        macAddress, 
        connected, 
        disconnected);

                    

Sending Data


    bluetoothSerial.write("Hello Arduino");
  				    

Receiving Data

 
  var onMessage = function(data) {
      console.log(data);
  };
                    
  bluetoothSerial.subscribe("\n", 
            onMessage, 
            onFailure);
                    

Demo

Phone receives data from Arduino

Listing Devices


  bluetoothSerial.list(success, failure);
	

List shows paired devices

[{
    "class": 276,
    "id": "10:BF:48:CB:00:00",
    "address": "10:BF:48:CB:00:00",
    "name": "Nexus 7"
}, {
    "class": 7936,
    "id": "00:06:66:4D:00:00",
    "address": "00:06:66:4D:00:00",
    "name": "RN42"
}]

What about iOS?

Bluetooth Low Energy

aka Bluetooth Smart

BLE doesn't have SPP

but available hardware has "serial like" services

UART Service

  • RX - write without response
  • TX - read, notify

Discovering devices


  bluetoothSerial.list(success, failure);
	

No pairing, finds devices

List discovered devices

[{
  "id": "CC410A23-2865-F03E-FC6A-4C17E858E11E",
  "uuid": "CC410A23-2865-F03E-FC6A-4C17E858E11E",
  "name": "Biscuit",
  "rssi": -68
},{
  "id": "ED7127D4-593B-490E-9DA7-959FE78EC603",
  "uuid": "ED7127D4-593B-490E-9DA7-959FE78EC603",
  "name": "UART",
  "rssi": -47
}]

Once you're connected there's no difference

(except for BLE only handles small chunks of data)

BLE hardware

c red, green, blue

c0,0,255\n

Demo

Phone controls LED strip on Arduino
Control NeoPixel LED on Arduino from Android

  onColorChange: function (evt) {
    var c = app.getColor();
    rgbText.innerText = c; // 0,0,255
    previewColor.style.backgroundColor = 
        "rgb(" + c + ")";
    app.sendToArduino(c);
  },
					

  sendToArduino: function(c) {
    bluetoothSerial.write("c" + c + "\n");
  },
	
  void loop() {

    if (bluetooth.find("c")) {
      red = bluetooth.parseInt();
      green = bluetooth.parseInt();
      blue = bluetooth.parseInt();
      showColor(red, green, blue);
    }
  }

Bluetooth Low Energy Lock

Stride Tool - iManifold

BluetoothSerial Plugin

  • Bluetooth Classic for Android
  • Bluetooth LE for iOS
  • List or discover devices
  • Connect to a peripheral
  • Write to send data
  • Subscribe to read data
http://rfduino.com

RFduino Cordova Plugin

https://github.com/don/corodva-plugin-rfduino

rfduino.discover(seconds, success, failure);


{
  "name": "RFduino",
  "uuid": "BD922605-1B07-4D55-8D09-B66653E51BBA",
  "advertising": "echo",
  "rssi": -79
}
    

  rfduino.connect(uuid, success, failure);
  

  rfduino.write("hello", success, failure);
  

  rfduino.onData(success, failure);
  

  var onData = function(arrayBuffer) {
    var a = new Float32Array(arrayBuffer);
    celsius = a[0];
    fahrenheit = celsius * 1.8 + 32;
  }
    

RFduino Plugin

  • Bluetooth LE for iOS and Android
  • Discover devices
  • Connect to a peripheral
  • Write to send data
  • Subscribe to read data

Future

BluetoothLE

https://github.com/randdusing/BluetoothLE

Cordova Hackathon

Tomorrow, Tuesday April 8, 2014

10:30am - 5:30pm

McCourt room at ApacheCon


Thank You

Don Coleman

@doncoleman

don@chariotsolutions.com

http://don.github.io/slides/

 

 

Creative Commons License
Connecting Arduino & Phones with Bluetooth & Cordova by Don Coleman
is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at https://github.com/don/.../2014-04-07-apachecon-bluetooth.