【发布时间】:2021-08-01 22:27:45
【问题描述】:
在我的 Raspberry Pi 4 上第一次运行以下代码(来自给定 here 的示例之一)后,它运行良好并且可以找到许多设备。但是当我重新运行时,扫描功能坏了,只能找到几个设备:
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning();
} else {
noble.stopScanning();
}
});
noble.on('discover', function(peripheral) {
console.log('peripheral discovered (' + peripheral.id +
' with address <' + peripheral.address + ', ' + peripheral.addressType + '>,' +
' connectable ' + peripheral.connectable + ',' +
' RSSI ' + peripheral.rssi + ':');
console.log('\thello my local name is:');
console.log('\t\t' + peripheral.advertisement.localName);
console.log('\tcan I interest you in any of the following advertised services:');
console.log('\t\t' + JSON.stringify(peripheral.advertisement.serviceUuids));
var serviceData = peripheral.advertisement.serviceData;
if (serviceData && serviceData.length) {
console.log('\there is my service data:');
for (var i in serviceData) {
console.log('\t\t' + JSON.stringify(serviceData[i].uuid) + ': ' + JSON.stringify(serviceData[i].data.toString('hex')));
}
}
if (peripheral.advertisement.manufacturerData) {
console.log('\there is my manufacturer data:');
console.log('\t\t' + JSON.stringify(peripheral.advertisement.manufacturerData.toString('hex')));
}
if (peripheral.advertisement.txPowerLevel !== undefined) {
console.log('\tmy TX power level is:');
console.log('\t\t' + peripheral.advertisement.txPowerLevel);
}
console.log();
});
如果我重置设备,问题就会消失。如果我通过bluetoothctl 然后scan on 扫描,则完全没有问题。我可以根据需要开启和关闭。所以我的猜测是这个问题与noble.js
BlueZ 版本是 5.50。
【问题讨论】:
-
@ukBaz 则与
hciAPI 有关。如何准确定位问题?Command disallowed错误没有把我带到任何地方。 -
@ukBaz 我正在手动停止发现,因为它会永远运行。我也没有收到任何通知。如果我重新扫描,就会出现问题。然后,我在哪里进行扫描并不重要,
noble.js和bluetoothctl scan on都只能找到少数(1 个或 2 个)设备。我想,我需要深入了解noble.js以了解是什么破坏了扫描。 -
好的,我可能误解了您遇到的问题。你需要允许重复吗? github.com/noble/noble/blob/…
-
@ukBaz 抱歉,如果问题在问题中不清楚。问题是,如果我们通过
noble.js运行它,scan函数在运行几次后无法按预期工作。通常,scan会发现很多设备并且是连续的。但是,如果我们通过noble.js多次scan,它就坏了。仅找到 1 和 2 个设备,它会停止发现。然后在btmon,我得到command disallowed,它没有带我到任何地方。
标签: javascript bluetooth bluetooth-lowenergy bluez raspberry-pi4