-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
110 lines (89 loc) · 4.03 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bluetooth Serial Communication</title>
</head>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<body>
<button>Search</button>
<div id="receivedData"></div>
<div id="myChart" style="width:100%; height:600px;"></div>
<script>
google.charts.load('current', { packages: ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
let chartData = [
['Blood Pressure', 'Seconds']
];
const options = {
title: 'Heart Rate Monitor',
hAxis: { title: 'Seconds' },
vAxis: { title: 'Blood Pressure' },
legend: 'none'
};
function drawChart() {
const chart = new google.visualization.LineChart(document.getElementById('myChart'));
chart.draw(google.visualization.arrayToDataTable(chartData), options);
}
var seconds = 0;
var NORDIC_SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
var NORDIC_RX = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";
button = document.querySelector('button');
button.addEventListener('pointerup', function (event) {
navigator.bluetooth.requestDevice({
filters: [{ services: [NORDIC_SERVICE] }],
acceptAllDevices: false // Set to false to apply filters
})
.then(device => {
// Human-readable name of the device.
console.log(device.name);
document.querySelector("button").style.display = "none";
// Attempts to connect to remote GATT Server.
return device.gatt.connect();
})
.then(server => {
// Once connected to the server, you can get the primary service(s)
// and characteristic(s) to listen for data.
return server.getPrimaryService(NORDIC_SERVICE);
})
.then(service => {
// Get the characteristic you want to listen to.
return service.getCharacteristic(NORDIC_RX);
})
.then(characteristic => {
// Set up event listener for characteristic value changes.
characteristic.addEventListener('characteristicvaluechanged', event => {
// Handle incoming data here.
var value = event.target.value;
var stringValue = new TextDecoder().decode(value);
if (stringValue.includes("BPM:")) {
var bpmRegex = /BPM:\s*(\d+)/;
var match = stringValue.match(bpmRegex);
if (match && match.length > 1) {
var bpmValue = match[1];
console.log("BPM value:", bpmValue);
chartData.push([parseFloat(seconds), parseFloat(bpmValue)]);
drawChart();
if(bpmValue > 100){
document.querySelector("body").style.background = "red";
}
else{
document.querySelector("body").style.background = "white";
}
seconds++;
} else {
console.log("BPM value not found.");
}
}
});
// Start notifications for the characteristic to enable listening.
return characteristic.startNotifications();
})
.catch(error => {
console.error(error);
});
});
</script>
</body>
</html>