-
Notifications
You must be signed in to change notification settings - Fork 5
/
notification.html
353 lines (262 loc) · 8.83 KB
/
notification.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Notification</title>
<style>
@import url(https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,500,700,300);
body {
font-family: "Roboto";
}
div {
margin: 10px;
}
h1, h3 {
text-align: center;
}
ul {
margin-top: 10px;
margin-bottom: 30px;
font-size: 14px;
}
label {
display: inline-block;
width: 120px;
font-weight: 500;
}
input[type="text"] {
width: 220px;
padding: 5px;
}
button {
padding: 10px;
margin-right: 30px;
border: 1px solid white;
font-weight: 700;
box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.1);
}
#main {
max-width: 500px;
margin: 20px auto;
border: 1px solid #DDD;
padding: 20px;
box-shadow: 0px 0px 5px 1px rgba(0, 0, 0, 0.1);
}
.button-container {
margin: 0 auto;
width: 300px;
text-align: center;
}
#ledon, #ledoff {
color: white;
margin-top: 20px;
margin-bottom: 20px;
}
#ledon {
background-color: #006600;
}
#ledoff {
background-color: #cc0000;
}
#signal-box {
margin: 20px auto;
width: 100px;
border: 1px solid #EEE;
font-size: 40px;
font-weight: 700;
text-align: center;
padding: 20px;
vertical-align: middle;
}
</style>
<script>
// If we're running on a real web server (as opposed to localhost, which is whitelisted),
// then change the protocol to HTTPS.
// See https://goo.gl/lq4gCo for an explanation as to why this is needed for some features.
(function() {
var isLocalhost = !!(window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/));
if (window.location.protocol === 'http:' && !isLocalhost) {
// Redirect to https: if we're currently using http: and we're not on localhost.
window.location.protocol = 'https:';
}
})();
window.addEventListener('error', function(error) {
if (ChromeSamples && ChromeSamples.setStatus) {
ChromeSamples.setStatus(error.message + ' (Nettleseren din støtter ikke Web Bluetooth.)');
error.preventDefault();
}
});
</script>
</head>
<body>
<div id="main">
<h1>Notification-test</h1>
For å bruke Web Bluetooth til å telle antall knappetrykk på kitet:
<ul>
<li>Last inn "Experimental-ble-app-blinky" på kitet</li>
<li>Åpne denne nettsiden i Chrome Dev eller Chromium på Android L/M</li>
</ul>
<form>
<div><label for="service">Service: </label><input id="service" type="text" list="services" autofocus placeholder="Bluetooth Service" value="00001523-1212-efde-1523-785feabcd123"></div>
<div><label for="service">Button-char.: </label><input id="button-characteristic" type="text" value="00001524-1212-efde-1523-785feabcd123"></div>
<div><label for="service">LED-char.: </label><input id="led-characteristic" type="text" value="00001525-1212-efde-1523-785feabcd123"></div>
<div><button id="connect" >Koble til enhet</button></div>
</form>
<div class="button-container">
<button id="ledon" onclick="ledOn()">Slå LED PÅ</button>
<button id="ledoff" onclick="ledOff()">Slå LED AV</button>
</div>
<div id="signal-box">
</div>
<script>
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 = '';
},
setStatus: function(status) {
document.querySelector('#status').textContent = status;
},
setContent: function(newContent) {
var content = document.querySelector('#content');
while(content.hasChildNodes()) {
content.removeChild(content.lastChild);
}
content.appendChild(newContent);
}
};
</script>
<h3>Logg</h3>
<div id="output" class="output">
<div id="content"></div>
<div id="status"></div>
<pre id="log"></pre>
</div>
<!-- // END OF MAIN -->
</div>
<script>
'use strict';
var customService = null;
var my_button_characteristic;
var my_led_characteristic;
var charVal = new Uint8Array(20);
function onFormSubmit() {
let serviceUuid = document.getElementById('service').value;
if (serviceUuid.startsWith('0x')) {
serviceUuid = parseInt(serviceUuid, 16);
}
let button_characteristicUuid = document.getElementById('button-characteristic').value;
if (button_characteristicUuid.startsWith('0x')) {
button_characteristicUuid = parseInt(button_characteristicUuid, 16);
}
let led_characteristicUuid = document.getElementById('led-characteristic').value;
if (led_characteristicUuid.startsWith('0x')) {
led_characteristicUuid = parseInt(led_characteristicUuid, 16);
}
log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice({filters: [{services: [serviceUuid]}]})
.then(device => device.connectGATT())
.then(server => server.getPrimaryService(serviceUuid))
/*.then(service =>
service.getCharacteristic(button_characteristicUuid))
.then(button_characteristic => {
my_button_characteristic = button_characteristic;
return my_button_characteristic.startNotifications().then(() => {
log('> Tilkobling oppretta');
my_button_characteristic.addEventListener('characteristicvaluechanged',
handleNotifications);
});
})
*/
.then(service => {
customService = service;
return Promise.all([
service.getCharacteristic(button_characteristicUuid)
.then(handleButtonCharacteristic),
service.getCharacteristic(led_characteristicUuid))
.then(handleLedCharacteristic => {
my_led_characteristic = led_characteristic;
log('> Tilkoblet enhet. LED kan nå styres.');
})
]);
});
.catch(error => {
log('Argh! ' + error);
});
}
/*
.then(service => service.getCharacteristic(led_characteristicUuid))
.then(led_characteristic => {
my_led_characteristic = led_characteristic;
log('> Tilkoblet enhet. LED kan nå styres.');
})*/
function handleButtonCharacteristic(characteristic) {
characteristic.addEventListener('characteristicvaluechanged', onButtonChange);
return characteristic.startNotifications();
}
/*
function (event) {
let characteristic = event.target;
console.log(parseHeartRate(characteristic.value));
}*/
var count = 0;
function onButtonChange(event) {
count++;
document.getElementById("signal-box").innerHTML = Math.floor(count/2);
let value = event.target.value;
// In Chrome 50+, a DataView is returned instead of an ArrayBuffer.
value = value.buffer ? value : new DataView(value);
let a = [];
// Convert raw data bytes to hex values just for the sake of showing something.
// In the "real" world, you'd use data.getUint8, data.getUint16 or even
// TextDecoder to process raw data bytes.
for (var i = 0; i < value.byteLength; i++) {
a.push(('Event nr. ' + count + ': 00' + value.getUint8(i).toString(16)).slice(-2));
}
log('> ' + a.join(''));
}
// Funksjon for å skru LED på
function ledOn() {
'use strict';
if(my_led_charateristic) {
log('> Skrur LED på...');
let ledValue = new Uint8Array([1], [240]);
my_led_charateristic.writeValue(ledValue);
}
};
// Funksjon for å skru LED av
function ledOff() {
'use strict';
if(my_led_charateristic) {
log('> Skrur LED av...');
let ledValue = new Uint8Array([0], [0]);
my_led_charateristic.writeValue(ledValue);
}
};
</script>
<script>
document.querySelector('form').addEventListener('submit', function(event) {
event.stopPropagation();
event.preventDefault();
if (!navigator.bluetooth) {
ChromeSamples.setStatus('Web Bluetooth API er ikke tilgjengelig.\n' +
'Husk å aktivere Web Bluetooth-flagget: chrome://flags');
} else {
ChromeSamples.clearLog();
onFormSubmit();
}
});
log = ChromeSamples.log;
</script>
</body>
</html>