micro:bit Web Bluetooth

Web Bluetooth library for micro:bit implementing the micro:bit Bluetooth Profile.

Prerequisites

Node.js > v10.16.0, which includes npm

Installation

$ npm install microbit-web-bluetooth

Device Setup

Your micro:bit needs to be running a program which exposes the bluetooth service you wish to interact with.

Pre-compiled programs exposing bluetooth services are available for testing purposes:

https://github.com/thegecko/microbit-web-bluetooth/tree/master/hex

Service no-magnet-no-io no-magnet-no-uart no-event-no-uart-no-dfu no-event-no-io-no-temp-no-acc
Device Info
Button
LED
Temperature -
Accelerometer -
Event - -
DFU -
UART - -
Magnetometer - -
IO Pin - -

The source for a sample program to do this is available here.

Note: The configuration must mark bluetooth as being open in order for Web Bluetooth to work. e.g.:

{
    "microbit-dal": {
        "bluetooth": {
            "enabled": 1,
            "pairing_mode": 1,
            "private_addressing": 0,
            "open": 1,
            "whitelist": 1,
            "advertising_timeout": 0,
            "tx_power": 0,
            "dfu_service": 0,
            "event_service": 1,
            "device_info_service": 1,
            "security_level": "SECURITY_MODE_ENCRYPTION_NO_MITM"
        },
        "gatt_table_size": "0x700"
    }
}

Usage

Include the package in your web page, exposing a global named microbit:

<script type="text/javascript" src="microbit.umd.js"></script>

Use window.navigator.bluetooth.requestDevice() to obtain a bluetooth device or use the requestMicrobit() helper to discover a micro:bit:

const device = await microbit.requestMicrobit(window.navigator.bluetooth);

Use getServices() to return an array of available services:

const services = await microbit.getServices(device);

Services returned could be any of the following:

Refer to the web example (running) for a full example for each supported service.

Index

Type aliases

AccelerometerPeriod

AccelerometerPeriod: 1 | 2 | 5 | 10 | 20 | 80 | 160 | 640

LedMatrix

LedMatrix: [[boolean, boolean, boolean, boolean, boolean], [boolean, boolean, boolean, boolean, boolean], [boolean, boolean, boolean, boolean, boolean], [boolean, boolean, boolean, boolean, boolean], [boolean, boolean, boolean, boolean, boolean]]

MagnetometerPeriod

MagnetometerPeriod: 1 | 2 | 5 | 10 | 20 | 80 | 160 | 640

Variables

Const littleEndian

littleEndian: true = true

Functions

Const getServices

  • getServices(device: BluetoothDevice): Promise<Services>
  • Parameters

    • device: BluetoothDevice

    Returns Promise<Services>

Const requestMicrobit

  • requestMicrobit(bluetooth: Bluetooth): Promise<BluetoothDevice | undefined>
  • Parameters

    • bluetooth: Bluetooth

    Returns Promise<BluetoothDevice | undefined>