https://github.com/clebert/node-bluez
A Node.js API for BlueZ with native TypeScript support.
https://github.com/clebert/node-bluez
Last synced: over 1 year ago
JSON representation
A Node.js API for BlueZ with native TypeScript support.
- Host: GitHub
- URL: https://github.com/clebert/node-bluez
- Owner: clebert
- License: mit
- Created: 2021-04-21T14:35:17.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-11-12T09:33:56.000Z (over 2 years ago)
- Last Synced: 2025-02-24T16:05:08.552Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 79.1 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Node.js BlueZ
> A Node.js API for BlueZ with native TypeScript support.
This package runs only on Linux and has a very limited feature set. It is
specially designed for controlling simple BLE devices (e.g.
[@clebert/node-switch-bot](https://github.com/clebert/node-switch-bot) and
[@clebert/node-plant-sensor](https://github.com/clebert/node-plant-sensor)) in
the context of home automation.
## Installation
```
npm install @clebert/node-bluez @clebert/node-d-bus
```
## Features
- Designed from the ground up with TypeScript.
- Uses a solid and well-tested D-Bus implementation to communicate with BlueZ.
## Usage example
```js
import {Adapter} from '@clebert/node-bluez';
import {SystemDBus} from '@clebert/node-d-bus';
(async () => {
const dBus = new SystemDBus();
await dBus.connectAsExternal();
try {
await dBus.hello();
const [adapter] = await Adapter.getAll(dBus);
if (!adapter) {
throw new Error(`Adapter not found.`);
}
const unlockAdapter = await adapter.lock.aquire();
try {
await adapter.setPowered(true);
await adapter.setDiscoveryFilter({
serviceUUIDs: [`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`],
transport: `le`,
});
await adapter.startDiscovery();
let device;
try {
device = await adapter.waitForDevice(`XX:XX:XX:XX:XX:XX`);
} finally {
await adapter.stopDiscovery();
}
await device.connect();
const gattCharacteristic = await device.waitForGattCharacteristic(
`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`,
);
await gattCharacteristic.writeValue([
`f`.charCodeAt(0),
`o`.charCodeAt(0),
`o`.charCodeAt(0),
]);
await device.disconnect();
} finally {
unlockAdapter();
}
} finally {
dBus.disconnect();
}
})().catch((error) => {
console.error(error);
process.exit(1);
});
```
## Configure user permissions
Create the `/etc/dbus-1/system.d/node-bluez.conf` configuration file. The
username may need to be modified.
```xml
```