refactor heart rate measurement logic in index.html for improved clarity and functionality
Browse files- index.html +43 -64
index.html
CHANGED
|
@@ -116,91 +116,70 @@
|
|
| 116 |
// ================================
|
| 117 |
// AUTO DETECT DATA
|
| 118 |
// ================================
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
| 120 |
const value = event.target.value;
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
|
| 127 |
-
console.log(
|
| 128 |
-
|
| 129 |
-
for (let i = 0; i < raw.length; i++) {
|
| 130 |
-
let v = raw[i];
|
| 131 |
-
|
| 132 |
-
if (v >= 40 && v <= 180) {
|
| 133 |
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
currentBpm = newBpm;
|
| 141 |
-
|
| 142 |
-
bpmDisplay.innerText = `${currentBpm} BPM`;
|
| 143 |
-
bpmDisplay.style.color = "#e94560";
|
| 144 |
-
|
| 145 |
-
statusText.innerText = `Đã detect BPM (${uuid})`;
|
| 146 |
-
statusText.style.color = "#4ecca3";
|
| 147 |
-
|
| 148 |
-
detectedCharUUID = uuid;
|
| 149 |
-
break;
|
| 150 |
-
}
|
| 151 |
-
}
|
| 152 |
}
|
| 153 |
|
| 154 |
// ================================
|
| 155 |
-
//
|
| 156 |
// ================================
|
| 157 |
btnConnect.addEventListener('click', async () => {
|
| 158 |
try {
|
| 159 |
-
statusText.innerText = "Đang
|
| 160 |
-
|
|
|
|
| 161 |
const device = await navigator.bluetooth.requestDevice({
|
| 162 |
-
|
| 163 |
-
optionalServices: [
|
| 164 |
-
'battery_service',
|
| 165 |
-
'device_information',
|
| 166 |
-
'heart_rate'
|
| 167 |
-
]
|
| 168 |
});
|
| 169 |
|
|
|
|
| 170 |
const server = await device.gatt.connect();
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
char.addEventListener(
|
| 186 |
-
'characteristicvaluechanged',
|
| 187 |
-
(event) => handleAutoData(event, char.uuid)
|
| 188 |
-
);
|
| 189 |
-
|
| 190 |
-
console.log("✅ Subscribed:", char.uuid);
|
| 191 |
-
} catch (err) {
|
| 192 |
-
console.log("❌ Cannot subscribe:", char.uuid);
|
| 193 |
-
}
|
| 194 |
-
}
|
| 195 |
-
}
|
| 196 |
-
}
|
| 197 |
|
| 198 |
statusText.innerText = "Đã kết nối! Đang chờ BPM...";
|
| 199 |
btnConnect.innerText = "✓ BLE Connected";
|
|
|
|
|
|
|
| 200 |
|
| 201 |
} catch (error) {
|
| 202 |
console.error(error);
|
| 203 |
-
statusText.innerText = "Lỗi
|
| 204 |
statusText.style.color = "#ff4d4d";
|
| 205 |
}
|
| 206 |
});
|
|
|
|
| 116 |
// ================================
|
| 117 |
// AUTO DETECT DATA
|
| 118 |
// ================================
|
| 119 |
+
// ================================
|
| 120 |
+
// ĐỌC DỮ LIỆU NHỊP TIM CHUẨN BLE
|
| 121 |
+
// ================================
|
| 122 |
+
function handleHeartRateMeasurement(event) {
|
| 123 |
const value = event.target.value;
|
| 124 |
+
// Chuẩn BLE: Byte 0 là cờ (Flags) quy định định dạng
|
| 125 |
+
const flags = value.getUint8(0);
|
| 126 |
+
const format16bit = flags & 0x01; // Kiểm tra bit đầu tiên
|
| 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(`❤️ Dữ liệu nhịp tim thô: ${newBpm} BPM`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
+
// Áp dụng thuật toán làm mượt
|
| 138 |
+
currentBpm = smoothBpm(newBpm);
|
| 139 |
|
| 140 |
+
bpmDisplay.innerText = `${currentBpm} BPM`;
|
| 141 |
+
bpmDisplay.style.color = "#e94560";
|
| 142 |
+
statusText.innerText = "Đang nhận dữ liệu nhịp tim...";
|
| 143 |
+
statusText.style.color = "#4ecca3";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
}
|
| 145 |
|
| 146 |
// ================================
|
| 147 |
+
// KẾT NỐI THEO CHUẨN BLUETOOTH SIG
|
| 148 |
// ================================
|
| 149 |
btnConnect.addEventListener('click', async () => {
|
| 150 |
try {
|
| 151 |
+
statusText.innerText = "Đang tìm thiết bị đo nhịp tim chuẩn BLE...";
|
| 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 |
+
filters: [{ services: ['heart_rate'] }]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
});
|
| 157 |
|
| 158 |
+
statusText.innerText = "Đang kết nối tới " + device.name + "...";
|
| 159 |
const server = await device.gatt.connect();
|
| 160 |
+
|
| 161 |
+
// Trực tiếp lấy Service Nhịp tim
|
| 162 |
+
const service = await server.getPrimaryService('heart_rate');
|
| 163 |
+
|
| 164 |
+
// Trực tiếp lấy Characteristic đo nhịp tim (0x2A37)
|
| 165 |
+
const characteristic = await service.getCharacteristic('heart_rate_measurement');
|
| 166 |
+
|
| 167 |
+
// Bật thông báo (Notify)
|
| 168 |
+
await characteristic.startNotifications();
|
| 169 |
+
|
| 170 |
+
characteristic.addEventListener(
|
| 171 |
+
'characteristicvaluechanged',
|
| 172 |
+
handleHeartRateMeasurement
|
| 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 kết nối: " + error.message;
|
| 183 |
statusText.style.color = "#ff4d4d";
|
| 184 |
}
|
| 185 |
});
|