Hezu06 commited on
Commit
6d55568
·
1 Parent(s): 89cf37d

refactor heart rate measurement logic in index.html for improved clarity and functionality

Browse files
Files changed (1) hide show
  1. index.html +43 -64
index.html CHANGED
@@ -116,91 +116,70 @@
116
  // ================================
117
  // AUTO DETECT DATA
118
  // ================================
119
- function handleAutoData(event, uuid) {
 
 
 
120
  const value = event.target.value;
121
-
122
- let raw = [];
123
- for (let i = 0; i < value.byteLength; i++) {
124
- raw.push(value.getUint8(i));
 
 
 
 
 
125
  }
126
 
127
- console.log("📡 UUID:", uuid, "DATA:", raw);
128
-
129
- for (let i = 0; i < raw.length; i++) {
130
- let v = raw[i];
131
-
132
- if (v >= 40 && v <= 180) {
133
 
134
- let newBpm = smoothBpm(v);
 
135
 
136
- if (currentBpm !== 0 && Math.abs(newBpm - currentBpm) > 40) {
137
- continue;
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
- // AUTO SCAN BLE
156
  // ================================
157
  btnConnect.addEventListener('click', async () => {
158
  try {
159
- statusText.innerText = "Đang scan BLE...";
160
-
 
161
  const device = await navigator.bluetooth.requestDevice({
162
- acceptAllDevices: true,
163
- optionalServices: [
164
- 'battery_service',
165
- 'device_information',
166
- 'heart_rate'
167
- ]
168
  });
169
 
 
170
  const server = await device.gatt.connect();
171
- const services = await server.getPrimaryServices();
172
-
173
- for (const service of services) {
174
- console.log("🔵 Service:", service.uuid);
175
-
176
- const characteristics = await service.getCharacteristics();
177
-
178
- for (const char of characteristics) {
179
- console.log("⚪ Char:", char.uuid, char.properties);
180
-
181
- if (char.properties.notify || char.properties.indicate) {
182
- try {
183
- await char.startNotifications();
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 BLE!";
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
  });