-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
61 lines (59 loc) · 1.51 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
'use strict';
const noble = require('noble');
let lastvalue = '';
noble.on('stateChange', state => {
if (state === 'poweredOn') {
noble.startScanning(['181d'], true);
} else {
noble.stopScanning();
}
});
noble.on('discover', peripheral => {
let serviceData = peripheral.advertisement.serviceData,
unit = '',
newval = false,
measured;
if (serviceData && serviceData.length) {
for (let i in serviceData) {
if (serviceData.hasOwnProperty(i)) {
let data = serviceData[i].data.toString('hex');
switch (data.substr(0, 2)) {
case '03':
unit = 'lbs';
newval = true;
break;
case 'a3':
unit = 'lbs';
break;
case '12':
unit = 'jin';
newval = true;
break;
case 'b2':
unit = 'jin';
break;
case '22':
unit = 'kg';
newval = true;
break;
case 'a2':
unit = 'kg';
break;
default:
unit = '';
break;
}
measured = parseInt(data.slice(4, 6) + data.slice(2, 4), 16) * 0.01;
measured = unit === 'kg' ? measured / 2 : measured;
if (unit) {
if (lastvalue !== measured && !newval) {
console.log('Last measured: ' + measured + ' ' + unit);
} else if (newval && lastvalue !== measured) {
console.log('New measure: ' + measured + ' ' + unit);
}
lastvalue = measured;
}
}
}
}
});