A Node.js API for BlueZ with native TypeScript support.
npm install @clebert/node-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 and
@clebert/node-plant-sensor) in
the context of home automation.
```
npm install @clebert/node-bluez @clebert/node-d-bus
- Designed from the ground up with TypeScript.
- Uses a solid and well-tested D-Bus implementation to communicate with BlueZ.
- Tested with Node.js 14 on Raspberry Pi OS Lite.
`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],le
transport: ,
});
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);
});
`
Create the /etc/dbus-1/system.d/node-bluez.conf configuration file. The
username may need to be modified.
`xml
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
``