npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ble-helper

v0.2.6

Published

Provides functionality to help connecting to Bluetooth devices

Downloads

1

Readme

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.