-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
85 lines (72 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Generated by CoffeeScript 1.6.3
var BleScanner, spawn;
spawn = require('child_process').spawn;
BleScanner = (function() {
var filterHciDump, hciconfig, hcidump, hcitool, init, instance;
instance = void 0;
hciconfig = {};
hcidump = {};
hcitool = {};
init = function(hcidev, callback) {
hciconfig = spawn('hciconfig', [hcidev, 'up']);
hcidump = spawn('hcidump', ['-R']);
return hciconfig.on("exit", function(code) {
var clearHciDump, clearHciTool;
if (code !== 0) {
return console.log("HCICONFIG: failed to bring up device " + hcidev);
} else {
console.log("HCICONFIG: succesfully brought up device " + hcidev);
clearHciDump = spawn("killall", ["hcidump"]);
clearHciTool = spawn("killall", ["hcitool"]);
clearHciTool.on("exit", function(code) {
console.log("HCITOOL: cleared (code " + code + ")");
hcitool = spawn('hcitool', ['lescan']);
return hcitool.on("exit", function(code) {
if (code === 1) {
return console.log("HCITOOL: exited, already running? (code 1)");
} else {
console.log("HCITOOL: exited (code " + code + ")");
return instance = void 0;
}
});
});
return clearHciDump.on("exit", function(code) {
console.log("HCIDUMP: cleared (code " + code + ")");
hcidump = spawn('hcidump', ['-R']);
hcidump.on("exit", function(code) {
console.log("HCIDUMP: exited (code " + code + ")");
return instance = void 0;
});
return hcidump.stdout.on('data', function(data) {
data = (data.slice(2)).toString('ascii').trim();
if (data.split(" ")[0] === "04") {
data = filterHciDump(data);
return callback(data);
}
});
});
}
});
};
BleScanner.prototype.destroy = function() {
try {
hcidump.kill();
return hcitool.kill();
} finally {
instance = void 0;
}
};
function BleScanner(hcidev, callback) {
if (!instance) {
instance = init(hcidev, callback);
}
instance;
}
filterHciDump = function(output) {
output = output.replace(/(\r\n|\n|\r)/gm, "");
output = output.replace(/\s+/g, " ");
return output = output.split(" ");
};
return BleScanner;
})();
module.exports = BleScanner;