依赖安装
noble 在 Linux 和 MacOS 上的安装还是比较简单的,所以可以参考 noble/github 上的介绍就可以了。以下着重演示以下 Window10 下的依赖安装和驱动替换。
首先,我们需要安装的是 node-gyp / github。
在 WIndow 下安装,需要先使用管理员权限打开 powershell 或者 CMD命令行工具,这里推荐使用 powershell。打开后,输入以下命令:
npm install --global --production windows-build-tools
等待安装完成后即可。
然后,我们需要继续安装 (node-bluetooth-hci-socket / github)[https://github.com/noble/node-bluetooth-hci-socket#windows] 库,在安装之前,我们需要先替换我们的 USB蓝牙适配器驱动。这里要注意的是,当我们替换掉驱动以后,我们就不能在 Window 设置里面使用蓝牙了,如果想要恢复,可以通过 设备管理器-打开设备-更新驱动 把驱动替换回来。
替换驱动我们需要使用一个 Zadig 的工具,下载后,打开软件,然后选择 Option - List All Devices ,再在下拉菜单中选中我们的设备,我这款绿联的USB蓝牙适配器的名称是 CSR8510 A10,关于这个蓝牙的名称,可以在 设置 - 蓝牙和其他设备 中查看。
选择好以后,选择 Reinstall Diver ,等待片刻即可。

完成上面的替换驱动操作后,再使用下面的代码安装 node-bluetooth-hci-socket 即可:
npm install bluetooth-hci-socket
最后,我们再把 (noble / github)[https://github.com/noble/noble] 安装好即可:
npm install noble
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ble Hello World!</title>
</head>
<style>
#bleList button{
margin-right: 5px;
}
.ble {
display: flex;
}
.ble button {
margin-left: 10px;
}
.char button {
margin-left: 10px;
}
</style>
<body>
<script>
window.onload = function () {
var noble = require('noble');
var peripherals = {};
var _characteristics = {};
// 扫描
function scan(state){
if (state === 'poweredOn') {
noble.startScanning();
console.log("Started scanning");
} else {
noble.stopScanning();
console.log("Is Bluetooth on?");
}
}
// 连接
function connects(e) {
console.log('======开始连接=======');
var name = this.getAttribute('id');
var keys = Object.keys(peripherals);
var services = {};
keys.forEach(v => {
if (v === name){
// 连接成功后
peripherals[v].connect(function (err) {
peripherals[v].discoverAllServicesAndCharacteristics(function (error, services, characteristics){
services.forEach((v, k) => {
var characteristicsNames = [];
v.characteristics.forEach((char, kf) => {
var properties = char.properties;
_characteristics['uuid_'+char.uuid] = char;
console.log(char);
var div = document.createElement("div");
div.setAttribute('id', char.name);
div.setAttribute('class', 'char');
div.innerHTML = `${char.name}`;
var readBut = document.createElement("button");
readBut.addEventListener('click', read);
readBut.setAttribute('id', char.uuid);
readBut.innerHTML='read';
var writeBut = document.createElement("button");
writeBut.addEventListener('click', write);
writeBut.setAttribute('id', char.uuid);
writeBut.innerHTML='write';
if (char.properties.includes('read')) div.appendChild(readBut);
if (char.properties.includes('write')) div.appendChild(writeBut);
// if
characteristicsNames.push(div);
})
var service = document.getElementById('service');
var div = document.createElement("div");
div.setAttribute('id', v.name)
div.innerHTML = `
<h5>name: ${v.name}</h5>
`;
characteristicsNames.forEach((v, k) => {
div.appendChild(v);
});
service.appendChild(div);
})
});
})
}
});
console.log('======开始断开=======');
}
// 读取
function read(){
var uuid = this.getAttribute('id');
var char = _characteristics['uuid_'+uuid];
console.log(char);
console.log('读取成功');
char.read(function(error, data) {
console.log(data.toString());
})
}
// 写入
function write() {
console.log('写入成功');
}
// 断开
function dis(name){
console.log(name);
console.log('连接成功');
}
// 扫描到之后
function foundPeripheral (peripheral) {
var name = peripheral.advertisement.localName;
var bleList = document.getElementById('bleList');
// console.log(peripheral);
// bleList.innerHTML = `<button>${name}</button>`;
var ble = document.createElement("div");
var connectBtn = document.createElement("button");
var disconBtn = document.createElement("button");
ble.className = 'ble';
connectBtn.innerHTML = '连接';
disconBtn.innerHTML = '断开';
connectBtn.setAttribute('id', name);
disconBtn.setAttribute('id', name);
connectBtn.addEventListener('click', connects);
disconBtn.addEventListener('click', dis);
ble.innerHTML = `
<div class='name' >${name}</div>
`;
ble.appendChild(connectBtn);
ble.appendChild(disconBtn);
bleList.appendChild(ble);
peripherals[name] = peripheral;
};
var discover = document.getElementById("discover");
// var connect = document.getElementById("connect");
discover.addEventListener("click", function () {
noble.on('stateChange', scan);
// for every peripheral we discover, run this callback function
console.log('开始扫描');
noble.on('discover', foundPeripheral);
})
}
</script>
<br>
<h2>BLE Server</h2>
<br>
<button id="discover">开始扫描蓝牙</button>
<h3>蓝牙列表</h3>
<div id="bleList">
</div>
<h4>Server操作</h4>
<div id='service'>
</div>
</body>
</html>
运行效果:

参考资料
tigoe/BluetoothLE-Examples
noble/noble
nodejs/node-gyp
noble/node-bluetooth-hci-socket
参考 : noble
1017



被折叠的 条评论
为什么被折叠?



