Commit 4cc2ab5b authored by Donald Chen's avatar Donald Chen
Browse files

stop scanning before connecting

parent 0250a21c
......@@ -29,6 +29,10 @@ function startScanning() {
}
}
function stopScanning() {
noble.stopScanning();
}
/**
* Called when a new device is discovered
*/
......@@ -38,14 +42,17 @@ function onDiscover(beaconInst) {
// Ignore duplicates
if (devices[device.id]) {
console.log(`${device.id}: Duplicate device found, ignoring`);
startScanning();
return;
}
console.log(`Discovered: ${device.id}`);
else {
console.log(`Discovered: ${device.id}`);
//must stop scanning to initiate a connection
stopScanning()
// Connect to device and setup
device.setUp()
.then(() => {
// Connect to device and setup
device.setUp()
.then(() => {
device.onLuxometerChange((d, lux) => {
const telemetry = {
deviceID: d.id,
......@@ -54,21 +61,26 @@ function onDiscover(beaconInst) {
provider.sendTelemetry(telemetry);
});
// Wait for as long as possible before scanning again to avoid race
// conditions
startScanning();
// Store in list of known devices
devices[device.id] = device;
// Store in list of known devices
devices[device.id] = device;
// Set disconnect handler
device.bleDevice.on('disconnect', function disconnectHandler() {
console.log(`${device.id}: disconnected`);
delete devices[device.id];
device.bleDevice.removeListener('disconnect', disconnectHandler);
});
// Set disconnect handler
device.bleDevice.on('disconnect', function onDc() {
console.log(`${device.id}: disconnected`);
delete devices[device.id];
device.bleDevice.removeListener('disconnect', onDc);
// Wait for as long as possible before scanning again to avoid race
// conditions
startScanning();
})
.catch((error) => {
console.log(error)
process.exit(1)
});
});
}
}
}
noble.on('scanStop', () => {
console.log('Scanning stopped');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment