@abandonware/bluetooth-hci-socket 中文文档教程

发布于 3年前 浏览 35 项目主页 更新于 3年前

node-bluetooth-hci-socket

Node.js 的蓝牙 HCI 套接字绑定

注意: 目前仅支持 LinuxFreeBSDWindows

Prerequisites

注意: node-gyp 仅在 npm 找不到适用于您的操作系统版本的二进制文件时才需要,否则二进制文件是预构建的。

Linux

  • Bluetooth 4.0 Adapter

注意: node-usb 依赖项可能会安装失败,这没关系,因为它是一个可选的可选依赖项。 通过 Linux 发行版的包管理器安装 libudev-dev 将解决该问题。

Windows

这个库需要原始 USB 访问蓝牙 4.0 USB 适配器,因为它需要绕过 Windows 蓝牙堆栈。

需要 WinUSB 驱动程序,使用 Zadig 工具 用于替换适配器的驱动程序。

警告:这将使适配器在 Windows 蓝牙设置中不可用! 要回滚到原始驱动程序,请转到:<代码>设备管理器 -> 打开设备 -> 更新驱动程序 注意:

  • that one should select "Delete the driver software for this device" as per Zadig instructions if the generation of the system restoral point by Zadig fails if one wishes to use restore system restoral point as an option.

Compatible Bluetooth 4.0 USB Adapter's

NameUSB VIDUSB PID
BCM920702 Bluetooth 4.00x0a5c0x21e8
BCM920702 Bluetooth 4.00x0a5c0x21f1
BCM20702A0 Bluetooth 4.00x19ff0x0239
BCM20702A0 Bluetooth 4.00x04890xe07a
BCM20702A0 Bluetooth 4.00x413c0x8143
CSR8510 A100x0a120x0001
Asus BT-4000x0b050x17cb
Intel Wireless Bluetooth 62350x80870x07da
Intel Wireless Bluetooth 72600x80870x07dc
Intel Wireless Bluetooth 72650x80870x0a2a
Intel Wireless Bluetooth 82650x80870x0a2b
Belkin BCM20702A00x050D0x065A
Dell Precision 55300x80870x0025

Compatible Bluetooth 4.1 USB Adapter's

NameUSB VIDUSB PID
BCM2045A0 Bluetooth 4.10x0a5c0x6412
Marvell AVASTAR0x12860x204C

Install

npm install @abandonware/bluetooth-hci-socket

Usage

var BluetoothHciSocket = require('@abandonware/bluetooth-hci-socket');

Actions

Create

var bluetoothHciSocket = new BluetoothHciSocket();

Set Filter

var filter = new Buffer(14);

// ...

bluetoothHciSocket.setFilter(filter);

注意如果使用bindRaw,则不需要setFilter

Bind

Raw Channel
bluetoothHciSocket.bindRaw([deviceId]); // optional deviceId (integer)
User Channel
bluetoothHciSocket.bindUser([deviceId]); // optional deviceId (integer)

要求设备处于断电状态(sudo hciconfig hciX down)。

Control Channel
bluetoothHciSocket.bindControl();

Is Device Up

查询设备状态。

var isDevUp = bluetoothHciSocket.isDevUp(); // returns: true or false

注意:必须在bindRaw之后调用。

Start/stop

开始或停止事件处理:

bluetoothHciSocket.start();

// ...

bluetoothHciSocket.stop();

注意:必须在bindRawbindControl之后调用。

Write

var data = new Buffer(/* ... */);

// ...


bluetoothHciSocket.write(data);

注意:必须在bindRawbindControl之后调用。

Events

Data

bluetoothHciSocket.on('data', function(data) {
  // data is a Buffer

  // ...
});

Error

bluetoothHciSocket.on('error', function(error) {
  // error is a Error

  // ...
});

Examples

有关代码示例,请参阅示例文件夹

Platform Notes

Linux

Force Raw USB mode

卸载 btusb 内核模块:

sudo rmmod btusb

设置 BLUETOOTH_HCI_SOCKET_FORCE_USB 环境变量:

sudo BLUETOOTH_HCI_SOCKET_FORCE_USB=1 node <file>.js

FreeBSD

通过将 no-ubt.conf 进入 /usr/local/etc/devd/no-ubt.conf 并重新启动 devd(sudo service devd restart ).

卸载 ng_ubt 内核模块(如果已加载):

sudo kldunload ng_ubt

OS X

Disable CSR USB Driver

sudo kextunload -b com.apple.iokit.CSRBluetoothHostControllerUSBTransport

Disable Broadcom USB Driver

sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport

Windows

Force adapter USB VID and PID

设置 BLUETOOTH_HCI_SOCKET_USB_VIDBLUETOOTH_HCI_SOCKET_USB_PID 环境变量。

USB 设备 ID 示例:050d:065a:

set BLUETOOTH_HCI_SOCKET_USB_VID=0x050d
set BLUETOOTH_HCI_SOCKET_USB_PID=0x065a

node <file>.js

node-bluetooth-hci-socket

Bluetooth HCI socket binding for Node.js

NOTE: Currently only supports Linux, FreeBSD and Windows.

Prerequisites

NOTE: node-gyp is only required if the npm cannot find binary for your OS version otherwise the binaries are prebuilt.

Linux

  • Bluetooth 4.0 Adapter

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.

Windows

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:

  • that one should select "Delete the driver software for this device" as per Zadig instructions if the generation of the system restoral point by Zadig fails if one wishes to use restore system restoral point as an option.

Compatible Bluetooth 4.0 USB Adapter's

NameUSB VIDUSB PID
BCM920702 Bluetooth 4.00x0a5c0x21e8
BCM920702 Bluetooth 4.00x0a5c0x21f1
BCM20702A0 Bluetooth 4.00x19ff0x0239
BCM20702A0 Bluetooth 4.00x04890xe07a
BCM20702A0 Bluetooth 4.00x413c0x8143
CSR8510 A100x0a120x0001
Asus BT-4000x0b050x17cb
Intel Wireless Bluetooth 62350x80870x07da
Intel Wireless Bluetooth 72600x80870x07dc
Intel Wireless Bluetooth 72650x80870x0a2a
Intel Wireless Bluetooth 82650x80870x0a2b
Belkin BCM20702A00x050D0x065A
Dell Precision 55300x80870x0025

Compatible Bluetooth 4.1 USB Adapter's

NameUSB VIDUSB PID
BCM2045A0 Bluetooth 4.10x0a5c0x6412
Marvell AVASTAR0x12860x204C

Install

npm install @abandonware/bluetooth-hci-socket

Usage

var BluetoothHciSocket = require('@abandonware/bluetooth-hci-socket');

Actions

Create

var bluetoothHciSocket = new BluetoothHciSocket();

Set Filter

var filter = new Buffer(14);

// ...

bluetoothHciSocket.setFilter(filter);

Note: setFilter is not required if bindRaw is used.

Bind

Raw Channel
bluetoothHciSocket.bindRaw([deviceId]); // optional deviceId (integer)
User Channel
bluetoothHciSocket.bindUser([deviceId]); // optional deviceId (integer)

Requires the device to be in the powered down state (sudo hciconfig hciX down).

Control Channel
bluetoothHciSocket.bindControl();

Is Device Up

Query the device state.

var isDevUp = bluetoothHciSocket.isDevUp(); // returns: true or false

Note: must be called after bindRaw.

Start/stop

Start or stop event handling:

bluetoothHciSocket.start();

// ...

bluetoothHciSocket.stop();

Note: must be called after bindRaw or bindControl.

Write

var data = new Buffer(/* ... */);

// ...


bluetoothHciSocket.write(data);

Note: must be called after bindRaw or bindControl.

Events

Data

bluetoothHciSocket.on('data', function(data) {
  // data is a Buffer

  // ...
});

Error

bluetoothHciSocket.on('error', function(error) {
  // error is a Error

  // ...
});

Examples

See examples folder for code examples.

Platform Notes

Linux

Force Raw USB mode

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

FreeBSD

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

OS X

Disable CSR USB Driver

sudo kextunload -b com.apple.iokit.CSRBluetoothHostControllerUSBTransport

Disable Broadcom USB Driver

sudo kextunload -b com.apple.iokit.BroadcomBluetoothHostControllerUSBTransport

Windows

Force adapter USB VID and PID

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
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文