Creating Bluetooth Low Energy Apps

Philadelphia ETE - April 7, 2015

Don Coleman - Chariot Solutions

@doncoleman

Lightbulb Service

  • Light Switch
  • Dimmer Setting
  • Power Consumption

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

Scan and Discover Peripherals

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

  • Discover advertising peripherals
  • Connect using MAC Address or UUID
  • Explore the services and characteristics
  • Read and write characteristics
  • Subscribe to be notified of changes

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 com.megster.cordova.ble
                 

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.notify(deviceId, 
    "FFE0", 
    "FFE1", 
    app.onButtonData, 
    app.onError);
                

Code

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

iBeacon

  • UUID: ad5d9ece-9331-48c2-b597-2845aac4a8f0
  • Major: 0x01
  • Minor: 0x0A

Core Location

(not Core Bluetooth)

Region

Ranging

Code

https://github.com/don/ibeacon-demo

URI Beacon

http://www.phillyete.com/r/a

Thank You

Don Coleman

@doncoleman

don@chariotsolutions.com

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

 

 

Creative Commons License
Bluetooth Low Energy 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-04-07-phillyete-bluetooth.