2013-09-30 14:50:56 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2013-09-30 14:50:56 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
//import noble
|
|
|
|
var noble = require('noble');
|
2013-09-30 14:50:56 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
// The main node definition - most things happen in here
|
|
|
|
function BleScan(n) {
|
|
|
|
// Create a RED node
|
|
|
|
RED.nodes.createNode(this,n);
|
2013-09-30 14:50:56 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
var msg = {};
|
|
|
|
var ble_name;
|
|
|
|
var node = this;
|
2013-10-26 23:19:43 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
//get name and uuid from user
|
|
|
|
this.ble_name = n.ble_name;
|
|
|
|
this.ble_uuid = n.ble_uuid;
|
2013-09-30 14:50:56 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
this.on("input", function(msg) {
|
|
|
|
noble.startScanning();
|
|
|
|
});
|
2014-07-01 00:41:38 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
noble.on('scanStart', function(msg) {
|
|
|
|
msg = {};
|
|
|
|
msg.topic = node.topic;
|
|
|
|
msg.payload = "Scanning initiated..." //debugging
|
|
|
|
//console.log('scanning initiated...');
|
|
|
|
node.send(msg);
|
|
|
|
});
|
2013-09-30 14:50:56 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
noble.on('discover', function(peripheral) {
|
|
|
|
var msg = {};
|
|
|
|
msg.topic = node.topic;
|
|
|
|
msg.payload = "not found";
|
2013-10-26 23:19:43 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
//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);
|
|
|
|
});
|
2013-09-30 14:50:56 +02:00
|
|
|
|
2016-04-09 19:00:14 +02:00
|
|
|
this.on("close", function() {
|
|
|
|
try { noble.stopScanning(); }
|
|
|
|
catch (err) { console.log(err); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Register the node by name. This must be called before overriding any of the
|
|
|
|
// Node functions.
|
|
|
|
RED.nodes.registerType("scanBLE", BleScan);
|
2013-09-30 14:50:56 +02:00
|
|
|
}
|