-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathheartRate.details.js
128 lines (111 loc) · 4 KB
/
heartRate.details.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var heartRate_Characteristic = null;
function heartRate_start(readEvent) {
let serviceUuid = "heart_rate";
let characteristicUuid = "heart_rate_measurement"
log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice
({
filters: [{ services: [serviceUuid] }]
})
.then(device => { return device.gatt.connect(); })
.then(server => { return server.getPrimaryService(serviceUuid); })
// .then(service => { return service.getCharacteristic(characteristicUuid); })
// .then(characteristic => {
// heartRate_Characteristic = characteristic;
// return heartRate_Characteristic.startNotifications().then(_ => {
// heartRate_Characteristic.addEventListener(
// 'characteristicvaluechanged',
// hearRate_read);
// });
// })
.then(service => {
chosenHeartRateService = service;
return Promise.all([
service.getCharacteristic('body_sensor_location')
.then(handleBodySensorLocationCharacteristic),
service.getCharacteristic('heart_rate_measurement')
.then(handleHeartRateMeasurementCharacteristic),
])
})
.catch(error => {
log('Argh! ' + error);
});
}
function hearRate_read(event) {
let value = event.target.value;
var bpm = value.getUint8(1);
log(bpm.toString().padStart(3) + '|' + '-'.repeat(bpm) + '>');
}
function handleBodySensorLocationCharacteristic(characteristic) {
if (characteristic === null) {
log("Unknown sensor location.");
return Promise.resolve();
}
return characteristic.readValue()
.then(sensorLocationData => {
let sensorLocation = sensorLocationData.getUint8(0);
var list = ['Other', 'Chest', 'Wrist', 'Finger', 'Hand', 'Ear Lobe', 'Foot'];
var location = list[sensorLocation];
log('Sensor Location: ' + location);
});
}
function handleHeartRateMeasurementCharacteristic(characteristic) {
return characteristic.startNotifications()
.then(char => {
characteristic.addEventListener('characteristicvaluechanged',
onHeartRateChanged);
});
}
function onHeartRateChanged(event) {
let characteristic = event.target;
log(parseHeartRate(characteristic.value));
}
function parseHeartRate(data) {
let flags = data.getUint8(0);
let rate16Bits = flags & 0x1;
let result = {};
let index = 1;
if (rate16Bits) {
result.heartRate = data.getUint16(index, /*littleEndian=*/true);
index += 2;
} else {
result.heartRate = data.getUint8(index);
index += 1;
}
let contactDetected = flags & 0x2;
let contactSensorPresent = flags & 0x4;
if (contactSensorPresent) {
result.contactDetected = !!contactDetected;
}
let energyPresent = flags & 0x8;
if (energyPresent) {
result.energyExpended = data.getUint16(index, /*littleEndian=*/true);
index += 2;
}
let rrIntervalPresent = flags & 0x10;
if (rrIntervalPresent) {
let rrIntervals = [];
for (; index + 1 < data.byteLength; index += 2) {
rrIntervals.push(data.getUint16(index, /*littleEndian=*/true));
}
result.rrIntervals = rrIntervals;
}
log("heartRate =" + result.heartRate);
log("contactDetected =" + result.contactDetected);
log("energyExpended =" + result.energyExpended);
log("rrIntervals =" + result.rrIntervals);
return result;
}
function heartRate_stop() {
if (heartRate_Characteristic) {
heartRate_Characteristic.stopNotifications()
.then(_ => {
log('> Notifications stopped');
heartRate_Characteristic.removeEventListener('characteristicvaluechanged',
hearRate_read);
})
.catch(error => {
log('Argh! ' + error);
});
}
}