Node Web Bluetooth

Node.js implementation of the Web Bluetooth Specification

Prerequisites

Node.js > v8.14.0, which includes npm.

Installation

$ npm install webbluetooth

Usage

The module exports a default navigator.bluetooth instance, the Bluetooth class to allow you to instantiate your own bluetooth instances and some helper methods:

Using the default bluetooth instance

To use existing Web Bluetooth scripts, you can simply use the default bluetooth instance in place of the navigator.bluetooth object:

const bluetooth = require("webbluetooth").bluetooth;

const device = await bluetooth.requestDevice({
    filters:[{ services:[ "heart_rate" ] }]
});

const server = await device.gatt.connect();
...

The first device matching the filters will be returned.

Creating your own bluetooth instances

You may want to create your own instance of the Bluetooth class. For example, to inject a device chooser function or control the referring device:

const Bluetooth = require("webbluetooth").Bluetooth;

const deviceFound = (device, selectFn) => {
    // If device can be automatically selected, do so by returning true
    if (device.name === "myName") return true;

    // Otherwise store the selectFn somewhere and execute it later to select this device
};

const bluetooth = new Bluetooth({ deviceFound });

const device = await bluetooth.requestDevice({
    filters:[{ services:[ "heart_rate" ] }]
});

const server = await device.gatt.connect();
...

Specification

The Web Bluetooth specification can be found here:

https://webbluetoothcg.github.io/web-bluetooth/

Index

Variables

Const bluetooth

bluetooth: Bluetooth = new Bluetooth()

Default bluetooth instance synonymous with navigator.bluetooth

Functions

getCanonicalUUID

  • getCanonicalUUID(uuid: string | number): string
  • Gets a canonical UUID from a partial UUID in string or hex format

    Parameters

    • uuid: string | number

      The partial UUID

    Returns string

    canonical UUID

getCharacteristicUUID

  • getCharacteristicUUID(characteristic: string | number): string
  • Gets a canonical characteristic UUID from a known characteristic name or partial UUID in string or hex format

    Parameters

    • characteristic: string | number

      The known characteristic name

    Returns string

    canonical UUID

getDescriptorUUID

  • getDescriptorUUID(descriptor: string | number): string
  • Gets a canonical descriptor UUID from a known descriptor name or partial UUID in string or hex format

    Parameters

    • descriptor: string | number

      The known descriptor name

    Returns string

    canonical UUID

getServiceUUID

  • getServiceUUID(service: string | number): string
  • Gets a canonical service UUID from a known service name or partial UUID in string or hex format

    Parameters

    • service: string | number

      The known service name

    Returns string

    canonical UUID