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

bt-presence

v2.0.0

Published

Detects collective presence or absence of a set of bluetooth devices

Downloads

32

Readme

npm

bt-presence

A node.js module that uses the Linux-based BlueZ package's l2ping binary to detect the collective presence (or absence) of 1 or more bluetooth devices.

Assumptions / Dependencies

  • You're running a Linux-based host
  • The BlueZ bluetooth package is installed, including the l2ping cmd-line tool
  • node.js is installed

Installation

npm install --save bt-presence

A Note on Requirement for Root Priveleges

The l2ping linux binary requires root priveleges in order to write to bluetooth. You have 2 options for addressing this issue:

  1. Run sudo setcap 'cap_net_raw+eip' `which l2ping` in order to grant root priveleges to l2ping
  2. Run whichever software you are integrating bt-presence into as a user with root priveleges

Option #1 is recommended since it prevents bt-presence (and your code that may depend on it) from needing to run with escalated priveleges.

Usage

Including the package

const btPresence = require('bt-presence').btPresence

Instantiating a New Scanner

You must instantiate a new instance of btPresence prior to use

let btp = new btPresence()

Adding Addresses to Scan

You can add addresses to scan by supplying an array of MAC addresses. The devices in the supplied array are concatenated to, and do not replace, any existing devices that may have already been added.

btp.addDevices(["00:12:34:56:78:90", "09:87:65:43:21:00"])

Removing Addresses from Scan

You can remove addresses from the scan by supplying an array of MAC addresses.

btp.removeDevices(["00:12:34:56:78:90", "09:87:65:43:21:00"])

Setting/Replacing List of Addresses to Scan

You can replace the entire list of addresses to scan by supplying an array of MAC addresses.

btp.setDevices(["00:12:34:56:78:90", "09:87:65:43:21:00"])

Getting List of Addresses to Scan

Returns the entire list of addresses to scan.

btp.getDevices()

Configuring Ping Options

If you desire to change the parameters used with l2ping (for ping count and timeout):

btp.setScanOptions({ count: 3, timeoutSecs: 8 })

Getting Ping Options

Returns the current l2ping scan options

btp.getPingOptions()

Detecting Change: from No Devices Present to 1+ Devices Present

Subscribe to the 'present' event in order to know when 1 or more devices have appeared after a state where none were present prior.

btp.on('present', (macAddress) => console.log(`A device [${macAddress}] is now present`))

Detecting Change: from No Devices Present to 1+ Devices Present

Subscribe to the 'not-present' event in order to know when all devices are gone.

btp.on('not-present', (macAddress) => console.log(`All devices have disappeared`))

Starting the Scan

This starts the scan. By default, the first positive or negative reponse from any device in a newly started scan will be emitted as a change. To disable this, call btp.start(false).

btp.start(true)

Stopping the Scan

btp.stop()

Adjusting the Scan Interval

You can adjust the time interval upon which the entire list of devices is pinged to detect presence or absence. The default scan interval is 15 seconds; use the function below to adjust it. The underlying ping tool, l2ping, takes around 2-3 secs to complete when a device is present, and 5-10 secs to complete when a device is not present. So, consider how many devices you are scanning before you set this.

btp.setScanInterval(60)

Getting the Scan Interval

Returns the number of seconds between scans

btp.getScanInterval()

Contributing / Modifying

The project is now developed in TypeScript. To modify the code for your own use:

  1. Clone the repo with git clone https://github.com/cmvee/bt-presence.git
  2. Edit the .ts files in the src directory
  3. npm run build to transpile the TypeScript .ts source files into Javascript
  4. Look in the dist directory for the fresh Javascript files