sandeepmistry / node-sensortag

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

Passing in Instance of Noble #36

Closed dmelancon closed 9 years ago

dmelancon commented 9 years ago

I am working on a project where I am running a central that connects to multiple device types. Currently, I am running this library to handle the SensorTag and Noble to handle the other devices. Is there a way to pass in the same instance of Noble into the the Noble-SensorTag instance? or a way to discover the SensorTag while it is scanning and discovering the other devices. When they are both called it seems to only be able to scan and connect with the initial Noble instantiation.

sandeepmistry commented 9 years ago

@dmelancon yes there is, if you're sure it's a SensorTag, in the npm version:

var noble = require('noble');
var SensorTag  = require('sensortag');

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

noble.on('discover', function(peripheral) {
  var localName = peripheral.advertisement.localName;

  if ((localName === 'SensorTag') || (localName === 'TI BLE Sensor Tag')) {
     var sensortag = new SensorTag(peripheral);
     // ...
  }
});

version in master:

var noble = require('noble');
var CC2540SensorTag  = require('sensortag').CC2540;

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

noble.on('discover', function(peripheral) {
  var localName = peripheral.advertisement.localName;

  if ((localName === 'SensorTag') || (localName === 'TI BLE Sensor Tag')) {
     var sensortag = new CC2540SensorTag(peripheral);
     // ...
  }
});