-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
105 lines (91 loc) · 3.74 KB
/
index.html
File metadata and controls
105 lines (91 loc) · 3.74 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live Heart Rate</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<h2>Live Heart Rate Dashboard</h2>
<button onclick="connectToHeartRateMonitor()">Connect</button>
<p id="heartRateDisplay">Heart Rate: -- BPM</p>
<p id="batteryDisplay">Battery: --%</p>
<canvas id="heartRateChart"></canvas>
<script>
let chart;
let heartRateData = [];
let timeLabels = [];
async function getBatteryLevel(server) {
try {
const service = await server.getPrimaryService('battery_service');
const characteristic = await service.getCharacteristic('battery_level');
const value = await characteristic.readValue();
const batteryLevel = value.getUint8(0);
document.getElementById('batteryDisplay').innerText = `Battery: ${batteryLevel}%`;
} catch (error) {
console.error("Error reading battery:", error);
}
}
async function connectToHeartRateMonitor() {
try {
const device = await navigator.bluetooth.requestDevice({
filters: [{ services: ['heart_rate'] }],
optionalServices: ['battery_service']
});
const server = await device.gatt.connect();
const service = await server.getPrimaryService('heart_rate');
const characteristic = await service.getCharacteristic('heart_rate_measurement');
characteristic.addEventListener('characteristicvaluechanged', handleHeartRateMeasurement);
await characteristic.startNotifications();
getBatteryLevel(server);
} catch (error) {
console.error("Connection error:", error);
}
}
function handleHeartRateMeasurement(event) {
const value = event.target.value;
const heartRate = parseHeartRate(value);
document.getElementById('heartRateDisplay').innerText = `Heart Rate: ${heartRate} BPM`;
const now = new Date().toLocaleTimeString();
heartRateData.push(heartRate);
timeLabels.push(now);
if (heartRateData.length > 20) {
heartRateData.shift();
timeLabels.shift();
}
chart.update();
}
function parseHeartRate(value) {
let data = new DataView(value.buffer);
let flags = data.getUint8(0);
let rate16Bits = flags & 0x1;
return rate16Bits ? data.getUint16(1, true) : data.getUint8(1);
}
document.addEventListener("DOMContentLoaded", function () {
let ctx = document.getElementById('heartRateChart').getContext('2d');
chart = new Chart(ctx, {
type: 'line',
data: {
labels: timeLabels,
datasets: [{
label: 'Heart Rate (BPM)',
data: heartRateData,
borderColor: 'red',
backgroundColor: 'rgba(255, 0, 0, 0.2)',
fill: true
}]
},
options: {
responsive: true,
animation: false,
scales: {
x: { title: { display: true, text: 'Time' } },
y: { title: { display: true, text: 'BPM' }, min: 40, max: 200 }
}
}
});
});
</script>
</body>
</html>