ble-helper
TypeScript icon, indicating that this package has built-in type declarations

0.2.6 • Public • Published

ble-helper

Helps to help connecting to Bluetooth devices

Test Harness

The test harness can be seen running here where you can try connecting to your bluetooth devices.

Installation

npm install ble-helper

If you are working in typescript you will most likely also need the web-bluetooth types as well:

npm install @types/web-bluetooth -D

Construction

Direct Construction

import { BluetoothHelper, Logger } from "ble-helper";

const bluetoothHelper = new BluetoothHelper(new Logger());

using this method you can replace Logger with your own logging implementation;

Construction with getInstance():

import { getInstance } from "ble-helper";

const bluetoothHelper = getInstance();

reflect-metadata will need to be installed and imported for this to work. See needle docs for more details.

Constructor Injection

BluetoothHelper is decorated so metadata about it's constructor parameters is recorded so you can use it in IOC environments:

@Injectable()
class MyClass{
    constructor(private helper: BluetoothHelper){

    }
}

to use in angular you can hook up the needle registry with angular to allow seamless injection into angular components:

main.ts:

import { getRegisteredTypesWithFactories } from '@morgan-stanley/needle';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

platformBrowserDynamic(getRegisteredTypesWithFactories())
    .bootstrapModule(AppModule)
    .catch((err) => console.error(err));

reflect-metadata will need to be installed and imported for this to work. See needle docs for more details.

Usage

import { getInstance, GattService, GattCharacteristic } from "ble-helper";

const helper = getInstance();

async function getHeartRateUpdates() {
    // request device. This will open a dialog in the browser where the user will pick the device.
    // Specify an array of what services you want.
    const device = await firstValueFrom(helper.requestDevice([GattService['Heart Rate']]));

    if (device == null) {
        // User cancelled the dialog, no device selected.
        return;
    }

    // Connect to server
    const server = await firstValueFrom(helper.connectServer(device));

    // Request the service we are interested from server
    const service = await firstValueFrom(helper.getService(server, GattService['Heart Rate']));

    // Request characteristic we are interested in from service
    const characteristic = await firstValueFrom(
        helper.getCharacteristic(server, service, GattCharacteristic['Heart Rate Measurement']),
    );

    // Subscribe to be notified of these values changing
    helper.getNotifications(characteristic).subscribe((value) => {
        console.log(parseValue(value));
    });
}

BluetoothHelper returns observables but I've used async above to make it a bit clearer what is going on.

If you want to connect to multiple different services on your device these services must be passed as optionalServices in a RequestDeviceOptions object:

const device = await lastValueFrom(
    this.helper.requestDevice({
        filters: [{ services: [GattService['Heart Rate']] }],
        optionalServices: [
            GattService.Battery,
            GattService['Generic Access'],
            GattService['Generic Attribute'],
            GattService['Device Information'],
        ],
    }),
);

The parseValue function will need to be written for each different BLuetooth characteristic based on the documents here.

Notes

ALthough Angular is included in this project this is only for the test harness. There are no angular dependencies when consuming this project.

Readme

Keywords

none

Package Sidebar

Install

npm i ble-helper

Weekly Downloads

0

Version

0.2.6

License

ISC

Unpacked Size

152 kB

Total Files

42

Last publish

Collaborators

  • roaders