-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_ble.html
executable file
·128 lines (106 loc) · 4.66 KB
/
index_ble.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
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<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:';
}
})();
// Add a global error event listener early on in the page load, to help ensure that browsers
// which don't support specific functionality still end up displaying a meaningful message.
window.addEventListener('error', function(error) {
if (ChromeSamples && ChromeSamples.setStatus) {
ChromeSamples.setStatus(error.message + ' (Your browser may not support this feature.)');
error.preventDefault();
}
});
</script>
</head>
<body>
<form>
<input id="service" type="text" autofocus placeholder="Bluetooth Service">
<button>Get characteristics</button>
</form>
<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>Live Output</h3>
<div id="output" class="output">
<div id="content"></div>
<div id="status"></div>
<pre id="log"></pre>
</div>
<script>function onFormSubmit() {
'use strict';
var serviceUuid = document.getElementById('service').value;
var i;
if (serviceUuid.startsWith('0x')) {
serviceUuid = parseInt(serviceUuid, 16);
}
log("Service chosen!");
navigator.bluetooth.requestDevice({
filters: [{services: [serviceUuid]}]
}).then(device => device.connectGATT())
.then(server => server.getPrimaryService('6c372000-d2cc-11e4-9a1f-0002a5d5c51b'))
.then(service => service.getCharacteristic('6c372001-d2cc-11e4-9a1f-0002a5d5c51b'))
.then(characteristic => characteristic.readValue())
.catch(error => {
log('Argh! ' + error);
});
};
</script>
<script>
document.querySelector('form').addEventListener('submit', function(event) {
event.stopPropagation();
event.preventDefault();
if (isWebBluetoothEnabled()) {
ChromeSamples.clearLog();
onFormSubmit();
}
});
</script>
<script>
log = ChromeSamples.log;
function isWebBluetoothEnabled() {
if (navigator.bluetooth) {
return true;
} else {
ChromeSamples.setStatus('Web Bluetooth API is not available.\n' +
'Please make sure the Web Bluetooth flag is enabled.');
return false;
}
}
</script>
</body>
</html>