-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
40 lines (36 loc) · 970 Bytes
/
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
'use strict';
let noble = require('noble'),
chalk = require('chalk'),
urldecode = require('./urldecode.js'),
metadata = require('./metadata.js');
noble.on('stateChange', state => {
if (state === 'poweredOn') {
noble.startScanning(['feaa']);
} else {
noble.stopScanning();
}
});
noble.on('scanStart', () => {
console.log(chalk.dim('Scan started...'));
console.log();
});
noble.on('scanStop', () => {
console.log(chalk.dim('Scan stopped...'));
console.log();
});
noble.on('discover', peripheral => {
let serviceData = peripheral.advertisement.serviceData;
if (serviceData && serviceData.length) {
let objects = [];
for (let i in serviceData) {
// check if Eddystone-URL
if (serviceData[i].data.toString('hex').substr(0, 2) === '10') {
let url = urldecode(serviceData[i].data.toString('hex'));
objects.push({'url': url});
}
}
if (objects.length) {
metadata(objects);
}
}
});