Bluetooth LE

ITP | Week 3 | February 10, 2017

Don Coleman

Callbacks


  try {
    temperature = api.getTemperature();
    console.log(temperature);
  } catch(e) {
    console.log("Error " + e);
  }
                    
synchronous API call
  var success = function(temperature) {
    console.log(temperature);
  };
  var failure = function(err) {
    console.log("Error " + err);
  };
  api.getTemperature(success, failure);
                    
Cordova APIs (usually) end with success and failure callbacks

Bluetooth Low Energy

Cordova Plugin

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

Today we'll build apps to

  • Scan for peripherals
  • Connect to peripheral
  • Control a LED
  • Respond to button input
  • 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

LED

Lightbulb Service - FF10


    ble.scan(
        [LED_SERVICE], // only led
        5,  // 5 seconds
        onDiscoverDevice,
        failure
    );
                

  setSwitchValue: function(value) {
      var data = new Uint8Array(1);
      data[0] = value;
      // ...
  },
                
Create typed array in setSwitchValue

  ble.write(
      app.peripheral.id,
      LED_SERVICE,
      SWITCH_CHARACTERISTIC,
      data.buffer,
      success,
      app.onError
  );
                
Write value to switch characteristic in setSwitchValue
  var data = new Uint8Array(1);
  data[0] = brightness.value;
  ble.write(
      app.peripheral.id,
      LED_SERVICE,
      BRIGHTNESS_CHARACTERISTIC,
      data.buffer,
      success,
      app.onError
  );
                
Write value to dimmer characteristic in setBrightness

Exercise 4

Button

Button Service - FFE0


    ble.startNotification(
        device.id,
        BUTTON_SERVICE,
        STATUS_CHARACTERISTIC,
        app.onNotification,
        failure);
                
subscribe to be notified when the button state changes

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

Exercise 5

Thermometer

Thermometer Service - 0xBBB0


    ble.startNotification(
        peripheral.id,
        THERMOMETER_SERVICE,
        TEMPERATURE_CHARACTERISTIC,
        app.onTemperatureChange,
        failure
    );
                
subscribe to be notified when the temperature changes

    ble.read(
        peripheral.id,
        THERMOMETER_SERVICE,
        TEMPERATURE_CHARACTERISTIC,
        app.onTemperatureChange,
        failure
    );
                
read the initial value

  onTemperatureChange: function(buffer) {
      var data = new Float32Array(buffer);
      var celsius = data[0];
      var fahrenheit = (celsius * 1.8 + 32.0);
      var message = "Temperature is " +
                       fahrenheit + " °F";
      statusDiv.innerHTML = message;
  },
                

Debugging

JSHint


$ jshint www/js/index.js
www/js/index.js: line 26, col 33, Expected
 an identifier and instead saw '5'.

1 error
                
Read the console output

 window.onerror = function (e, fileName, lineNumber) {
   console.log(fileName,
         'Line:', lineNumber, '\n', 
         'Error:', e.message);
   alert(fileName +
         'Line:' + lineNumber, '\n',
         'Error:' + e.message);
 };
                
chrome://inspect requires debug version of PhoneGap Developer App
Safari debugging requires debug version of PhoneGap Developer App
Creating Apps without PhoneGap Developer App

 $ cd phonegap/led_v2
 $ phonegap platform add ios
 $ phonegap plugin add cordova-plugin-ble-central
 $ phonegap plugin add cordova-plugin-dialogs
 $ phonegap run ios --device
                

Don Coleman

dc159@nyu.edu

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

 

 

Creative Commons License
NYU ITP Bluetooth Spring 2017 Week 3 by Don Coleman is licensed under a Creative Commons Attribution 4.0 International License.