【问题标题】:BLE Scan stops after discovering few devices发现少数设备后 BLE 扫描停止
【发布时间】: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

有关

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

这是scan on 挂起的图像。

BlueZ 版本是 5.50

【问题讨论】:

  • @ukBaz 则与hci API 有关。如何准确定位问题? Command disallowed 错误没有把我带到任何地方。
  • @ukBaz 我正在手动停止发现,因为它会永远运行。我也没有收到任何通知。如果我重新扫描,就会出现问题。然后,我在哪里进行扫描并不重要,noble.jsbluetoothctl 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


【解决方案1】:

问题是我刚刚退出脚本而没有停止扫描,而 duplicates 设置为 false。因此,当我重新运行时,只发现了一些新设备。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多