-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
175 lines (156 loc) · 6.05 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Bluetooth experiment with Thingy 52 - LED</title>
<meta charset="utf-8">
<!--
https://nordicsemiconductor.github.io/Nordic-Thingy52-FW/documentation/
-->
<script>
const serviceUUID = 'ef680100-9b35-4933-9b10-52ffa9740042';
const optionalServicesUUID = [
"ef680200-9b35-4933-9b10-52ffa9740042",
"ef680300-9b35-4933-9b10-52ffa9740042",
"ef680400-9b35-4933-9b10-52ffa9740042",
"ef680500-9b35-4933-9b10-52ffa9740042"
];
//UI
const uiServicesUUID = 'ef680300-9b35-4933-9b10-52ffa9740042';
const buttonCharacteristicUUID = 'ef680302-9b35-4933-9b10-52ffa9740042';
const ledCharacteristicUUID = 'ef680301-9b35-4933-9b10-52ffa9740042';
//Battery
const batteryServiceUUID = '180f';
const batteryCharacteristicUUID = '2a19';
var bleDevice;
var bleServer;
var uiService;
var ledChar;
window.onload = function() {
document.querySelector('#connect').addEventListener('click', connect);
document.querySelector('#disconnect').addEventListener('click', disconnect);
document.querySelector('#ledOff').addEventListener('click', LEDOff);
document.querySelector('#ledConstant').addEventListener('click', LEDConstantRGB);
document.querySelector('#ledBreathe').addEventListener('click', LEDBreathe);
document.querySelector('#ledOneshot').addEventListener('click', LEDOneShot);
};
function connect() {
if (!navigator.bluetooth) {
return log('Web Bluetooth API is not available in browser.');
}
log('=============================');
log('Requesting Bluetooth Device...');
// RequestDevice must be in click handler (or other user inited events)
navigator.bluetooth.requestDevice({
filters: [{
services: [serviceUUID],
//optionalServices: optionalServicesUUID
//"name": "Thingy"
}]
})
.then(device => {
bleDevice = device;
console.log({
Name: device.name,
Id: device.id,
UUIDs: device.uuids,
Device: device
});
log('Connected to device: ' + device.name);
return device.gatt.connect();
})
.then(server => {
bleServer = server;
log('Found Server', server);
})
.then(() => bleServer.getPrimaryService(uiServicesUUID))
.then(service => {
log('Found UI service', service);
uiService = service;
})
.then(() => uiService.getCharacteristic(ledCharacteristicUUID))
.then(characteristic => {
log('Found LED characteristic');
ledChar = characteristic;
})
.then(() => {
document.getElementById("ledButtons").classList.remove('hidden');
log('=========== READY ===========');
})
.catch(error => {
log('Connection Error: ' + error);
});
}
function disconnect() {
if (!bleDevice) {
log('No Bluetooth Device found...');
return;
}
log('Disconnecting from Bluetooth Device...');
if (bleDevice.gatt.connected) {
bleDevice.gatt.disconnect();
log('Bluetooth Device connected: ' + bleDevice.gatt.connected);
} else {
log('Bluetooth Device is already disconnected');
}
}
function LEDOff(event) {
if (!ledChar) return;
const mode = 0
const dataArray = new Uint8Array([mode])
log("LED off", dataArray)
ledChar.writeValue(dataArray)
}
function LEDConstantRGB(event) {
if (!ledChar) return;
const mode = 1;
const red = 255;
const green = 0;
const blue = 128;
const dataArray = new Uint8Array([mode, red, green, blue])
log("LED constant color: rgb(" + red + "," + green + "," + blue + ")", dataArray)
ledChar.writeValue(dataArray)
}
function LEDBreathe(event) {
if (!ledChar) return
const mode = 2
const predefinedColor = 4
const intensity = 50
const delay = 500
const dataArray = new Uint8Array([mode, predefinedColor, intensity, delay & 0xFF, (delay >> 8) & 0xFF])
log("LED Breathe mode: ", dataArray)
ledChar.writeValue(dataArray)
}
function LEDOneShot(event) {
if (!ledChar) return;
const mode = 3
const predefinedColor = 2
const intensity = 90
const dataArray = new Uint8Array([mode, predefinedColor, intensity])
log("LED one shot breath", dataArray);
ledChar.writeValue(dataArray);
}
function log(text) {
console.log(arguments);
const logElm = document.querySelector('#log');
logElm.textContent = text + '\n' + logElm.textContent;
}
</script>
<style>
.hidden {
display: none;
}
</style>
</head>
<body>
<button id="connect">Connect</button>
<button id="disconnect">Disonnect</button>
<div id="ledButtons" class="hidden">
<h3>LED</h3>
<button id="ledOff">LED off</button>
<button id="ledConstant">LED Constant</button>
<button id="ledBreathe">LED Breathe</button>
<button id="ledOneshot">LED One Shot</button>
</div>
<div><pre id="log"></pre></div>
</body>
</html>