Evothings Studio 2.1 alpha with support for Web Bluetooth and ECMAScript 6

Mikael KindborgBlogs, Tutorials

bluetooth
Tweet about this on TwitterShare on FacebookShare on LinkedInShare on Google+

Writing mobile apps that communicate with IoT devices using Bluetooth Low Energy (BLE) can be quite an undertaking. Web Bluetooth is a new API that makes it easier to develop BLE enabled applications in JavaScript. In this post we present an overview of Web Bluetooth and invite you to download a brand new release of Evothings Studio for early testers.

Web Bluetooth

Web Bluetooth is a standards initiative by the W3C Web Bluetooth Community Group.
Originally designed to enable apps running in web browsers to communicate with BLE devices, the Web Bluetooth API is now also available for mobile apps built with Apache Cordova.

The Web Bluetooth API specification makes use of ECMAScript 6, a new version of JavaScript that features a new function closure syntax and a bunch of other improvements. You can write Web Bluetooth applications using ECMAScript 5, but the code becomes more readable with ECMAScript 6.

During the last few weeks, we have been busy implementing support for Web Bluetooth and ECMAScript 6 in Evothings Studio 2.1.0, now available in a release for early testers.

Bleat – Bluetooth Low Energy Abstraction Tool

To bring Web Bluetooth to mobile apps, we have used Bleat, which offers libraries and a pluggable architecture for BLE APIs in JavaScript. Bleat was created by Rob Moran (@thegecko), who works in the mbed team at ARM and is part of the W3C Web Bluetooth Community Group. Thanks to Bleat, we have been able to include early support for Web Bluetooth in Evothings Studio.

Bleat is included with the new example apps for ARM mbed (classic) and TI SensorTag that ships with Evothings Studio 2.1.0-alpha.

ECMAScript 6

ECMAScript 6 or ECMAScript 2015, or simply ES6, is a new version of the JavaScript language. ES6 has several new features (such as arrow functions) that fit nicely with the Web Bluetooth API.

ES6 is not yet widely supported on mobile devices. We have included the Babel compiler in Evothings Studio 2.1.0-alpha, which enables writing mobile apps in ES6. When you click the Run button in Evothings Workbench, ES6 source files are transparently translated to ES5, and your app launches as usual on connected mobile phones.

Run the Hello ECMAScript 6 that comes with the Evothings Studio 2.1.0-alpha download to see ES6 in action.

Visit the Evothings ECMAScript 6 documentation page to learn more.

A taste of the Web Bluetooth API

Let’s illustrate the Web Bluetooth API with a classic example, blinking a LED. Here we connect to a named device and start blinking a LED on the device:

// Find device named 'MyDevice' (you can also find
// device by service UUIDs).
bleat.requestDevice({
    filters:[{ name: 'MyDevice' }]
})
.then(device => {
    // Connect to device.
    return device.gatt.connect();
})
.then(server => {
    // Get service.
    return server.getPrimaryService(LED_SERVICE_UUID);
})
.then(service => {
    // Get LED write characteristic.
    return service.getCharacteristic(LED_WRITE_UUID);
})
.then(characteristic => {
    // Start blinking the LED each second.
    var ledState = 0;
    setInterval(() => {
        ledState = 0 === ledState ? 1 : 0;
        characteristic.writeValue(new Uint8Array([ledState]));
    }, 1000);
})
.catch(error => {
    console.log('Error: ' + error);
});

Function requestDevice is the entry point in the Web Bluetooth API. It scans for devices that matches the filters you provide, and returns a matching device. It is possible to filter devices by name and by service UUID.

Each function in the Web Bluetooth API returns a Promise, an object that will invoke a callback function when the promise is fulfilled (for example when a matching device is found, or a BLE service or characteristic is available). The function .then is used to specify the callback for a promise. Together with arrow functions in ES6, this makes for a clean syntax for code that is based on async functions and callbacks.

New extensions to the Web Bluetooth API

In the original Web Bluetooth specification, requestDevice brings up a UI controlled by the browser, which displays a list of matching devices the user can select from. When selecting a device, the promise returned by requestDevice is resolved.

With mobile apps developed using Apache Cordova, and in applications developed using node.js, the requirements are a bit different. It can be desirable to have control of the device selection UI, and to be able to scan for devices (such as Eddystone devices) and connect to them programmatically.

Rob Moran has presented extensions to Web Bluetooth, to allow more flexibility for apps that are not running in a web browser context. These extensions are implemented in Bleat, included in Evothings Studio, and allows mobile apps to use requestDevice for tasks such as scanning for beacons and connect devices in a flexible way.

Web Bluetooth example apps

Evothings Studio 2.1.0-alpha comes with two new example apps that uses Web Bluetooth and the Bleat library.

mbed GATT Web Bluetooth

This is an app for ARM mbed devices, that turns on and off a LED on the device.

The application code shows how to connect to a device and read and write characteristics using Web Bluetooth API.

mbed-nordic-nrf51-dk

We have been using the Nordic Semiconductor nRF51 DK when developing and testing this app.

TI SensorTag CC2541 Accelerometer Web Bluetooth

This app listens for the TI SensorTag CC2541 accelerometer sensor, and displays sensor data continuously.

The application code shows how to enable notifications using Web Bluetooth API.

mbed-nordic-nrf51-dk

Get started with Web Bluetooth and ES6

Download Evothings Studio 2.1.0-alpha and get started with mobile app development using Web Bluetooth.

You will find the download under the alpha section on the Evothings download page.