Bluetooth HCI socket binding for Node.js
Understanding the functionality and benefits of the npm package @abandonware/bluetooth-hci-socket is crucial for developers working on Bluetooth technology applications. This node module provides a direct binding to the Bluetooth Host Controller Interface (HCI) socket in Node.js environments, enabling developers to have low-level access to the Bluetooth stack. It is particularly useful for those who need to implement custom Bluetooth connectivity solutions without the overhead of additional libraries or tools. The package supports various Bluetooth adapters and allows for the implementation of both BLE (Bluetooth Low Energy) and traditional Bluetooth functionalities. This flexibility makes it an essential tool for developing robust and efficient Bluetooth applications.
To get started with this powerful module, developers can simply run the command `npm install @abandonware/bluetooth-hci-socket` in their project environment. This installation command integrates the module into your Node.js project, setting the stage for direct interaction with Bluetooth devices. Once installed, it enables the handling of all HCI events and commands directly from within your JavaScript code, providing a seamless and powerful interface for Bluetooth operations. The ability to work directly with HCI commands and events allows for a more fine-tuned and optimized control over Bluetooth communications, crucial for applications requiring high levels of reliability and performance.
The @abandonware/bluetooth-hci-socket module is not only powerful but also maintained under the Abandonware organization, ensuring that it stays up-to-date with current technology trends and community needs despite the original repository no longer being actively maintained. This aspect of the module ensures that developers can rely on the tool for both current and future projects, as the community-driven updates and patches keep the module relevant and functional. Leveraging this module can significantly cut down development time and increase the efficiency of Bluetooth-based applications, making it an invaluable asset in the toolkit of developers specializing in IoT and Bluetooth technologies.
Core dependencies of this npm package and its dev dependencies.
@mapbox/node-pre-gyp, debug, nan, node-gyp, jshint
A README file for the @abandonware/bluetooth-hci-socket code repository. View Code
Bluetooth HCI socket binding for Node.js
NOTE: Currently only supports Linux, FreeBSD and Windows.
NOTE: node-gyp
is only required if the npm cannot find binary for your OS version otherwise the binaries are prebuilt.
Note: the node-usb dependency might fail install, this is ok, because it is an optional optional dependency. Installing libudev-dev
via your Linux distribution's package manager will resolve the problem.
This library needs raw USB access to a Bluetooth 4.0 USB adapter, as it needs to bypass the Windows Bluetooth stack.
A WinUSB driver is required, use Zadig tool to replace the driver for your adapter.
WARNING: This will make the adapter unavailable in Windows Bluetooth settings! To roll back to the original driver go to: Device Manager -> Open Device -> Update Driver
Note:
Name | USB VID | USB PID |
---|---|---|
BCM920702 Bluetooth 4.0 | 0x0a5c | 0x21e8 |
BCM920702 Bluetooth 4.0 | 0x0a5c | 0x21f1 |
BCM20702A0 Bluetooth 4.0 | 0x19ff | 0x0239 |
BCM20702A0 Bluetooth 4.0 | 0x0489 | 0xe07a |
BCM20702A0 Bluetooth 4.0 | 0x413c | 0x8143 |
CSR8510 A10 | 0x0a12 | 0x0001 |
Asus BT-400 | 0x0b05 | 0x17cb |
Intel Wireless Bluetooth 6235 | 0x8087 | 0x07da |
Intel Wireless Bluetooth 7260 | 0x8087 | 0x07dc |
Intel Wireless Bluetooth 7265 | 0x8087 | 0x0a2a |
Intel Wireless Bluetooth 8265 | 0x8087 | 0x0a2b |
Belkin BCM20702A0 | 0x050D | 0x065A |
Dell Precision 5530 | 0x8087 | 0x0025 |
Name | USB VID | USB PID |
---|---|---|
BCM2045A0 Bluetooth 4.1 | 0x0a5c | 0x6412 |
Marvell AVASTAR | 0x1286 | 0x204C |
npm install @abandonware/bluetooth-hci-socket
var BluetoothHciSocket = require('@abandonware/bluetooth-hci-socket');
var bluetoothHciSocket = new BluetoothHciSocket();
var filter = Buffer.alloc(14);
// ...
bluetoothHciSocket.setFilter(filter);
Note: setFilter
is not required if bindRaw
is used.
bluetoothHciSocket.bindRaw([deviceId]); // optional deviceId (integer)
bluetoothHciSocket.bindUser([deviceId]); // optional deviceId (integer)
Requires the device to be in the powered down state (sudo hciconfig hciX down
).
bluetoothHciSocket.bindControl();
Query the device state.
var isDevUp = bluetoothHciSocket.isDevUp(); // returns: true or false
Note: must be called after bindRaw
.
Start or stop event handling:
bluetoothHciSocket.start();
// ...
bluetoothHciSocket.stop();
Note: must be called after bindRaw
or bindControl
.
var data = Buffer.from(/* ... */);
// ...
bluetoothHciSocket.write(data);
Note: must be called after bindRaw
or bindControl
.
bluetoothHciSocket.on('data', function(data) {
// data is a Buffer
// ...
});
bluetoothHciSocket.on('error', function(error) {
// error is a Error
// ...
});
See examples folder for code examples.
Unload btusb
kernel module:
sudo rmmod btusb
Set BLUETOOTH_HCI_SOCKET_FORCE_USB
environment variable:
sudo BLUETOOTH_HCI_SOCKET_FORCE_USB=1 node <file>.js
Disable automatic loading of the default Bluetooth stack by putting no-ubt.conf into /usr/local/etc/devd/no-ubt.conf
and restarting devd (sudo service devd restart
).
Unload ng_ubt
kernel module if already loaded:
sudo kldunload ng_ubt
sudo kextunload -b com.apple.iokit.CSRBluetoothHostControllerUSBTransport
sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport
Set BLUETOOTH_HCI_SOCKET_USB_VID
and BLUETOOTH_HCI_SOCKET_USB_PID
environment variables.
Example for USB device id: 050d:065a:
set BLUETOOTH_HCI_SOCKET_USB_VID=0x050d
set BLUETOOTH_HCI_SOCKET_USB_PID=0x065a
node <file>.js