function Imuduino(peripheral_id, packet_type) { if (!(this instanceof Imuduino)) { return new Imuduino(peripheral_id) } peripheral_id = peripheral_id || service_info.peripheralUUID packet_type = (packet_type && parser_mappings[packet_type]) || DEFAULT_PARSER console.log(packet_type) this.parser = require(packet_type) var self = this this.buffer = [] noble.on('discover', function (peripheral) { console.log('...Discoverd peripheral UUID:', peripheral.uuid, peripheral.advertisement.localName); if (peripheral.uuid == peripheral_id) { self.emit('peripheralDiscovered', peripheral) self.connect(peripheral) noble.stopScanning(); } }) noble.on('stateChange', function (state) { if (state == 'poweredOn') { noble.startScanning() } }) }
BlePeripherals.prototype._registerListeners = function(){ var that = this; noble.on('stateChange', function(state){ that._handleStateChanges(state); }); noble.on('discover', function(peripheral){ that._handlePeripheralDiscovery(peripheral); }); };
function BTProximity(data) { this.mac = data.mac; this.name = data.name; noble.on('stateChange', this.handleStateChange.bind(this)); noble.on('discover', this.handleDiscover.bind(this)); setInterval(this.checkAway.bind(this), 5000); }
MyoDiscoveryAgent.prototype.discover = function() { debug('INFO: Beginning Myo discovery'); // Attach listeners // noble.on('discover', _handleNobleDiscover.bind(this)); noble.on('stateChange', _handleNobleStateChange.bind(this)); };
this.start = function(){ noble.on('stateChange', _self.ddbstateChange); noble.on('discover', _self.ddbDiscover); //cron 処理を開始 _self.job.start(); //json-serverを初期化 _self.initJsonServer(); }
function init() { if(initDone) {return false;} //else {initDone = true;} noble.on('scanStop', function() { setTimeout( function() { console.log("restart BLE scanning"); noble.startScanning(); }, 1000 ); }); // Start scan console.log("noble.state ===", noble.state); if(noble.state === "poweredOn") { console.log("BLE start scanning"); noble.startScanning( ); initDone = true; } else { noble.on('stateChange', function(state) { console.log("\tnoble state:", state); switch(state) { case 'poweredOn': console.log("BLE start scanning"); noble.startScanning(); initDone = true; break; default: } return true; }); } // When a device is discovered... if(initDone) { noble.on( 'discover' , function(peripheral) { var i, nobleType, brickType, object , name = peripheral.advertisement?peripheral.advertisement.localName:peripheral.address , brick ; console.log("Discover BLE", peripheral.id, name); for(i=0; i<L_types.length; i++) { nobleType = L_types[i].nobleType; brickType = L_types[i].brickType; if(nobleType.is(peripheral)) { if(nobleType !== brickType) { object = new nobleType(peripheral); } else {object = peripheral;} brick = new brickType(peripheral.id, object); } } if(!brick) {brick = new BrickBLE(peripheral.id, peripheral);} for(i=0; i<L_CB_Discover.length; i++) {L_CB_Discover[i].apply(brick);} }); } return true; }
TicklsHome.prototype.init = function() { logger.debug("TicklsHome init"); noble.on('stateChange', function(state) { logger.debug("Noble changed state: " + state); if(state == 'poweredOn') { noble.startScanning([], true); } else if(state == 'poweredOff') { noble.stopScanning(); } }); noble.on('scanStart', function() { logger.debug("Noble scan started"); }); noble.on('scanStop', function() { logger.warn("Noble scan stopped"); }); noble.on('discover', function(peripheral) { peripheral.on('rssiUpdate', function(rssi) { logger.debug("RSSI update: " + JSON.stringify(rssi)); // setTimeout(function() { // peripheral.updateRssi(); // }, 500); }); peripheral.connect(function(err) { /* if(!err) { logger.info("Connected to peripheral"); peripheral.updateRssi(); } else { logger.error("Could not connect: " + error); } */ }); if(peripheral.advertisement !== undefined && peripheral.advertisement.serviceUuids !== undefined) { logger.debug("rssi: " + peripheral.rssi); // peripheral.advertisement.serviceUuids.forEach(function (serviceUuid) { // logger.debug(" Peripheral service UUID: " + serviceUuid); // }); } }); }
BlueYeast.prototype._startScan = function() { if (this._isScanning) { return; } this._isScanning = true; if (noble.state == 'poweredOn') { noble.on('discover', this._handleDevicefound.bind(this)); noble.startScanning(); } else { noble.on('stateChange', this._handleStatechanged.bind(this)); } };
listen : (_opts) => { opts = Object.assign(opts ,defaultOpts, _opts || {}); noble.on('discover', function(peripheral) { // Match discovered characteristics and name or address against // provided options. If opts.localName is empty, match against // all named devices let mdata = peripheral.advertisement.manufacturerData; if (!mdata) return; opts.devices.some((dev) => { if (dev.manufacturer_data === mdata.toString("hex")){ peripheral.connect(function(err) { // Discover services and characteristics peripheral.discoverSomeServicesAndCharacteristics( [dev.service_uuid], [dev.transmit_characteristic_uuid,dev.receive_characteristic_uuid], function(err, services, characteristics){ var transmit, receive; characteristics.forEach(function(characteristic) { if (compareUUIDs(dev.transmit_characteristic_uuid, characteristic.uuid)) { transmit = characteristic; } else if (compareUUIDs(dev.receive_characteristic_uuid, characteristic.uuid)) { receive = characteristic; } }) if (transmit && receive){ Mesh.frames(new BLEStream(peripheral, transmit, receive), FRAME_SIZE); } else { peripheral.disconnect(); } } ) }); // connect return true; } else { return false; } }) }); // on discover // set up scanning; if (_nobleOn) noble.startScanning(); else noble.on('stateChange', (state) => state === "poweredOn" ? noble.startScanning() : null) return Promise.resolve(() => noble.stopScanning()) }
this.discover = function () { noble.on('stateChange', function (state) { if (state === 'poweredOn') { noble.startScanning(); } else { noble.stopScanning(); } }); noble.on('discover', function (peripheral) { peripheral.connect(function(error) { console.log('connected to peripheral: ' + peripheral.uuid); }); }) };
getmac.getMac(function (err, mac_address) { if (err) { console.log('Could not get MAC address.'); console.log(err); } else { console.log('Found mac address: ' + mac_address); noble.on('discover', function (peripheral) { manufac_data = ''; console.log('peripheral discovered (' + peripheral.uuid.match(/../g).join(':') + '):'); console.log('\thello my local name is: ' + peripheral.advertisement.localName); console.log('\t\t' + peripheral.rssi); if (peripheral.advertisement.manufacturerData) { manufac_data = peripheral.advertisement.manufacturerData.toString('hex'); } blob = { location_str: 'unknown', ble_addr: peripheral.uuid.match(/../g).join(':'), name: peripheral.advertisement.localName, scanner_macAddr: mac_address.toUpperCase(), service_uuids: peripheral.advertisement.serviceUuids, manufacturer_data: manufac_data, rssi: peripheral.rssi, time: Date.now()/1000, } // Publish advertisement to RabbitMQ xch.publish('scanner.bleScanner.'+mac_address.toUpperCase(), blob); }); } });
onboard: function(name, advertisementLocalNameFilter, successCallback) { console.log('Onboarding device : ' + name); console.log('advertisementLocalNameFilter : ' + advertisementLocalNameFilter); var advertisementLocalNameRegEx = new RegExp(advertisementLocalNameFilter); noble.on('stateChange', function() { noble.on('discover', function(peripheral) { console.log('found peripheral: ' + peripheral); if ((peripheral.advertisement != null) && (peripheral.advertisement.localName != null) && advertisementLocalNameRegEx.test(peripheral.advertisement.localName)) { noble.stopScanning(); if (successCallback) { successCallback(peripheral.id, 'All done. Happy coding!'); } return; } }); noble.startScanning(); }); }
module.exports = function (timeout, serviceUuid, peripherals, done) { noble.on('discover', discover.bind({peripherals:peripherals})); noble.startScanning([serviceUuid]); console.log('Scanning for BLE devices...'); setTimeout(stopandreturn.bind({done:done, peripherals:peripherals}), timeout); };
var Bleacon = function() { this._uuid = null; this._major = null; this._minor = null; noble.on('discover', this.onDiscover.bind(this)); };
BlueYeast.prototype._handleStatechanged = function() { if (noble.state == 'poweredOn') { noble.startScanning(); noble.on('discover', this._handleDevicefound.bind(this)); //noble.removeEventListener('stateChange', this._handleStatechanged); } };
Proxy.prototype.acquireTarget = function(config) { console.log(('[status] Acquiring target ' + this.target).bold); /* Track BLE advertisement reports. */ if (this.le_adv_handler != null) noble._bindings._gap._hci.removeListener( 'leAdvertisingReport', this.le_adv_handler ) this.le_adv_handler = function(status, type, address, addressType, report, rssi){ this.discoverDeviceAdv(address, report, rssi); }.bind(this); noble._bindings._gap._hci.on( 'leAdvertisingReport', this.le_adv_handler ); /* noble._bindings._gap._hci.on( 'leAdvertisingReport', function(status, type, address, addressType, report, rssi){ this.discoverDeviceAdv(address, report, rssi); }.bind(this)); */ /* Track BLE advertisement reports. */ noble.on('discover', function(peripheral){ if (peripheral.address.toLowerCase() === this.target.toLowerCase()) { noble.stopScanning(); this.connectDevice(peripheral, config); } }.bind(this) ); /* Start scanning when ble device is ready. */ noble.startScanning(); };
function BrickBLE_server(id) { var server = this; Brick.apply(this, [ id ]); this.BLE_server = { bricks : [], scanning : false, continuousScan : false, state : noble.state }; noble.on('scanStart', function() { server.BLE_server.scanning = true; server.emit("update_BrickBLE_sever", {scanning: server.BLE_server.scanning} ); }); noble.on('scanStop', function() { if(server.BLE_server.continuousScan && server.BLE_server.scanning) { noble.startScanning(); } else { server.BLE_server.scanning = false; server.emit("update_BrickBLE_sever", {scanning: server.BLE_server.scanning} ); } }); noble.on('stateChange', function(state) { server.emit("update_BrickBLE_sever", {state: server.BLE_server.state = state}) if(state === "poweredOn") {noble.startScanning();} }); noble.on( 'discover', function(peripheral) { var i, nobleType, brickType, object , name = peripheral.advertisement?peripheral.advertisement.localName:peripheral.address , brick ; console.log("Discover BLE", peripheral.id, name); for(i=0; i<L_types.length; i++) { nobleType = L_types[i].nobleType; brickType = L_types[i].brickType; if(nobleType.is(peripheral)) { if(nobleType !== brickType) { object = new nobleType(peripheral); } else {object = peripheral;} brick = new brickType(peripheral.id, object); } } if(!brick) {brick = new BrickBLE(peripheral.id, peripheral);} server.BLE_server.bricks.push( brick ); server.emit("update_BrickBLE_sever", {bricks: server.getDescription().BLE_server.bricks}); }); }
MagicBlueBulb.prototype.findBulb = function(mac, callback) { var that = this; noble.on('stateChange', function(state) { if (state === 'poweredOn') { noble.startScanning(); } else { noble.stopScanning(); } }); noble.on('discover', function(peripheral) { if (peripheral.id === mac || peripheral.address === mac) { that.log("found my bulb"); that.peripheral = peripheral; } }); };
function onDisconnect() { beacon.removeListener('disconnect', onDisconnect); console.log('disconnected!'); noble.on('discover', onDiscover); noble.startScanning(); }
var get = module.exports = function () { var events = new EventEmitter(); noble.on('stateChange', function(state) { if (state === ON_STATE) { console.log("Starting scan for BLE devices"); noble.startScanning(); } else { console.log("Stop scanning for devices"); noble.stopScanning(); } }); noble.on('discover', function (peripheral) { events.emit('data', peripheral); }); return events; };
//スキャン開始 function start () { //スキャン開始 noble.startScanning(); //見つかった時の処理 noble.on('discover', function(peripheral) { num++; console.log(num,"uuid:"+peripheral.uuid,peripheral.advertisement.localName); }); }
it('should should handle initialization - powered on', function(done) { var callback = function(state) { noble.removeListener('stateChange', callback); assert(controller.startScanning.calledOnce, 'Did not receive BT state change on initialization.'); done(); }; noble.on('stateChange', callback); noble.emit('stateChange', 'poweredOn'); });
function BT() { noble.on('stateChange', function(state) { if (state === 'poweredOn') { noble.startScanning(); } else { noble.stopScanning(); } }); };
return new Promise((resolve, reject) => { console.info('Scanning for : ', deviceClass.SCAN_UUIDS); noble.on('stateChange', function(state) { if (state === 'poweredOn') { noble.startScanning(deviceClass.SCAN_UUIDS | [], this.SCAN_DUPLICATES); } else { noble.stopScanning(); reject('bluetooth off?'); } }); noble.on('discover', (peripheral) => { resolve(new deviceClass(peripheral)); }); });
app.listen(0, function() { var peripheral = null; console.log(new Date(), 'Serving http://localhost:' + this.address().port); // Advertise over mDNS mdns.createAdvertisement(mdns.tcp('http'), this.address().port,{name:'Robosmart'}).start(); app.post('/state', function(req, res) { peripheral.connect(function(){ peripheral.discoverSomeServicesAndCharacteristics(["ff10"],["ff11"], function(error, services, characteristics) { characteristics[0].read(function(error,data){ if (req) res.send(data[0]==1); peripheral.disconnect(); }) }); }); }); app.post('/toggle', function(req, res) { peripheral.connect(function(){ peripheral.discoverSomeServicesAndCharacteristics(["ff10"],["ff11"], function(error, services, characteristics) { characteristics[0].read(function(error,data){ characteristics[0].write(new Buffer([!data[0]]),false,function(e){ if (req) res.send(!data[0]); peripheral.disconnect(); }); }); }); }); }); noble.on('stateChange',function(state){ if (state=="poweredOn") noble.startScanning(["ff10"]); }); noble.on('discover', function(p){ if (p.advertisement.localName && p.advertisement.localName.startsWith("SHL")) { peripheral = p; noble.stopScanning(); console.log("Found device.") } }); });
this.start= function(delay){ var time=delay if (delay>0){ timer=setTimeout(this.stop,1000*time) } noble.startScanning() console.log ('DEBUG - Scan.js: *****Scanning...for '+ time + ' seconds.******') self.emit('scanning') noble.on('discover', btListener) }
// The main node definition - most things happen in here function BleScan(n) { // Create a RED node RED.nodes.createNode(this,n); var msg = {}; var ble_name; var node = this; //get name and uuid from user this.ble_name = n.ble_name; this.ble_uuid = n.ble_uuid; this.on("input", function(msg) { noble.startScanning(); }); noble.on('scanStart', function(msg) { msg = {}; msg.topic = node.topic; msg.payload = "Scanning initiated..." //debugging //console.log('scanning initiated...'); node.send(msg); }); noble.on('discover', function(peripheral) { var msg = {}; msg.topic = node.topic; msg.payload = "not found"; //check for the device name and the UUID (first one from the UUID list) if (peripheral.advertisement.localName==node.ble_name && peripheral.advertisement.serviceUuids[0]==node.ble_uuid) { msg.payload=peripheral.advertisement.localName; noble.stopScanning(); } node.send(msg); }); this.on("close", function() { try { noble.stopScanning(); } catch (err) { console.log(err); } }); }
it('does not stop scanning when bb8 device not discovered', function(done) { sinon.stub(controller, "isBB8Peripheral").returns(false); var spy = sinon.spy(controller, 'stopScanning'); controller.startScanning(); noble.on('discover', function(peripheral) { assert.isTrue(controller._isScanning, 'Should still be scanning'); assert.isFalse(spy.called, 'Scanning was not stopped after discovery'); done(); }); noble.emit('discover', {}); });
function (callback) { noble.on('discover', discoverListener); noble.startScanning([], true); //duplicate scan winston.log('info','Scan started!'); that.startTime = Date.now(); that.state = 1; callback(null, 'Scan started!'); }],
this.startScanning = function() { noble.on('stateChange', function(state) { if (state === 'poweredOn') { noble.startScanning([], true); powered = true; } else { console.log('stop scanning'); noble.stopScanning(); } }); },