refactor heart rate measurement logic for improved data handling and clarity
Browse files- index.html +64 -40
index.html
CHANGED
|
@@ -119,67 +119,91 @@
|
|
| 119 |
// ================================
|
| 120 |
// ĐỌC DỮ LIỆU NHỊP TIM CHUẨN BLE
|
| 121 |
// ================================
|
| 122 |
-
function
|
| 123 |
const value = event.target.value;
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
let newBpm = 0;
|
| 129 |
-
if (format16bit) {
|
| 130 |
-
newBpm = value.getUint16(1, true); // Đọc 16-bit nếu cờ báo hiệu
|
| 131 |
-
} else {
|
| 132 |
-
newBpm = value.getUint8(1); // Đọc 8-bit chuẩn
|
| 133 |
}
|
| 134 |
|
| 135 |
-
console.log(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
currentBpm = smoothBpm(newBpm);
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
}
|
| 145 |
|
| 146 |
// ================================
|
| 147 |
-
//
|
| 148 |
// ================================
|
| 149 |
btnConnect.addEventListener('click', async () => {
|
| 150 |
try {
|
| 151 |
-
statusText.innerText = "Đang
|
| 152 |
-
|
| 153 |
-
// Chỉ yêu cầu kết nối với thiết bị có Service Nhịp Tim (0x180D)
|
| 154 |
const device = await navigator.bluetooth.requestDevice({
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
});
|
| 157 |
|
| 158 |
-
statusText.innerText = "Đang kết nối tới " + device.name + "...";
|
| 159 |
const server = await device.gatt.connect();
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
const service
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
statusText.innerText = "Đã kết nối! Đang chờ BPM...";
|
| 176 |
btnConnect.innerText = "✓ BLE Connected";
|
| 177 |
-
btnConnect.style.backgroundColor = "#4ecca3";
|
| 178 |
-
btnConnect.style.color = "#1a1a2e";
|
| 179 |
|
| 180 |
} catch (error) {
|
| 181 |
console.error(error);
|
| 182 |
-
statusText.innerText = "Lỗi
|
| 183 |
statusText.style.color = "#ff4d4d";
|
| 184 |
}
|
| 185 |
});
|
|
|
|
| 119 |
// ================================
|
| 120 |
// ĐỌC DỮ LIỆU NHỊP TIM CHUẨN BLE
|
| 121 |
// ================================
|
| 122 |
+
function handleAutoData(event, uuid) {
|
| 123 |
const value = event.target.value;
|
| 124 |
+
|
| 125 |
+
let raw = [];
|
| 126 |
+
for (let i = 0; i < value.byteLength; i++) {
|
| 127 |
+
raw.push(value.getUint8(i));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
}
|
| 129 |
|
| 130 |
+
console.log("📡 UUID:", uuid, "DATA:", raw);
|
| 131 |
+
|
| 132 |
+
for (let i = 0; i < raw.length; i++) {
|
| 133 |
+
let v = raw[i];
|
| 134 |
+
|
| 135 |
+
if (v >= 40 && v <= 180) {
|
| 136 |
+
|
| 137 |
+
let newBpm = smoothBpm(v);
|
| 138 |
+
|
| 139 |
+
if (currentBpm !== 0 && Math.abs(newBpm - currentBpm) > 40) {
|
| 140 |
+
continue;
|
| 141 |
+
}
|
| 142 |
|
| 143 |
+
currentBpm = newBpm;
|
|
|
|
| 144 |
|
| 145 |
+
bpmDisplay.innerText = `${currentBpm} BPM`;
|
| 146 |
+
bpmDisplay.style.color = "#e94560";
|
| 147 |
+
|
| 148 |
+
statusText.innerText = `Đã detect BPM (${uuid})`;
|
| 149 |
+
statusText.style.color = "#4ecca3";
|
| 150 |
+
|
| 151 |
+
detectedCharUUID = uuid;
|
| 152 |
+
break;
|
| 153 |
+
}
|
| 154 |
+
}
|
| 155 |
}
|
| 156 |
|
| 157 |
// ================================
|
| 158 |
+
// AUTO SCAN BLE
|
| 159 |
// ================================
|
| 160 |
btnConnect.addEventListener('click', async () => {
|
| 161 |
try {
|
| 162 |
+
statusText.innerText = "Đang scan BLE...";
|
| 163 |
+
|
|
|
|
| 164 |
const device = await navigator.bluetooth.requestDevice({
|
| 165 |
+
acceptAllDevices: true,
|
| 166 |
+
optionalServices: [
|
| 167 |
+
'battery_service',
|
| 168 |
+
'device_information',
|
| 169 |
+
'heart_rate'
|
| 170 |
+
]
|
| 171 |
});
|
| 172 |
|
|
|
|
| 173 |
const server = await device.gatt.connect();
|
| 174 |
+
const services = await server.getPrimaryServices();
|
| 175 |
+
|
| 176 |
+
for (const service of services) {
|
| 177 |
+
console.log("🔵 Service:", service.uuid);
|
| 178 |
+
|
| 179 |
+
const characteristics = await service.getCharacteristics();
|
| 180 |
+
|
| 181 |
+
for (const char of characteristics) {
|
| 182 |
+
console.log("⚪ Char:", char.uuid, char.properties);
|
| 183 |
+
|
| 184 |
+
if (char.properties.notify || char.properties.indicate) {
|
| 185 |
+
try {
|
| 186 |
+
await char.startNotifications();
|
| 187 |
+
|
| 188 |
+
char.addEventListener(
|
| 189 |
+
'characteristicvaluechanged',
|
| 190 |
+
(event) => handleAutoData(event, char.uuid)
|
| 191 |
+
);
|
| 192 |
+
|
| 193 |
+
console.log("✅ Subscribed:", char.uuid);
|
| 194 |
+
} catch (err) {
|
| 195 |
+
console.log("❌ Cannot subscribe:", char.uuid);
|
| 196 |
+
}
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
}
|
| 200 |
|
| 201 |
statusText.innerText = "Đã kết nối! Đang chờ BPM...";
|
| 202 |
btnConnect.innerText = "✓ BLE Connected";
|
|
|
|
|
|
|
| 203 |
|
| 204 |
} catch (error) {
|
| 205 |
console.error(error);
|
| 206 |
+
statusText.innerText = "Lỗi BLE!";
|
| 207 |
statusText.style.color = "#ff4d4d";
|
| 208 |
}
|
| 209 |
});
|