Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble getting notified when device disconnects #76

Closed
facultymatt opened this issue Jun 2, 2014 · 4 comments
Closed

Trouble getting notified when device disconnects #76

facultymatt opened this issue Jun 2, 2014 · 4 comments

Comments

@facultymatt
Copy link

I have a sample app and the peripheral.on('disconnect') is not working as expected.

var noble = require('noble');

noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    noble.startScanning([], false);
  } else {
    noble.stopScanning();
  }
});

noble.on('discover', function(peripheral) {
  peripheral.connect();
  peripheral.on('connect', function() {
    console.log('Hello ' + peripheral.advertisement.localName);
  });
  peripheral.on('goodbye', function() {
    console.log('Goodbye ' + peripheral.advertisement.localName);
  });
});

I would expect this would give me the message hello and goodbye as my bluetooth device moved in and out of range. The hello message works, but I never get a disconnect event.

Is there a better way to do this?

@sandeepmistry
Copy link
Collaborator

Two notes:

  • your code snippet is listening for a goodbye event instead of disconnect
  • How are you triggering a disconnect? Going out of range, triggering it peripheral side?

I've updated the snippet to disconnect after connecting:

var noble = require('noble');

noble.on('stateChange', function(state) {
  if (state === 'poweredOn') {
    noble.startScanning([], false);
  } else {
    noble.stopScanning();
  }
});

noble.on('discover', function(peripheral) {
  peripheral.connect();
  peripheral.on('connect', function() {
    console.log('Hello ' + peripheral.advertisement.localName);
    peripheral.disconnect(); // change 1
  });
  peripheral.on('disconnect', function() { // change 2
    console.log('Goodbye ' + peripheral.advertisement.localName);
  });
});

@facultymatt
Copy link
Author

Ahh, good catch, cut and paste error. In my code I'm using peripheral.on('disconnect').

If I call peripheral.disconnect(); then it works as expected. However I'm attempting to trigger the disconnect on the peripheral side, by turning it off. Seems this doesn't cause the disconnect event to be fired as expected.

Thoughts?

@sandeepmistry
Copy link
Collaborator

Which peripheral are you using?

I haven't tried turning a device off while connected. On OS X blued/CoreBluetooth is responsible for the disconnect event - maybe you just have to wait a while?

@sandeepmistry
Copy link
Collaborator

Closing due to inactivity, @facultymatt re-open if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants