The missing Web Bluetooth module for Angular
Tags: angular, bluetooth, javascript
Published: 2019-06-01 19:43:29

For the past few months, I’ve been playing around with the new [Web Bluetooth API](https://www.chromestatus.com/feature/5264933985976320) 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](https://angular-web-bluetooth.staticweb.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](http://reactivex.io/rxjs/manual/overview.html) about [Observables](http://reactivex.io/rxjs/manual/overview.html#observable), [Observers](http://reactivex.io/rxjs/manual/overview.html#observer) and [Subjects](http://reactivex.io/rxjs/manual/overview.html#subject).

### 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](https://medium.com/@benlesh/observables-and-finnish-notation-df8356ed1c9b#.e1uakchpi).

![](https://cdn-images-1.medium.com/max/2400/1*hTkqd86iz_wxMBNT74EGVA.gif)


# Installing the module

You can get this module either using npm:

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

# Using the WebBluetoothModule

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

{% gist https://gist.github.com/manekinekko/2311fcf947ce6cd2637378b09e245eea %}

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*:

{% gist https://gist.github.com/manekinekko/86eea083e20c7ee1bc50c604dd655fb6 %}

The **WebBluetoothModule.forRoot()** also provides a **BrowserWebBluetooth** implementation which uses [navigator.bluetooth](https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice) 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:

{% gist https://gist.github.com/manekinekko/0b32eab29b535d114be45af7128ec227 %}

# 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](https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.battery_level.xml), 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);
 });
```

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

![](https://cdn-images-1.medium.com/max/2400/1*C5T5hLCleCfLlzB9VHFh4g.gif)


# Need a starter?

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

{% github https://github.com/manekinekko/angular-web-bluetooth %}


# Have feedback?
All contributions are welcome. Please open an issue on [GitHub](https://github.com/manekinekko/angular-web-bluetooth) 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](https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web).

<hr/>

Follow me at [@manekinekko](https://twitter.com/manekinekko) for more updates about Angular, IoT and Web technologies.