-
Notifications
You must be signed in to change notification settings - Fork 25
/
adapter.js
48 lines (41 loc) · 937 Bytes
/
adapter.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
const noble = require('@abandonware/noble');
const EventEmitter = require('events').EventEmitter;
class Adapter extends EventEmitter {
constructor () {
super();
noble.on('discover', (peripheral) => {
this.emit('discover', peripheral);
});
noble.on('warning', (warning) => {
this.emit('warning', warning);
});
// start scanning
if (noble.state === 'poweredOn') {
this.start();
} else {
noble.once('stateChange', (state) => {
if (state === 'poweredOn') {
this.start();
} else {
this.stop();
}
this.emit('stateChange', state);
});
}
}
start () {
if (this._scanning) {
return;
}
this._scanning = true;
noble.startScanning([], true);
}
stop () {
if (!this._scanning) {
return;
}
this._scanning = false;
noble.stopScanning();
}
}
module.exports = new Adapter();