Socket
Book a DemoInstallSign in
Socket

ble-helper

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ble-helper

Provides functionality to help connecting to Bluetooth devices

0.2.6
latest
Source
npmnpm
Version published
Weekly downloads
8
Maintainers
1
Weekly downloads
 
Created
Source

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.

FAQs

Package last updated on 26 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.