Hands-on

Bluetooth Low Energy

Workshop

PhoneGap Day - January 28, 2016

Don Coleman @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

Lightbulb Service - FF10

Advertising

Advertising

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

Connect

Read Value

Write Value

Notification

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

Bluetooth Low Energy

Cordova Plugin

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

CC2650 SensorTag

CC2650 Sensors & Services

  • IR & Ambient Temperature Sensor
  • Ambient Light Sensor
  • Humidity Sensor
  • Barometric Pressure Sensor
  • 9-axis Motion Tracking
  • Magnet Sensor
  • OTA Download

http://bit.ly/sensortag-guide

Today we'll build apps to

  • Scan for peripherals
  • Connect to peripheral
  • Respond to button input
  • Control a LED
  • Read temperature sensors

Exercise 1

Scan


    ble.scan(
        [], // any services
        5,  // 5 seconds
        onDiscoverDevice,
        failure
    );
                

  var onDiscoverDevice = function(device) {
      console.log(
          device.name,
          device.id,
          device.rssi
      );
  }
                

Exercise 2

Connect


    ble.connect(
        device.id,
        onConnection,
        failure
    );
                

  var onConnection = function(peripheral) {
      console.log(
          JSON.stringify(peripheral)
      );
  }
                

Exercise 3

Button

Button Service - FFE0

http://bit.ly/sensortag-button


    ble.startNotification(
        device.id,
        BUTTON_SERVICE,
        DATA_CHARACTERISTIC,
        onNotification,
        failure);
                

  var onNotification = function(buffer) {
      var data = new Uint8Array(buffer);
      if (data[0] === 0) {
          message = 'Button is released.';
      } else {
          message = 'Button is pressed.';
      }
  }
                

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.onNotification()

Exercise 4

LED

IO Service

f000AA64-0451-4000-b000-000000000000

http://bit.ly/sensortag-led


    ble.write(
        device.id,
        service,
        characteristic,
        data,
        success,
        failure);
                

  ble.write(
    app.peripheral.id,
    IO_SERVICE,
    CONFIGURATION_CHARACTERISTIC,
    new Uint8Array([1]).buffer,
    app.showDetailPage,
    app.onError
  );
                
Enable IO Service in app.onConnect()

  ble.write(
    app.peripheral.id,
    IO_SERVICE,
    DATA_CHARACTERISTIC,
    data.buffer,
    success,
    app.onError
  );
                
Write value to data characteristic in app.setDataValue(int)

IO Bitmask

Device Integer Bits
Red LED 1 0001
Green LED 2 0010
Buzzer 4 0100
  var data = 0;
  if (redCheckbox.checked) {
    data = data | RED_LED;
  }
  if (greenCheckbox.checked) {
    data = data | GREEN_LED;
  }
  if (buzzerCheckbox.checked) {
    data = data | BUZZER;
  }
  app.setDataValue(data);
                
Encode data with bitmask in app.optionsChanged()

Exercise 5

Thermometer

Temperature Service

f000AA00-0451-4000-b000-000000000000

http://bit.ly/sensortag-temp


http://bit.ly/bluetooth-book

Thank You

Don Coleman @doncoleman

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

chariotsolutions.com

 

 

Creative Commons License
Hands-on Bluetooth Low Energy Workshop by Don Coleman
is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at https://github.com/don/.../2016-01-28-phonegap-day.