-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
42 lines (42 loc) · 1.19 KB
/
index.html
File metadata and controls
42 lines (42 loc) · 1.19 KB
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
<html>
<body>
<button onclick="connect()">Connect to Web Bluetooth</button>
<script>
function connect() {
var options = {
filters: [
{namePrefix: 'Pixl.js'},
],
optionalServices: [ "7b340000-105b-2b38-3a74-2932f884e90e" ]
};
var busy = false;
var gatt, service;
// Bring up the Web Bluetooth Device Chooser
navigator.bluetooth.requestDevice(options).then(function(device) {
console.log('Device: ' + JSON.stringify(device));
return device.gatt.connect();
}).then(function(g) {
gatt = g;
// Get our custom service
return gatt.getPrimaryService("7b340000-105b-2b38-3a74-2932f884e90e");
}).then(function(service) {
// Get the RGB LED characteristic
return service.getCharacteristic("7b340001-105b-2b38-3a74-2932f884e90e");
}).then(function(characteristic) {
// Make a random color
var rgb = new Uint8Array([
Math.random()*255,
Math.random()*255,
Math.random()*255]);
// Write it to the characteristic
return characteristic.writeValue(rgb);
}).then(function() {
gatt.disconnect();
console.log("All Done!");
}).catch(function(error) {
console.log("Something went wrong. " + error);
});
}
</script>
</body>
</html>