-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth.js
175 lines (160 loc) · 5.33 KB
/
bluetooth.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
import { Robot, robotServices } from './robot.js';
import Dashboard from './dashboard.js';
import sendMessage from './message.js';
export async function displayBluetooth() {
const table = document.createElement('table');
const header = document.createElement('th');
header.colSpan = 3;
header.innerText = "Known Devices";
table.createTHead().insertRow().append(header);
const tableBody = table.createTBody();
const devices = await navigator.bluetooth.getDevices();
for (const device of devices) {
if (typeof device.gatt !== 'undefined') {
addToKnownDevices(device.gatt, tableBody);
} else {
device.forget();
}
}
const row = tableBody.insertRow();
row.insertCell().innerText = "Scan for new robots: ";
const scanForRobotsButton = document.createElement('button');
scanForRobotsButton.innerText = "Scan";
scanForRobotsButton.addEventListener('click', () => scanForRobots(scanForRobotsButton, tableBody));
row.insertCell();
row.insertCell().append(scanForRobotsButton);
document.getElementsByTagName('header')[0].prepend(table);
}
/**
@param {HTMLButtonElement} button
@param {HTMLTableSectionElement} table
@returns {Promise<void>}
*/
async function scanForRobots(button, table) {
button.disabled = true;
button.innerText = "Scanning";
try {
const device = await navigator.bluetooth.requestDevice({
filters: [{ services: [robotServices.identifier.uuid] }],
// Optional services need to be listed to be able to access them later.
optionalServices: [
robotServices.information.uuid,
robotServices.uart.uuid
]
});
if (typeof device.gatt !== 'undefined') {
const [connectButton, forgetButton] = addToKnownDevices(device.gatt, table);
connectRobot(device.gatt, connectButton, forgetButton);
} else {
throw new DOMException("No GATT Server found")
}
} catch (error) {
if (error instanceof DOMException) {
sendMessage(error.message);
}
} finally {
button.innerText = "Scan";
button.disabled = false;
}
}
/**
@param {BluetoothRemoteGATTServer} gattServer
@param {HTMLButtonElement} connectButton
@param {HTMLButtonElement} forgetButton
@returns {Promise<void>}
*/
async function connectRobot( gattServer, connectButton, forgetButton) {
sendMessage("Connecting", 3000);
try {
gattServer = await gattServer.connect()
const uart = await gattServer.getPrimaryService(robotServices.uart.uuid);
const rx = await uart.getCharacteristic(robotServices.uart.characteristic.rx);
const tx = await uart.getCharacteristic(robotServices.uart.characteristic.tx);
sendMessage("Connected", 3000);
connectButton.innerText = "Disconnect";
const robot = new Robot(gattServer, rx, tx);
const dashboard = new Dashboard(robot, forgetButton);
const controller = new AbortController();
// @ts-ignore
gattServer.device.addEventListener(
"gattserverdisconnected",
() => onDisconnected(connectButton, dashboard, controller),
{signal: controller.signal}
);
} catch (error) {
if (error instanceof DOMException) {
sendMessage(error.message);
}
}
}
/**
@param {BluetoothRemoteGATTServer} gattServer
@param {HTMLTableSectionElement} table
@returns {HTMLButtonElement[]}
*/
function addToKnownDevices(gattServer, table) {
const row = table.insertRow(0);
row.insertCell().innerText = gattServer.device.name ? gattServer.device.name : gattServer.device.id;
row.id = gattServer.device.id;
const connectButton = document.createElement('button');
connectButton.innerText = "Connect";
connectButton.addEventListener('click', () => connectToGATTServer(gattServer, connectButton, forgetButton));
row.insertCell().append(connectButton);
const forgetButton = document.createElement('button');
forgetButton.innerText = "Forget";
forgetButton.addEventListener('click', () => forgetDevice(forgetButton, row, gattServer))
row.insertCell().append(forgetButton);
return [connectButton, forgetButton];
}
/**
@param {BluetoothRemoteGATTServer} gattServer
@param {HTMLButtonElement} connectButton
@param {HTMLButtonElement} forgetButton
@returns {Promise<void>}
*/
async function connectToGATTServer(gattServer, connectButton, forgetButton) {
connectButton.disabled = true;
if (gattServer.connected) {
sendMessage("Disconnecting", 3000);
gattServer.disconnect();
sendMessage("Disconnected", 3000);
connectButton.innerText = "Connect";
} else {
try {
connectRobot(gattServer, connectButton, forgetButton);
} catch (error) {
if (error instanceof DOMException) {
sendMessage(error.message);
}
} finally {
connectButton.disabled = false;
}
}
connectButton.disabled = false;
}
/**
@param {HTMLButtonElement} button
@param {HTMLTableRowElement} row
@param {BluetoothRemoteGATTServer} gattServer
@returns {Promise<void>}
*/
async function forgetDevice(button, row, gattServer) {
button.disabled = true;
if (gattServer.connected) {
gattServer.disconnect();
}
gattServer.device.forget();
button.innerText = "Removed";
setTimeout(() => row.remove(), 600);
}
/**
@param {HTMLButtonElement} button
@param {Dashboard} dashboard
@param {AbortController} controller
*/
function onDisconnected(button, dashboard, controller) {
button.innerText = "Connect";
dashboard.remove();
controller.abort();
sendMessage("Robot disconnected", 3000);
}