-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto.js
68 lines (59 loc) · 2.18 KB
/
proto.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
66
67
68
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
var parseRawRuuvi = function(manufacturerDataString){
let robject = {};
let data = manufacturerDataString;
let dataFormat = data[0] & 0xFF;
let temperature = (data[1] << 8 | data[2] & 0xFF);
if (temperature > 32767) {
temperature -= 65535;
}
temperature /= 200.0;
let humidity = ((data[3] & 0xFF) << 8 | data[4] & 0xFF) / 400.0;
let pressure = ((data[5] & 0xFF) << 8 | data[6] & 0xFF) + 50000;
let accelerationX = (data[7] << 8 | data[8] & 0xFF) / 1000.0;
let accelerationY = (data[9] << 8 | data[10] & 0xFF) / 1000.0;
let accelerationZ = (data[11] << 8 | data[12] & 0xFF) / 1000.0;
let powerInfo = (data[13] & 0xFF) << 8 | data[14] & 0xFF;
let batteryVoltage = (powerInfo >>> 5) / 1000.0 + 1.6;
let txPower = (powerInfo & 0b11111) * 2 - 40;
let movementCounter = data[15] & 0xFF;
let measurementSequenceNumber = (data[16] & 0xFF) << 8 | data[17] & 0xFF;
// let mac = toHexString(data.subarray(18));
robject.destination_endpoint = dataFormat;
robject.temperature = temperature;
robject.humidity = humidity;
robject.pressure = pressure;
robject.accelerationX = accelerationX;
robject.accelerationY = accelerationY;
robject.accelerationZ = accelerationZ;
robject.batteryVoltage = batteryVoltage;
robject.txPower = txPower;
robject.movementCounter = movementCounter;
robject.measurementSequenceNumber = measurementSequenceNumber;
// robject.mac = mac;
return robject;
}
navigator.bluetooth.requestLEScan(
{acceptAllAdvertisements: true, keepRepeatedDevices: true}
).then(() => {
navigator.bluetooth.addEventListener('advertisementreceived', event => {
let value = event.manufacturerData.get(0x0499);
if (!value) {
return;
}
let dataFormat = value.getUint8(0);
if (dataFormat != 5) {
return;
}
let a = [];
for (let i = 0; i < value.byteLength; i++) {
a.push('0x' + ('00' + value.getUint8(i).toString(16)).slice(-2));
}
console.log(a.join(' '));
console.log(parseRawRuuvi(a));
});
})