-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.js
65 lines (55 loc) · 1.3 KB
/
scan.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
const noble = require("noble")
let activeReadWrites = 0;
const timeout = ms => new Promise(res => setTimeout(res, ms))
async function delay()
{
await timeout(100);
}
async function waitForEvents()
{
while (activeReadWrites > 0)
{
await delay();
}
}
noble.on('stateChange', state => {
if (state === 'poweredOn')
{
console.log('Scanning');
noble.startScanning();
}
else
{
console.log("State: " + state);
noble.stopScanning();
process.exit();
}
});
function foundChars(error, services, characteristics)
{
console.log("error", error);
console.log(services);
console.log(characteristics);
}
noble.on('discover', discovered);
function discovered(device)
{
console.log("Discovered Device: ", device.address);
// if (device.address === "dd:82:10:ff:52:5e")
if (typeof device.advertisement.localName === "undefined")
{
return;
}
if (device.advertisement.localName.indexOf("micro:bit") < 0)
{
console.log(device.advertisement.localName);
return;
}
if (device.advertisement.localName.indexOf("micro:bit") >= 0)
{
console.log(device.advertisement.localName);
console.log("RSSI: ", device.rssi);
noble.stopScanning();
process.exit();
}
}