Bluetooth Low Energy with Apache Cordova

PhoneGap SF Meetup - May 24, 2016

Don Coleman - Chariot Solutions

@doncoleman

Lightbulb Service

  • Light Switch
  • Dimmer Setting

Lightbulb Service - FF10

16 bit UUIDs

0000FF10-0000-1000-8000-00805F9B34FB

0000FF11-0000-1000-8000-00805F9B34FB

0000FF12-0000-1000-8000-00805F9B34FB

0000FF16-0000-1000-8000-00805F9B34FB

Lightbulb Service - FF10

Properties

Attributes

Permissions

Descriptors

Advertising

Advertising

  • MAC Address: AA:BB:CC:DD:EE:FF
  • Services: [ 0x180A, 0xFF10, 0XFF20, …]
  • Manufacturer Data: <00007465 6d70>
  • Connectable: True
  • TX Power Level: 4
  • RSSI: -80
  • Name: Smart Lightbulb

Advertising

  • 5425B44A-024A-4D39-88D2-D9F016199B29
  • Services: [ 0x180A, 0xFF10, 0XFF20, …]
  • Manufacturer Data: <00007465 6d70>
  • Connectable: True
  • TX Power Level: 4
  • RSSI: -80
  • Name: Smart Lightbulb

Connect

Explore

  • Get a list of services
  •   For each service, get a list of characteristics
  •     For each characteristic, get properties
  •       Save handle to read or write later
  •       Subscribe to be notified on changes

Read Value

Write Value

Write Command

Heart Rate Service - 180d

org.bluetooth.service.heart_rate.xml
org.bluetooth.characteristic.heart_rate_measurement.xml

Notification

Indication

Peripheral

Provides services

Central

Uses services on a peripheral

Lightbulb Service - FF10

Apache Cordova

http://docs.cordova.io

Bluetooth Low Energy

Cordova Plugin

https://github.com/don/cordova-plugin-ble-central

Installing


 cordova plugin add cordova-plugin-ble-central
                

Scanning


    ble.scan(services,
             seconds,
             success,
             failure);
                

Scanning


    ble.scan(["FF10"],
             5,
             app.onDeviceDiscovered,
             failure);
                

Device Discovered


    {
        "name": "Robosmart",
        "id": "78:C5:E5:9A:15:AA",
        "advertising": {},
        "rssi": -65
    }
                

Connecting


    ble.connect(device_id,
        connectSuccess,
        connectFailure);
                

Connecting


    ble.connect("78:C5:E5:9A:15:AA",
        onConnect,
        onDisconnect);
                

Connection Success


    var onConnect = function(peripheral) {
        console.log(peripheral);
        // read current state of light
        // update UI
    };
                

Read Characteristic


    ble.read(device_id,
        service_uuid,
        characteristic_uuid,
        success,
        failure);
                

Read Characteristic


    ble.read("78:C5:E5:9A:15:AA",
        "FF10",
        "FF11",
        success,
        failure);
                

Read Callback


  var success = function(buffer) {
    var data = new Uint8Array(buffer);
    if (data[0] === 1) {
        onButton.className = 'selected';
    } else {
        offButton.className = 'selected';
    }
  };
                

Write Characteristic


  ble.write(device_id,
      service_uuid,
      characteristic_uuid,
      value,
      success,
      failure);
                

Write Characteristic


  var data = new Uint8Array(1);
  data[0] = 1;
  ble.write("78:C5:E5:9A:15:AA",
            "FF10",
            "FF11",
            data.buffer,
            success);
                

Write Characteristic


ble.writeWithoutResponse("78:C5:E5:9A:15:AA",
                         "FF10",
                         "FF11",
                          data.buffer,
                          success,
                          failure);
                

Code

https://github.com/don/robosmart

Heart Rate Service - 180d

Notification


    ble.startNotification(device_id,
                service_uuid,
                characteristic_uuid,
                success,
                failure);
                

Notification


    ble.startNotification(deviceId,
        "180d",
        "2a37",
        onData,
        failure);
                

Notification Callback


  onData = function(buffer) {
      var data = new Unit8Array(buffer);
      var flags = data[0];
      // read flags, etc
      var rate = data[1];
      console.log("Heart Rate is " + rate);
  }
                

Code

https://github.com/don/.../examples/heartrate

Button Service - FFE0


  ble.startNotification(deviceId,
    "FFE0",
    "FFE1",
    app.onButtonData,
    app.onError);
                

Button Bitmask

Button Integer Bits
Left Button 1 0001
Right Button 2 0010
Reed Switch 4 0100
  if (state & LEFT_BUTTON) {
    message += 'Left button is pressed.';
  }
  if (state & RIGHT_BUTTON) {
    message += 'Right button is pressed.';
  }
  if (state & REED_SWITCH) {
    message += 'Reed switch is activated.';
  }
                
Decode button from bitmask in app.onButtonData()

Code

https://github.com/don/.../examples/sensortag

BLE Peripheral

WARNING: Experimental

Installing


 cordova plugin add cordova-plugin-ble-peripheral
                

Demo

https://github.com/don/cordova-plugin-ble-peripheral

makebluetooth.com

Thank You

Don Coleman

@doncoleman

don@chariotsolutions.com

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

 

 

Creative Commons License
Bluetooth Low Energy with Apache 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/.../2015-06-24-phonegap-meetup.