Package Exports
- @kojibuta/ble-hci-central
- @kojibuta/ble-hci-central/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@kojibuta/ble-hci-central) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ble-hci-central
Bluetooth LE HCI Central for Node.js.
Mostly based on node-bluetooth-hci-socket and noble projects.
NOTE: Currently only supports Linux.
Prerequisites
On-board Bluetooth 4.0+ controller.
NOTE: node-gyp
is only required if the npm cannot find binary for your OS version.
Install
npm install @kojibuta/ble-hci-central
Usage
import central from "@kojibuta/ble-hci-central";
const onAdvertisement = async (
type,
addressType,
address,
advLength,
advData,
rssi,
numReports
) => {
// Wait for an advertisement from target device C1:79:C4:77:5A:06
if (address !== "c179c4775a06") return;
central.off("advertisement", onAdvertisement);
// Stop scanning before opening a connection
await central.stopScanningAsync();
// Connect to device
const connection = await central.connectAsync(addressType, address);
console.log("connected", connection);
// Discover all services, characteristics and descriptors
const services = await central.discoverAsync(address, true);
console.log("services", JSON.stringify(services, null, 4));
// Read device name
const handle = services["1800"]?.characteristics["2a00"]?.handle;
if (handle) {
const result = await central.readAsync(address, handle);
if (result.error) {
console.log("error reading device name", result.error);
} else {
console.log("device name", result.value.toString("utf8"));
}
}
// Close connection
await central.disconnectAsync(address);
// That's all folks
process.exit(0);
};
// Start BLE HCI Central
central.start();
// Register advertisement callback
central.on("advertisement", onAdvertisement);
// Set scan parameters (active scan, allow duplicates)
central.setScanParameters(1, 0x20, 0x20, 0, 0);
// Start scanning
central.startScanning();
Examples
See examples folder for code examples.
Platform Notes
Linux
Install system packages
sudo apt install bluez libbluetooth-dev libcap2-bin cmake
Grant permissions to Node.js
If not running as root, following permissions must be granted to node executable.
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(eval readlink -f `which node`)