jasongin / noble-uwp

Noble (Node.js Bluetooth LE) with Windows 10 UWP bindings
MIT License
83 stars 45 forks source link

On windows, toggling BT off and back on leaves noble-uwp confused #87

Open pursual opened 4 years ago

pursual commented 4 years ago
  noble-uwp bluetooth radio is off +7m
  noble stateChange poweredOff +7m
  noble-uwp stopScanning() +6ms
  noble-uwp keepAlive(false) => 0 +1ms
  noble-uwp bluetooth radio is off +112ms

  noble-uwp bluetooth radio is on +9s
  noble stateChange poweredOn +9s.
  noble-uwp startScanning(, true) +5ms
  noble-uwp keepAlive(true) => 1 +0ms
  noble-uwp watcher aborted +2ms   <----- noble-uwp finds aborted watcher, and stops scan right after user started scan.
  noble scanStop +6ms
Noble stopped scanning.
  noble-uwp bluetooth radio is on +148ms
pursual commented 4 years ago

Seems to be a matter of timing?

modifying bindings.js to add a delay in NobleBindings.prototype.startScanning eliminates the issue?

setTimeout(() => {
  this._advertisementWatcher.start();
  rt.keepAlive(true);
}, 200);
pursual commented 4 years ago

Turns out you can also add the delay in your noble code, as opposed to modifying the bindings.js:

noble.on('stateChange', (state) => {
  console.log('BLE stateChange:', state);
  if (state === 'poweredOn') {
    setTimeout(() => {
      //start scanning here. 
    }, 300);
  }
  if (state === 'poweredOff') {
    // stop scanning, etc
  }
});

Winrt bindings had same issue: https://github.com/Timeular/noble-winrt/issues/21