-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
71 lines (63 loc) · 1.57 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
// Local vars
var port = 8080;
var timeout = 5000;
var pollFreq = 5000;
var noneFound;
var devices = [];
// Use this to add names to a specific device IDs
var recognizedDevices = {
'105.105.60.123.235.203': 'Myo',
'70.10.117.62.49.92': 'August'
};
// Set up server
var router = require('tiny-router');
router.get('/', function (req, res) {
if (devices.length > 0) {
var page = "<html><body>Devices in view:<br/><br/>";
for (var device in devices) {
var deviceID = devices[device].id;
page += deviceID;
if (deviceID in recognizedDevices) {
page += " " + recognizedDevices[deviceID];
}
page += "<br/>";
}
page += "</body></html>";
res.send(page);
} else {
res.send('No devices in range.');
}
});
// Start server
router.listen(port);
console.log('Listening on port', port);
// Set up hardware
var tessel = require('tessel');
var blelib = require('ble-ble113a');
var ble = blelib.use(tessel.port['A']);
ble.on('ready', function () {
// Initial scan for devices
scan();
});
// When a device is discovered
ble.on('discover', function(peripheral) {
deviceID = peripheral.address._str;
console.log('Found device:', deviceID);
devices.push({id: deviceID});
});
// Scan for devices regularly
function poll() {
setTimeout(scan, pollFreq);
}
// Check and see if authed devices in range
function scan () {
// Reset found devices
devices = [];
console.log('Scanning...');
ble.startScanning();
noneFound = setTimeout(function () {
ble.stopScanning();
// Check for changes
poll();
}, timeout);
}