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

node-hue-ble

v1.1.0

Published

Connect to your Hue Philips lights using Bluetooth

Downloads

2

Readme

node-hue-ble

Installation

npm i node-hue-ble

This library uses @abandonware/noble, please see the prerequisites for your operating system before using this library

Example

This example uses Bluetooth to find nearby lamps, connects to the first one found, turn it on, set the color to white and the brightness to 100% and disconnect

import { Client } from "node-hue-ble";

const scanner = await Client.scanForLamps()
scanner.on("discover", async peripheral => {
    console.log("Discovered peripheral", peripheral.advertisement.localName, peripheral.uuid)
    scanner.stopScanning()

    const lamp = new Client(peripheral)
    await lamp.connect()
    console.log("Connected to", await lamp.getLampName())

    await lamp.on()
    await lamp.setBrightness(100)
    // if your lamp does not support colors, you can use
    // .setTemperature(100) instead
    await lamp.setRGBColor("#ffffff")

    await lamp.disconnect()
    console.log("Disconnected")
    process.exit(0)
})
Discovered peripheral Bedroom xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Connected to Bedroom
Disconnected

API

Client

The Client class provides methods to interact with a smart lamp device over Bluetooth Low Energy (BLE). It is designed to manage connections, discover services and characteristics, and perform various operations such as turning the lamp on or off, setting brightness, temperature, and color, as well as managing the lamp name.

Importing

import { Client } from 'node-hue-ble';

Static Methods

static async scanForLamps(): Promise<Scanner>

Scans for nearby lamps and returns a Scanner instance.

Instance Methods

constructor(peripheral: noble.Peripheral)

Creates a new Client instance with the specified noble.Peripheral.

async connect()

Connects to the peripheral and discovers the services and characteristics.

async disconnect()

Disconnects from the peripheral.

async off()

Turns off the lamp.

async on()

Turns on the lamp.

async toggle()

Toggles the lamp's state.

async isOn(): Promise<boolean>

Returns whether the lamp is on or not.

async setBrightness(brightness: number)

Sets the brightness of the lamp (0-100).

async getBrightness(): Promise<number>

Returns the current brightness of the lamp.

async setTemperature(temperature: number)

Sets the color temperature of the lamp (0-100).

async getTemperature(): Promise<number>

Returns the current color temperature of the lamp.

async setRGBColor(rgb: number[]|string|number|Buffer)

Sets the color of the lamp using an RGB value, which can be provided in various formats (array, string, number, or Buffer).

async setColor(color: number[])

Sets the color of the lamp using a 4-element array.

async getColor(): Promise<number[]>

Returns the current color of the lamp as a 4-element array.

async getLampName(): Promise<string|null>

Returns the name of the lamp.

async setLampName(name: string)

Sets the name of the lamp.

Properties

  • peripheral: noble.Peripheral
  • services: noble.Service[]
  • characteristics: noble.Characteristic[]
  • lightService: noble.Service
  • lightCharacteristic: noble.Characteristic
  • brightnessCharacteristic: noble.Characteristic
  • temperatureCharacteristic: noble.Characteristic
  • colorCharacteristic: noble.Characteristic
  • settingsService: noble.Service
  • lampNameCharacteristic: noble.Characteristic

Note: These properties are mostly used internally and should not be accessed directly. Use the provided instance methods to interact with the lamp.

Scanner

The Scanner class is responsible for scanning and discovering BLE peripherals with the specified services. It extends the EEventEmitter class and emits a "discover" event when a matching peripheral is found.

Importing

import Scanner from 'node-hue-ble';

Instance Methods

constructor(services: string[])

Creates a new Scanner instance with the specified array of services UUIDs.

async startScanning()

Starts scanning for peripherals with the specified services. Emits a "discover" event when a matching peripheral is found.

async stopScanning()

Stops scanning for peripherals and removes the "discover" event listener.

Events

discover

Emitted when a matching peripheral is discovered. The event payload contains the discovered noble.Peripheral object.

Usage Example

import Scanner from './path/to/Scanner.js';
import { LIGHT_SERVICE, SETTINGS_SERVICE } from './constants.js';

const scanner = new Scanner([LIGHT_SERVICE, SETTINGS_SERVICE]);

scanner.on('discover', (peripheral) => {
    console.log('Discovered peripheral:', peripheral);
});

await scanner.startScanning();
setTimeout(async () => {
    await scanner.stopScanning();
}, 5000);

In this example, the scanner starts scanning for peripherals with the specified services for 5 seconds. When a matching peripheral is found, it logs the discovered peripheral to the console.