sandeepmistry / node-sensortag

Node.js lib for the TI SensorTag
MIT License
218 stars 111 forks source link

Cannot close onDiscover after disconnect #65

Closed martin-doyle closed 8 years ago

martin-doyle commented 8 years ago

If a SensorTag gets disconnected the onDiscover function is still running. In test.js of SensorTag, the script is closed via process.exit(0). This is OK, if you have only 1 SensorTag running and you want to stop the whole script in case of a disconnect. Is it possible to close the onDiscover function when a disconnect happens?

sandeepmistry commented 8 years ago

@martin-doyle what makes you say onDiscover is still running?

martin-doyle commented 8 years ago

@sandeepmistry If I skip the process.exit(0) in the test.js than the program keeps running and needs to be stopped by Ctrl C.

  sensorTag.on('disconnect', function() {
    console.log('disconnected!');
    process.exit(0);
  });

In my application I do a reconnect in case of a lost connection (for example if I change the battery):

  sensorTag.on('disconnect', function () {
    console.log('Sensortag disconnected.');
    SensorTag.discoverAll(onDiscover);
  });

Then, with every reconnect the disconnect message 'Sensortag disconnected.' gets fired multiple times telling me all onDiscover functions are still running. I have not found any solution in the samples other than process.exit(0).

sandeepmistry commented 8 years ago

@martin-doyle for the exit question, let's move the discussion here: https://github.com/sandeepmistry/noble/issues/299

It looks like you want to use .once instead of .on as your are not removing the listener, and noble re-uses the peripheral / sensor tag instance so multiple listeners are added.

  sensorTag.once('disconnect', function () {
    console.log('Sensortag disconnected.');
    SensorTag.discoverAll(onDiscover);
  });
martin-doyle commented 8 years ago

@sandeepmistry Just cool ! This is exactly I was looking for. The noble framework works like a charm. Thanks a lot.