Skip to content

Commit

Permalink
Clear discovered device on re-scan
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato committed Feb 28, 2021
1 parent b928a7b commit 7e03259
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion server/services/bluetooth/lib/commands/bluetooth.scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ const { TIMERS } = require('../utils/bluetooth.constants');
*/
async function scan(state, peripheralUuid = undefined) {
if (state) {
logger.trace(`Bluetooth: scanning for ${peripheralUuid} peripheral`);
if (peripheralUuid) {
logger.trace(`Bluetooth: scanning for ${peripheralUuid} peripheral`);
} else {
logger.trace(`Bluetooth: scanning for all peripherals`);
this.discoveredDevices = {};
}

this.scanCounter += 1;

if (this.scanPromise && this.scanPromise.isPending()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { expect } = require('chai');
const sinon = require('sinon');

const { fake, assert } = sinon;
Expand Down Expand Up @@ -75,14 +76,31 @@ describe('bluetooth.scan command', () => {
assert.calledOnce(stopScanningAsync);
});

it('should clear timeout', async () => {
it('should clear timeout, and discovered devices', async () => {
bluetoothManager.ready = true;
bluetoothManager.discoveredDevices = { one: {}, two: {} };

bluetoothManager.scan(true);
await bluetoothManager.scan(false);

assert.calledOnce(bluetooth.startScanning);
assert.notCalled(stopScanning);
assert.calledOnce(stopScanningAsync);

expect(bluetoothManager.discoveredDevices).deep.eq({});
});

it('should clear timeout, and keep discovered devices', async () => {
bluetoothManager.ready = true;
bluetoothManager.discoveredDevices = { one: {}, two: {} };
bluetoothManager.scan(true, 'any');

await bluetoothManager.scan(false);

assert.calledOnce(bluetooth.startScanning);
assert.notCalled(stopScanning);
assert.calledOnce(stopScanningAsync);

expect(bluetoothManager.discoveredDevices).deep.eq({ one: {}, two: {} });
});
});

0 comments on commit 7e03259

Please sign in to comment.