-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsticknfind.js
43 lines (33 loc) · 1.12 KB
/
sticknfind.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
var noble = require('noble');
console.log('Note: You\'ll probably have to flick the thing four times to make it work...');
noble.on('stateChange', function(state) {
console.log('state change', state);
if (state === 'poweredOn') {
noble.startScanning();
} else {
noble.stopScanning();
}
});
noble.on('discover', function(peripheral) {
console.log('on -> discover: ' + peripheral);
peripheral.on('connect', function() {
console.log('on -> connect');
this.discoverServices();
});
peripheral.on('servicesDiscover', function(services) {
console.log('on -> peripheral services discovered ',services);
services.forEach(function(service) {
if (service.type == 'org.bluetooth.service.immediate_alert') {
service.on('characteristicsDiscover', function(characteristics) {
characteristics[0].on('write', function() {
console.log('on -> characteristic write ');
peripheral.disconnect();
});
characteristics[0].write(new Buffer('3'));
});
service.discoverCharacteristics();
}
});
});
peripheral.connect();
});