发现少数设备后 BLE 扫描停止

时间:2021-05-11 14:23:40

标签: javascript bluetooth bluetooth-lowenergy bluez raspberry-pi4

第一次运行以下代码后,该代码来自在我的 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

有关

这是 sudo btmon 的输出,扫描停止。sudo btmon output

这是 scan on 如何挂起的图像。scan on hanging

BlueZ 版本是 5.50

1 个答案:

答案 0 :(得分:0)

问题是当重复项设置为false时,我只是在没有停止扫描的情况下退出脚本。因此,当我重新运行时,只发现了一些新设备。