Hezu06 commited on
Commit
0b72b24
·
1 Parent(s): 6d55568

refactor heart rate measurement logic for improved data handling and clarity

Browse files
Files changed (1) hide show
  1. 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 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
  });
 
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
  });