-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth-logic.js
262 lines (224 loc) · 8.23 KB
/
bluetooth-logic.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
let globalState = "IDLE",
/** @type {BluetoothRemoteGATTServer} */
globalServer;
// BPM means Blood Pressure Monitor
let globalBPMState = "IDLE",
/** @type {BluetoothRemoteGATTService} */
globalBPMService;
const globalAccountId = Math.floor(Math.random() * (Math.pow(2, 32) - 1));
log("globalAccountId : " + globalAccountId);
const setState = (state) => {
log("👥: " + state);
globalState = state;
};
const setBPMState = (state) => {
log("❤️: " + state);
globalBPMState = state;
};
const TRANSTEK_WRITE_CHARACTERISTIC = 0x8a81; // [WRITE] Information transfer from App to Device
const TRANSTEK_READ_CHARACTERISTIC = 0x8a82; // [Indicate] Information transfer from Device to App
const TRANSTEK_BPM_READ_CHARACTERISTIC = 0x8a91; // [Indicate]: Blood pressure measurement transfer from Device to App
const TRANSTEK_BASE_TIME = new Date(2010, 0, 1).getTime();
const BLUETOOTH_COMMANDS = Object.freeze({
PASSWORD: 0xa0,
ACCOUNT: 0x21,
RANDOM: 0xa1,
VERIFICATION_CODE: 0x20,
TIME_OFFSET: 0x02,
MEASURE: 0x1e,
DISCONNECT: 0x22,
});
const BLOOD_PRESSURE_MONITOR_OPTS = {
bluetoothStorageKey: 0x7809,
scanFilter: { filters: [{ services: [0x7809] }] },
type: "BLOOD_PRESSURE_MONITOR",
characteristics: [
0x8a81, // [Write]: Information transfer from App to Device
0x8a82, // [Indicate]: Information transfer from Device to App
0x8a91, // [Indicate]: Blood pressure measurement transfer from Device to App
0x8a92, // [Notify]: Blood pressure measurement transfer from Device to App
],
};
const delayPromise = (delay) => {
return new Promise((resolve) => {
setTimeout(resolve, delay);
});
};
const pairDevice = async (device) => {
const server = await device.gatt.connect();
console.log("Connected to device", device);
console.log("Server", server);
const serviceId = BLOOD_PRESSURE_MONITOR_OPTS.bluetoothStorageKey;
const service = await server.getPrimaryService(serviceId);
// Getting read Characteristic
const readChar = await service.getCharacteristic(
TRANSTEK_READ_CHARACTERISTIC
);
const writeChar = await service.getCharacteristic(
TRANSTEK_WRITE_CHARACTERISTIC
);
log("> Notifications started");
// log to the screen if the gatt server disconnects
if (!globalServer) {
device.addEventListener("gattserverdisconnected", () => {
setState("DISCONNECTED");
});
}
readChar.addEventListener("characteristicvaluechanged", async (e) => {
await readCharChangedPairing(e, {
deviceId: device.id,
writeChar,
});
});
// launching pairing protocol
await readChar.startNotifications();
globalServer = server;
// When doing the pairing, all the conversation should be done with the same characteristic
// It will not work if you try another service.getCharacteristic() call
};
const readCharChangedPairing = async (event, { deviceId, writeChar }) => {
/** @type {DataView} */
const value = event.target.value;
logValue("charChangedPairing", value);
if (value.getUint8(0) === BLUETOOTH_COMMANDS.PASSWORD) {
setState("RECEIVED_PASSWORD");
const password = value.getUint32(1, true);
setState("REQUESTING_RANDOM_FROM_ACCOUNT_ID");
const accountId = globalAccountId;
await Promise.resolve(writeChar).then((_writeChar) => {
const buffer = new ArrayBuffer(5);
const view = new DataView(buffer);
view.setUint8(0, BLUETOOTH_COMMANDS.ACCOUNT);
view.setUint32(1, accountId, true);
return _writeChar.writeValue(buffer);
});
savePasswordForDeviceId(deviceId, password);
}
if (value.getUint8(0) === BLUETOOTH_COMMANDS.RANDOM) {
setState("RECEIVED_RANDOM_FOR_VERIFICATION");
const random = value.getUint32(1, true);
const password = getPasswordForDeviceId(deviceId);
const verificationCode = computeVerificationCode(password, random);
setState("SENDING_VERIFICATION_CODE");
await Promise.resolve(writeChar).then((_writeChar) => {
const buffer = new ArrayBuffer(5);
const view = new DataView(buffer);
view.setUint8(0, BLUETOOTH_COMMANDS.VERIFICATION_CODE);
view.setUint32(1, verificationCode, true);
return _writeChar.writeValue(buffer);
});
// log("If we get disconnected, the verificationCode must be wrong");
setState("SENDING_TIME_OFFSET");
const offset = Math.floor((Date.now() - TRANSTEK_BASE_TIME) / 1000);
await Promise.resolve(writeChar).then((_writeChar) => {
const buffer = new ArrayBuffer(5);
const view = new DataView(buffer);
view.setUint8(0, BLUETOOTH_COMMANDS.TIME_OFFSET);
view.setUint32(1, offset, true);
return _writeChar.writeValue(buffer);
});
setState("SENDING_DISCONNECTION");
await Promise.resolve(writeChar).then((_writeChar) => {
const buffer = new ArrayBuffer(1);
const view = new DataView(buffer);
view.setUint8(0, BLUETOOTH_COMMANDS.DISCONNECT);
return _writeChar.writeValue(buffer);
});
setState("PAIRED");
}
};
const measureHeartRateWithDevice = async (device, password) => {
const server = await device.gatt.connect();
const serviceId = BLOOD_PRESSURE_MONITOR_OPTS.bluetoothStorageKey;
const service = await server.getPrimaryService(serviceId);
const readChar = await service.getCharacteristic(
TRANSTEK_READ_CHARACTERISTIC
);
const writeChar = await service.getCharacteristic(
TRANSTEK_WRITE_CHARACTERISTIC
);
setBPMState("REQUESTING_BPM_MEASURE");
readChar.addEventListener("characteristicvaluechanged", async (e) => {
await readCharChangedBpm(e, {
password,
service,
bpmWriteChar: writeChar,
});
});
await readChar.startNotifications();
log("> BPM Notifications started");
};
const readCharChangedBpm = async (
event,
{ password, service, bpmWriteChar }
) => {
/** @type {DataView} */
const value = event.target.value;
logValue("BPM Char Changed", value);
if (value.getUint8(0) === BLUETOOTH_COMMANDS.RANDOM) {
setBPMState("RECEIVED_RANDOM");
const random = value.getUint32(1, true);
setBPMState("SENDING_VERIFICATION_CODE");
const verifCode = computeVerificationCode(password, random);
await Promise.resolve(bpmWriteChar).then((_writeChar) => {
const buffer = new ArrayBuffer(5);
const view = new DataView(buffer);
view.setUint8(0, BLUETOOTH_COMMANDS.VERIFICATION_CODE);
view.setUint32(1, verifCode, true);
return _writeChar.writeValue(buffer);
});
setBPMState("SENDING_TIME_OFFSET");
const offset = Math.floor((Date.now() - TRANSTEK_BASE_TIME) / 1000);
await Promise.resolve(bpmWriteChar).then((_writeChar) => {
const buffer = new ArrayBuffer(5);
const view = new DataView(buffer);
view.setUint8(0, BLUETOOTH_COMMANDS.TIME_OFFSET);
view.setUint32(1, offset, true);
return _writeChar.writeValue(buffer);
});
// Be ready to receive measurement data
setBPMState("REQUEST_MEASUREMENT");
const bpmChar = await service.getCharacteristic(
TRANSTEK_BPM_READ_CHARACTERISTIC
);
bpmChar.addEventListener("gattserverdisconnected", (e) => {
setBPMState("STATE.DISCONNECTED");
});
bpmChar.addEventListener("characteristicvaluechanged", async (e) => {
await readMeasurementData(e, {
bpmWriteChar,
});
});
await bpmChar.startNotifications();
log("> Start notifications for BPM");
}
};
const readMeasurementData = async (event, { bpmWriteChar }) => {
const value = event.target.value;
if (globalBPMState !== "REQUEST_MEASUREMENT") {
log(`⚠️ We have received data in state: ${globalBPMState}`);
}
if (value.getUint8(0) !== BLUETOOTH_COMMANDS.MEASURE) {
log(`⚠️ We have received data that is not a measure`);
logValue("readMeasurementData", value);
return;
}
const systolic = value.getUint16(1, true);
const diastolic = value.getUint16(3, true);
const pulsation = value.getUint16(11, true);
log(
[
`✅ Systolic: ${systolic}`,
`Diastolic: ${diastolic}`,
`Pulsation: ${pulsation}`,
].join(", ")
);
setBPMState("SENDING_DISCONNECTION");
await Promise.resolve(bpmWriteChar).then((_writeChar) => {
const buffer = new ArrayBuffer(1);
const view = new DataView(buffer);
view.setUint8(0, BLUETOOTH_COMMANDS.DISCONNECT);
return _writeChar.writeValue(buffer);
});
setBPMState("MEASURE_ACQUIRED");
};