-
Notifications
You must be signed in to change notification settings - Fork 4
/
microbit-web-bluetooth-uart.js
271 lines (249 loc) · 12.4 KB
/
microbit-web-bluetooth-uart.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
263
264
265
266
267
268
269
270
271
/**
* Connects to a Bluetooth device.
* The background color shows if a Bluetooth device is connected (green) or
* disconnected (red).
* Allows to interact with the characteristics of the micro:bit Bluetooth UART
* service.
*/
var bluetoothDevice;
/**
* Object containing the Bluetooth UUIDs of all the services and
* characteristics of the micro:bit.
*/
microbitUuid = {
/**
* Services
*/
genericAccess: ["00001800-0000-1000-8000-00805f9b34fb", "Generic Access"],
genericAttribute: ["00001801-0000-1000-8000-00805f9b34fb", "Generic Attribute"],
deviceInformation: ["0000180a-0000-1000-8000-00805f9b34fb", "Device Information"],
accelerometerService: ["e95d0753-251d-470a-a062-fa1922dfa9a8", "Accelerometer Service"],
magnetometerService: ["e95df2d8-251d-470a-a062-fa1922dfa9a8", "Magnetometer Service"],
buttonService: ["e95d9882-251d-470a-a062-fa1922dfa9a8", "Button Service"],
ioPinService: ["e95d127b-251d-470a-a062-fa1922dfa9a8", "IO Pin Service"],
ledService: ["e95dd91d-251d-470a-a062-fa1922dfa9a8", "LED Service"],
eventService: ["e95d93af-251d-470a-a062-fa1922dfa9a8", "Event Service"],
dfuControlService: ["e95d93b0-251d-470a-a062-fa1922dfa9a8", "DFU Control Service"],
temperatureService: ["e95d6100-251d-470a-a062-fa1922dfa9a8", "Temperature Service"],
uartService: ["6e400001-b5a3-f393-e0a9-e50e24dcca9e", "UART Service"],
/**
* Characteristics
*/
deviceName: ["00002a00-0000-1000-8000-00805f9b34fb", "Device Name"],
appearance: ["00002a01-0000-1000-8000-00805f9b34fb", "Appearance"],
peripheralPreferredConnectionParameters: ["00002a04-0000-1000-8000-00805f9b34fb", "Peripheral Preferred Connection Parameters"],
serviceChanged: ["00002a05-0000-1000-8000-00805f9b34fb", "Service Changed"],
modelNumberString: ["00002a24-0000-1000-8000-00805f9b34fb", "Model Number String"],
serialNumberString: ["00002a25-0000-1000-8000-00805f9b34fb", "Serial Number String"],
hardwareRevisionString: ["00002a27-0000-1000-8000-00805f9b34fb", "Hardware Revision String"],
firmwareRevisionString: ["00002a26-0000-1000-8000-00805f9b34fb", "Firmware Revision String"],
manufacturerNameString: ["00002a29-0000-1000-8000-00805f9b34fb", "Manufacturer Name String"],
accelerometerData: ["e95dca4b-251d-470a-a062-fa1922dfa9a8", "Accelerometer Data"],
accelerometerPeriod: ["e95dfb24-251d-470a-a062-fa1922dfa9a8", "Accelerometer Period"],
magnetometerData: ["e95dfb11-251d-470a-a062-fa1922dfa9a8", "Magnetometer Data"],
magnetometerPeriod: ["e95d386c-251d-470a-a062-fa1922dfa9a8", "Magnetometer Period"],
magnetometerBearing: ["e95d9715-251d-470a-a062-fa1922dfa9a8", "Magnetometer Bearing"],
magnetometerCalibration: ["e95db358-251d-470a-a062-fa1922dfa9a8", "Magnetometer Calibration"],
buttonAState: ["e95dda90-251d-470a-a062-fa1922dfa9a8", "Button A State"],
buttonBState: ["e95dda91-251d-470a-a062-fa1922dfa9a8", "Button B State"],
pinData: ["e95d8d00-251d-470a-a062-fa1922dfa9a8", "Pin Data"],
pinADConfiguration: ["e95d5899-251d-470a-a062-fa1922dfa9a8", "Pin AD Configuration"],
pinIOConfiguration: ["e95db9fe-251d-470a-a062-fa1922dfa9a8", "Pin IO Configuration"],
pwmControl: ["e95dd822-251d-470a-a062-fa1922dfa9a8", "PWM Control"],
ledMatrixState: ["e95d7b77-251d-470a-a062-fa1922dfa9a8", "LED Matrix State"],
ledText: ["e95d93ee-251d-470a-a062-fa1922dfa9a8", "LED Text"],
scrollingDelay: ["e95d0d2d-251d-470a-a062-fa1922dfa9a8", "Scrolling Delay"],
microbitRequirements: ["e95db84c-251d-470a-a062-fa1922dfa9a8", "MicroBit Requirements"],
microbitEvent: ["e95d9775-251d-470a-a062-fa1922dfa9a8", "MicroBit Event"],
clientRequirements: ["e95d23c4-251d-470a-a062-fa1922dfa9a8", "Client Requirements"],
clientEvent: ["e95d5404-251d-470a-a062-fa1922dfa9a8", "Client Event"],
dfuControl: ["e95d93b1-251d-470a-a062-fa1922dfa9a8", "DFU Control"],
temperature: ["e95d9250-251d-470a-a062-fa1922dfa9a8", "Temperature"],
temperaturePeriod: ["e95d1b25-251d-470a-a062-fa1922dfa9a8", "Temperature Period"],
txCharacteristic: ["6e400002-b5a3-f393-e0a9-e50e24dcca9e", "Tx Characteristic"],
rxCharacteristic: ["6e400003-b5a3-f393-e0a9-e50e24dcca9e", "Rx Characteristic"],
/**
* Method that searches an UUID among the UUIDs of all the services and
* characteristics and returns:
* - in HTML blue color the name of the service/characteristic found.
* - in HTML red color a message if the UUID has not been found.
* @param uuid The service or characteristic UUID.
* @param serviceOrCharacteristic True (or 1) if it is a service, and false
* (or 0) if it is a characteristic.
*/
searchUuid(uuid, serviceOrCharacteristic) {
for (const key in microbitUuid) {
if (uuid === microbitUuid[key][0]) {
return "<font color='blue'>" + microbitUuid[key][1] + "</font>";
}
}
if (serviceOrCharacteristic) {
return "<font color='red'>Unknown Micro:Bit Service</font>";
} else {
return "<font color='red'>Unknown Micro:Bit Characteristic</font>";
}
},
}
/**
* Function that adds string to the log. If newLine is true, it adds a new line
* at the end of the string.
* @param string String to print to the log.
* @param newLine Boolean that specifies whether to start a new line or not.
*/
function addLog(string, newLine) {
document.getElementById("log").innerHTML += string;
if (newLine) {
document.getElementById("log").innerHTML += "<br>";
};
}
/**
* Function that adds string (and newline) to the log in bold and red color.
* @param string String to print to the log.
*/
function addLogError(string) {
addLog("<b><font color='red'>" + string + "</font></b>", true);
}
/**
* Function that empties the log.
*/
function clearLog() {
document.getElementById("log").innerHTML = "";
}
/**
* Function that empties the UART TX field.
*/
function clearTx() {
document.getElementById("tx").innerHTML = "";
}
/**
* Function that turns the background color red.
*/
function onDisconnected() {
document.getElementById("body").style = "background-color:#FFD0D0";
}
var txCharacteristic;
var rxCharacteristic;
/**
* Function that updates the HTML element according to the UART characteristic.
*/
function readTx(event) {
//addLog("Reading TX... ", false);
if (!("TextDecoder") in window) {
addLogError("Sorry, this browser does not support TextDecoder...");
} else {
let enc = new TextDecoder("utf-8");
document.getElementById("tx").innerHTML += enc.decode(event.target.value) + "<br>";
//addLog("<font color='green'>OK</font>", true);
};
}
/**
* Function that updates the RX using the corresponding micro:bit Bluetooth
* characteristic.
*/
function writeRx() {
addLog("Writing RX... ", false);
if (!bluetoothDevice) {
addLogError("There is no device connected.");
} else {
if (bluetoothDevice.gatt.connected) {
if (!rxCharacteristic) {
addLogError("There is no RX characteristic.");
} else {
if (!("TextEncoder" in window)) {
addLogError("Sorry, this browser does not support TextEncoder...");
} else {
let enc = new TextEncoder();
rxCharacteristic.writeValue(enc.encode(document.getElementById("rxText").value + '\n'))
.then(_ => {
addLog("<font color='green'>OK</font>", true);
})
.catch(error => {
addLogError(error);
});
};
};
} else {
addLogError("There is no device connected.");
};
};
}
/**
* Function that connects to a Bluetooth device, and saves the characteristics
* associated with the UART service.
*/
function connect() {
addLog("Requesting micro:bit Bluetooth devices... ", false);
if (!navigator.bluetooth) {
addErrorLog("Bluetooth not available in this browser or computer.");
} else {
navigator.bluetooth.requestDevice({
// To accept all devices, use acceptAllDevices: true and remove filters.
filters: [{namePrefix: "BBC micro:bit"}],
optionalServices: [microbitUuid.genericAccess[0], microbitUuid.genericAttribute[0], microbitUuid.deviceInformation[0], microbitUuid.accelerometerService[0], microbitUuid.magnetometerService[0], microbitUuid.buttonService[0], microbitUuid.ioPinService[0], microbitUuid.ledService[0], microbitUuid.eventService[0], microbitUuid.dfuControlService[0], microbitUuid.temperatureService[0], microbitUuid.uartService[0]],
})
.then(device => {
addLog("<font color='green'>OK</font>", true);
bluetoothDevice = device;
addLog("Connecting to GATT server (name: <font color='blue'>" + device.name + "</font>, ID: <font color='blue'>" + device.id + "</font>)... ", false);
device.addEventListener('gattserverdisconnected', onDisconnected);
document.getElementById("body").style = "background-color:#D0FFD0";
return device.gatt.connect();
})
.then(server => {
addLog("<font color='green'>OK</font>", true);
addLog("Getting UART service (UUID: " + microbitUuid.uartService[0] + ")... ", false);
return server.getPrimaryService(microbitUuid.uartService[0]);
})
.then(service => {
addLog("<font color='green'>OK</font>", true);
addLog("Getting TX characteristic... ", false);
service.getCharacteristic(microbitUuid.txCharacteristic[0])
.then(txChar => {
addLog("<font color='green'>OK</font>", true);
txCharacteristic = txChar;
addLog("Starting TX notifications... ", false);
return txChar.startNotifications()
.then(_ => {
txChar.addEventListener('characteristicvaluechanged', readTx);
addLog("<font color='green'>OK</font>", true);
addLog("Getting RX characteristic... ", false);
service.getCharacteristic(microbitUuid.rxCharacteristic[0])
.then(rxChar => {
rxCharacteristic = rxChar;
addLog("<font color='green'>OK</font>", true);
})
.catch(error => {
addErrorLog(error);
});
})
.catch(error => {
addLogError(error);
});
})
.catch(error => {
addLogError(error);
});
})
.catch(error => {
addLogError(error);
});
};
}
/**
* Function that disconnects from the Bluetooth device (if connected).
*/
function disconnect() {
addLog("Disconnecting... ", false);
if (!bluetoothDevice) {
addLogError("There is no device connected.");
} else {
if (bluetoothDevice.gatt.connected) {
bluetoothDevice.gatt.disconnect();
if (!bluetoothDevice.gatt.connected) {
addLog("<font color='green'>OK</font>", true);
}
} else {
addLogError("There is no device connected.");
};
};
}