• Stars
    star
    145
  • Rank 254,144 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node.js implementation of the Web Bluetooth Specification

Node Web Bluetooth

Node.js implementation of the Web Bluetooth Specification

Build Status npm Licence MIT

Prerequisites

Node.js > v10.20.0, which includes npm.

Installation

$ npm install webbluetooth

Getting Started

See the examples or view the API documentation at:

https://thegecko.github.io/webbluetooth/

Usage

The module exports a default navigator.bluetooth instance, the Bluetooth class to allow you to instantiate your own bluetooth instances and the Bluetooth 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/

Implementation Status

bluetooth

  • getAvailability() - unsupported in adapter
  • referringDevice
  • requestDevice()
  • getDevices()
  • RequestDeviceOptions.filter.name
  • RequestDeviceOptions.filter.namePrefix
  • RequestDeviceOptions.filter.services
  • RequestDeviceOptions.filter.manufacturerData
  • RequestDeviceOptions.filter.serviceData
  • RequestDeviceOptions.acceptAllDevices
  • RequestDeviceOptions.optionalServices
  • RequestDeviceOptions.exclusionFilters
  • RequestDeviceOptions.optionalManufacturerData - used in advertisements, unsupported in adapter

BluetoothDevice

  • id
  • name
  • gatt
  • forget()
  • watchAdvertisements() - unsupported in adapter
  • watchingAdvertisements - unsupported in adapter

BluetoothRemoteGATTServer

  • device
  • connected
  • connect()
  • disconnect()
  • getPrimaryService()
  • getPrimaryServices()

BluetoothRemoteGATTService

  • uuid
  • device
  • isPrimary
  • getCharacteristic()
  • getCharacteristics()
  • getIncludedService() - unsupported in adapter
  • getIncludedServices() - unsupported in adapter

BluetoothRemoteGATTCharacteristic

  • uuid
  • service
  • value
  • properties.broadcast - unsupported in adapter
  • properties.read
  • properties.writeWithoutResponse
  • properties.write
  • properties.notify
  • properties.indicate
  • properties.authenticatedSignedWrites - unsupported in adapter
  • properties.reliableWrite - unsupported in adapter
  • properties.writableAuxiliaries - unsupported in adapter
  • getDescriptor()
  • getDescriptors()
  • readValue()
  • writeValue()
  • writeValueWithResponse()
  • writeValueWithoutResponse()
  • startNotifications()
  • stopNotifications()

BluetoothRemoteGATTDescriptor

  • uuid
  • characteristic
  • value
  • readValue()
  • writeValue()

BluetoothUUID

  • getService()
  • getCharacteristic()
  • getDescriptor()
  • canonicalUUID()

Events

Bluetooth

  • availabilitychanged - unsupported in adapter

Bluetooth Device

  • gattserverdisconnected
  • advertisementreceived - unsupported in adapter

Bluetooth Service

  • serviceadded
  • servicechanged - unsupported in adapter
  • serviceremoved - unsupported in adapter

Bluetooth Characteristic

  • characteristicvaluechanged

Other

  • Device selector hook
  • Examples
  • API Documentation

Development

Cloning

This repository uses a submodule to reference the SimpleBLE library. Clone it as follows:

git clone https://github.com/thegecko/webbluetooth
cd webbluetooth
git submodule update --init

Building

To build the SimpleBLE module, bindings and TypeScriptsource, run:

yarn build:all

Testing

The tests are set up to use a BBC micro:bit in range with the following services available:

  • Device Info Service (0000180a-0000-1000-8000-00805f9b34fb)
  • LED Service (e95dd91d-251d-470a-a062-fa1922dfa9a8)
  • Button Service (e95d9882-251d-470a-a062-fa1922dfa9a8)

Firmware for this hardware including the services can be found here: https://microbit.org/get-started/user-guide/firmware/

To run the tests:

yarn test

More Repositories

1

webusb

Node.js implementation of the WebUSB Specification
TypeScript
183
star
2

web-bluetooth-dfu

Device firmware update with Web Bluetooth
TypeScript
95
star
3

microbit-web-bluetooth

Web Bluetooth library for micro:bit
TypeScript
52
star
4

bleat

Abstraction library for hiding differences in JavaScript BLE APIs
JavaScript
48
star
5

webusb-web-extension

VS Code Web Extension demonstrating WebUSB access
TypeScript
16
star
6

microbit-web-components

Web Components library for micro:bit
TypeScript
16
star
7

react-native-ble

React Native module for Bluetooth Low Energy
Objective-C
12
star
8

hyperion

grpc messages over IPC or WebSockets using TypeScript
TypeScript
11
star
9

gulp-protobuf

A thin gulp wrapper around protobufjs pbjs and pbts tools
TypeScript
4
star
10

usb-mountpoint

TypeScript
4
star
11

vscode-theia-extension

TypeScript
4
star
12

mbed-js

C
2
star
13

wasm-flash

https://thegecko.github.io/wasm-flash/
HTML
2
star
14

showdown.js

A simple slideshow for markdown files using vanilla JS
JavaScript
2
star
15

protobuf-templates

TypeScript
2
star
16

bunnybot

MSN Messenger bot for controlling a nabaztag bunny
C#
1
star
17

webusb-agent

TypeScript
1
star
18

typescript-talk

https://thegecko.github.io/typescript-talk/
CSS
1
star
19

web-bluetooth-toolbox

A toolbox for working with BLE devices over Web Bluetooth
JavaScript
1
star
20

stlink-info

JavaScript
1
star
21

svd-viewer

Standalone SVD Viewer extension extracted from https://github.com/Marus/cortex-debug
TypeScript
1
star
22

llvm-box

C++
1
star
23

autosuggest.js

Standalone JavaScript library for suggesting and completing text in an input field following a template
JavaScript
1
star
24

vscode-findfiles

JavaScript
1
star
25

busybox-wasm

Shell
1
star