Repository navigation
Expand file tree
/
Copy pathHeartRateMonitor.js
More file actions
81 lines (70 loc) · 2.24 KB
/
Copy pathHeartRateMonitor.js
File metadata and controls
81 lines (70 loc) · 2.24 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
class HeartRateMonitor {
constructor() {
this.SERVICE_ID = 0x180D;
this.CHARACTERISTIC_ID = 0x2A37;
this.hrElement_ = document.getElementById('hr');
this.avgElement_ = document.getElementById('avg');
this.resetAverage_();
}
computeAverage_() {
if (this.timeSum_ > 0) {
let avg = this.hrSum_ / ((this.timeSum_) * 2);
return avg.toFixed(1);
}
return '0.0';
}
resetAverage_() {
this.lastTick_ = 0;
this.lastHr_ = 0;
this.hrSum_ = 0;
this.timeSum_ = 0;
}
parseHeartRate_(data) {
let flags = data.getUint8(0);
if (flags & 0x1) {
return data.getUint16(1, true);
}
return data.getUint8(1);
}
onHeartRateChanged_(event) {
console.log(event);
let dataView = event.target.value;
let tick = (new Date()).getTime();
let hr = this.parseHeartRate_(dataView);
// Ignore readings where the HR or last HR value is 0 - treat this as a
// failed reading from the sensor.
if (this.lastTick_ && hr && this.lastHr_) {
this.hrSum_ += (tick - this.lastTick_) * (hr + this.lastHr_);
this.timeSum_ += tick - this.lastTick_;
}
this.lastTick_ = tick;
this.lastHr_ = hr;
this.hrElement_.textContent = hr;
this.avgElement_.textContent = this.computeAverage_();
}
handleCharacteristic_(characteristic) {
characteristic.addEventListener('characteristicvaluechanged',
event => this.onHeartRateChanged_(event));
return characteristic.startNotifications();
}
start() {
this.resetAverage_();
let options = {filters: [{
services: [this.SERVICE_ID]
}]};
navigator.bluetooth.requestDevice(options)
.then(device => {
return device.gatt.connect();
})
.then(server => {
return server.getPrimaryService(this.SERVICE_ID);
})
.then(service => {
return service.getCharacteristic(this.CHARACTERISTIC_ID);
})
.then(characteristic => this.handleCharacteristic_(characteristic))
.catch(error => {
console.log('Error: ' + error);
});
}
}