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

@blu.js/blu

v3.0.3

Published

Web Bluetooth — streamlined.

Downloads

117

Readme

Blu is a framework that streamlines the integration of Web Bluetooth into your projects. Designed with ease of use in mind, Blu offers a robust and intuitive interface for interacting with Bluetooth Low Energy devices from the web, as well as native platforms that don't support Web Bluetooth (yet).

Table of contents

Installation

npm install @blu.js/blu

Key advantages

Intuitive interface

Blu takes the complexity out of working with Web Bluetooth by providing extendable base classes for each component of the Bluetooth protocol with intuitive interfaces, streamlining the development process.

😕 Web Bluetooth API

await device.gatt.connect()

const service = await device.gatt.getPrimaryService("0x180F")
const characteristic = await service.getCharacteristic("0x2A19")
const value = await characteristic.readValue()

return value

✨ Blu

await device.connect()

const value = await device.service.characteristic.readValue()

return value

Intuitive communication model

Blu streamlines interactions with Bluetooth characteristics by implementing an intuitive communication model based on requests and responses. Fetching data from a Bluetooth device is made simple: Just request it and await a response. No need for dealing with adding and removing listeners, parsing data and – if you're using TypeScript – casting values for type safety.

😕 Web Bluetooth API

return new Promise(resolve => {
	const onCharacteristicValueChanged = () => {
		/**
		 * Ensure that the characteristic's value actually contains the data
		 * I wanted to get and is not the data for a `writeValueWithResponse`
		 * call that was triggered elsewhere in my application.
		 *
		 * ¯\_(ツ)_/¯
		 */

		/**
		 * Parse the data so that my application can actually use it.
		 *
		 * ¯\_(ツ)_/¯
		 */

		characteristic.removeEventListener(
			"characteristicvaluechanged",
			onCharacteristicValueChanged
		)

		resolve(myData)
	}

	characteristic.addEventListener(
		"characteristicvaluechanged",
		onCharacteristicValueChanged
	)

	// Let the characteristic know what data I want to query.
	const GET_MY_DATA_COMMAND = 1
	const PAYLOAD = [0, 1]

	/**
	 * Ensure that the device is actually ready to receive and send the data,
	 * so that it does not throw errors – telling me that it is busy.
	 *
	 * ¯\_(ツ)_/¯
	 */

	await characteristic.writeValueWithResponse(
		new Uint8Array([MY_DATA_OPCODE, ...PAYLOAD]),
	)
})

✨ Blu

class MyDataResponse extends BluResponse {
	static validatorFunction(response) {
		return response.data?.getUint8(0) === Command.GET_MY_DATA
	}

	get myData() {
		return this.data?.getUint8(1)
	}
}

class MyDataRequest extends BluRequest {
	responseType = MyDataResponse

	constructor(...payload) {
		super(convert.toUint8Array([Command.GET_MY_DATA, ...payload]))
	}
}

const response = await characteristic.request(new MyDataRequest(0, 1))

return response.myData

Global operation queueing

Blu automatically queues all GATT (Generic Attribute Profile) operations your application triggers. This prevents "device busy" errors and potential data loss and enables you to have truly asynchronous communication with a connected device, globally – across your whole application.

Support for Web Bluetooth polyfills

Blu can also be used in environments where Web Bluetooth is not natively available, i.e. where globalThis.navigator.bluetooth is missing, by allowing you to register a custom polyfill. You could, for example take the bluetooth-le plugin for Capacitor and to run your Blu-based application on iOS devices, where Web Bluetooth support is still lacking due to WebKit.

Type-safety

Blu is built with TypeScript, offering you a strongly typed and safely extendable interface.

Usage

Import Blu

The Blu framework is packaged as a minified ECMAScript module with source maps.

import blu from "@blu.js/blu"

// blu.bluetooth
// blu.configuration
// blu.convert
// blu.scanner
// blu.version
import {
	BluDevice,
	BluCharacteristic,
	bluetooth,
	configuration,
	// ...
} from "@blu.js/blu"

Use Blu in your project

You can find a detailed guide on how to use Blu in this repo's wiki.

How to implement your own device with Blu

Building

To start building Blu locally, run the following:

git clone https://github.com/maxherrmann/blu.git && cd blu && npm i

Build scripts

Build package

npm run build

Builds the package with esbuild and DTS Bundle Generator.

Format code

npm run format

Formats the source code with Prettier.

Lint code

npm run lint

Lints the source code with ESLint.

Lint and fix code

npm run lint:fix

Lints the source code with ESLint and fixes all auto-solvable issues.

Update dependencies

npm run update

Interactively updates all dependencies with ncu.

Update dependencies

npm run update:auto

Automatically updates all dependencies with ncu.