The missing Web Bluetooth module for Angular

Wassim Chegham - Jun 1 '19 - - Dev Community

For the past few months, I’ve been playing around with the new Web Bluetooth API which shipped in Chrome 56 in February 2017. And let me tell you, this relatively new feature just unlocked lots of new possibilities for the Web.

As an advocate for Web technologies, I was so excited and couldn’t wait to build an application showing how easy it is to combine Angular and the Web Bluetooth API (even more, with any of the upcoming Web APIs, more on that soon, stay tuned).

Let’s meet The Missing Web Bluetooth Module for Angular Application

I started then working with my buddy François Beaufort (kudos to him!) to build a demo app, a kind of proof of concept that illustrates how to integrate Web Bluetooth with Angular.

After implementing a couple of use cases, I came up with an Angular module that abstracts away all the boilerplate needed to configure the Web Bluetooth API.

A Few Disclaimers

Web Bluetooth APIs

I am going to assume that you’re already familiar with the Web Bluetooth APIs: GATT server, Services, Characteristics…etc. Please make yourself comfortable with this topic before reading the next sections. Here are a few resources:

  1. https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web

  2. https://medium.com/@urish/start-building-with-web-bluetooth-and-progressive-web-apps-6534835959a6

Observables

I am also assuming that you have some basic knowledge about Observables, Observers and Subjects.

Finnish Notation

You will notice that some methods end with a $ symbol. This is some sort of convention in the Observables world that we’ve been using for a while. We may drop this $ symbol in the future because of this blog post.

Installing the module

You can get this module either using npm:

$ npm install -S @manekinekko/angular-web-bluetooth @types/web-bluetooth
Enter fullscreen mode Exit fullscreen mode

Using the WebBluetoothModule

The module is simple to use. First, import the WebBluetoothModule module form @manekinekko/angular-web-bluetooth:

Calling the WebBluetoothModule.forRoot() method will provide the BluetoothCore service which you will need to use inside your own services/components, like so in battery-level.component.ts:

The WebBluetoothModule.forRoot() also provides a BrowserWebBluetooth implementation which uses navigator.bluetooth under the hood. A ServerWebBluetooth implementation for Angular Universal will come later. Of course, using Angular’s DI, you are free to provide your custom implementation if you like.

The BatteryLevelService (battery-level.service.ts) service is where you will (should) implement the logic of your device/sensor. In the following example, we are implementing a battery level service that reads the level of the connected device’s battery:

What's happening?

Okay! Let’s explain what’s happening inside the getBatteryLevel() method...

Basically, in order to read a value from a device, you need to go through the same workflow (for the common use cases):

  1. Call the discover$() method to run the discovery process:

  2. This will give you back the GATT server

  3. Then, you will get the primary service of that GATT server

  4. Next, get a specific characteristic

  5. Lastly, read the extracted value from that characteristic (as DataView)

  6. The last step will give you the values as DataView types. You will have to read the right values that are specific to your device/sensor. For instance, for simple values such as battery level, calling a value.getUint8(0) is enough:

.map( (value: DataView) => value.getUint8(0) );

But sometimes, things can be more complicated. Some manufacturers usually provide their own Bluetooth GATT characteristics implementation and not follow the standards. This is the case if you need to read values from a Luxometer, commonly called a light sensor (which measures in LUX). Here is a sample code related to the Texas Instrument SensorTag CC2650 sensor:

.map( (data: DataView) => {
    let value = data.getUint16(0, true /* little endian */);
    let mantissa = value & 0x0FFF;
    let exponent = value >> 12;
    let magnitude = Math.pow(2, exponent);
    let output = (mantissa * magnitude);
    let lux = output / 100.0;
    return +lux.toFixed(2);
 });
Enter fullscreen mode Exit fullscreen mode

This is usually can be found in the device/sensor documentation, or source code if you’re lucky!

Need a starter?

Here is a basic repo that will help you get started...

GitHub logo manekinekko / angular-web-bluetooth

The missing Web Bluetooth module for Angular

The missing Web Bluetooth module for Angular

Install

npm install -S @manekinekko/angular-web-bluetooth @types/web-bluetooth

Note: Make also sure the @types/web-bluetooth is installed correctly in your node_modules.

Getting started

1) import the WebBluetoothModule module

import { NgModule } from '@angular/core';
import { WebBluetoothModule } from '@manekinekko/angular-web-bluetooth';

@NgModule({
  imports: [
    //...,
    WebBluetoothModule.forRoot({
      enableTracing: true // or false, this will enable logs in the browser's console
    })
  ]
  //...
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

2.a) use it in your service/component (the easiest way)

Here is an annotated example using the BluetoothCore service:

import { Injectable } from '@angular/core';
import { BluetoothCore } from '@manekinekko/angular-web-bluetooth';
import { map } from 'rxjs/operators';
@Injectable({
  providedIn: 'root'
})
export class BatteryLevelService {

  constructor(public readonly ble: BluetoothCore) {}

  getDevice()
Enter fullscreen mode Exit fullscreen mode

Have feedback?

All contributions are welcome. Please open an issue on GitHub and give us your feedback...

Going further

If you need to go even further and implement even more cool applications using more devices and sensors, you can have a look at the Web Bluetooth API.


Follow me at @manekinekko for more updates about Angular, IoT and Web technologies.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .