-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
144 lines (123 loc) · 3.98 KB
/
helpers.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
var ChromeSamples = {
log: function () {
var line = Array.prototype.slice
.call(arguments)
.map(function (argument) {
return typeof argument === "string"
? argument
: JSON.stringify(argument);
})
.join(" ");
document.querySelector("#log").textContent += line + "\n";
},
clearLog: function () {
document.querySelector("#log").textContent = "";
},
};
function getLineForDevice(
deviceName,
deviceId,
onConnect,
onWatchAdvertisement,
onRead,
onForget,
onNaiveStrategy,
onRepeatWatchUntilEventStrategy,
onWatchAndRewatchOnFocusUnfocus,
connectionStatus$
) {
const tableRow = document.createElement("tr");
const deviceNameCell = document.createElement("td");
deviceNameCell.textContent = deviceName;
const deviceIdCell = document.createElement("td");
deviceIdCell.textContent = deviceId;
const deviceConnectionStatus = document.createElement("td");
deviceConnectionStatus.textContent = "Not connected";
connectionStatus$.subscribe((status) => {
deviceConnectionStatus.textContent = status;
});
const connectButton = document.createElement("button");
connectButton.textContent = "Connect";
connectButton.addEventListener("click", onConnect);
const watchAdvertisementButton = document.createElement("button");
watchAdvertisementButton.textContent = "Watch advertisement";
watchAdvertisementButton.addEventListener(
"click",
onWatchAdvertisement
);
const readButton = document.createElement("button");
readButton.textContent =
"Read";
readButton.addEventListener(
"click",
onRead
);
const naiveStrategyButton = document.createElement("button");
naiveStrategyButton.textContent =
"Naive strategy";
naiveStrategyButton.addEventListener(
"click",
onNaiveStrategy
);
const repeatWatchUntilEventStrategyButton = document.createElement("button");
repeatWatchUntilEventStrategyButton.textContent =
"Repeat watch until event strategy";
repeatWatchUntilEventStrategyButton.addEventListener(
"click",
onRepeatWatchUntilEventStrategy
);
const watchAndRewatchOnFocusUnfocusButton = document.createElement("button");
watchAndRewatchOnFocusUnfocusButton.textContent =
"Watch and rewatch on focus/unfocus";
watchAndRewatchOnFocusUnfocusButton.addEventListener(
"click",
onWatchAndRewatchOnFocusUnfocus
);
const forgetDeviceButton = document.createElement("button");
forgetDeviceButton.textContent =
"Forget device";
forgetDeviceButton.addEventListener(
"click",
onForget
);
const deviceActionsCell = document.createElement("td");
deviceActionsCell.appendChild(connectButton);
deviceActionsCell.appendChild(watchAdvertisementButton);
deviceActionsCell.appendChild(readButton);
deviceActionsCell.appendChild(naiveStrategyButton);
deviceActionsCell.appendChild(repeatWatchUntilEventStrategyButton);
deviceActionsCell.appendChild(watchAndRewatchOnFocusUnfocusButton);
deviceActionsCell.appendChild(forgetDeviceButton);
tableRow.appendChild(deviceIdCell);
tableRow.appendChild(deviceNameCell);
tableRow.appendChild(deviceConnectionStatus);
tableRow.appendChild(deviceActionsCell);
return tableRow;
}
function logValue(name, value) {
let log = `< "${name}": `;
for (let i = 0; i < value.byteLength; i++) {
log += value.getUint8(i).toString(16) + " ";
}
ChromeSamples.log(log);
}
/**
* Computes the verification code to send back to the device.
*
* @param {DataView} password stored password
* @param {number} random random number received from device
* @returns verification code
*/
function computeVerificationCode(password, random) {
return (password ^ random) >>> 0;
}
function getPasswordForDeviceId(deviceId) {
const key = `bt-password-${deviceId}`;
return localStorage.getItem(key);
}
function savePasswordForDeviceId(deviceId, password) {
const key = `bt-password-${deviceId}`;
localStorage.setItem(key, password);
}
log = ChromeSamples.log;
clearLog = ChromeSamples.clearLog;