-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbluetoothtest.html
More file actions
203 lines (169 loc) · 6.06 KB
/
bluetoothtest.html
File metadata and controls
203 lines (169 loc) · 6.06 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
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
<script>
(async () => {
try {
const isBluetoothAvailable = await navigator.bluetooth.getAvailability();
console.log(`> Bluetooth is ${isBluetoothAvailable ? 'available' : 'unavailable'}`);
} catch(error) {
console.log('Argh! ' + error);
}
})();
if ('onavailabilitychanged' in navigator.bluetooth) {
navigator.bluetooth.addEventListener('availabilitychanged', function(event) {
console.log(`> Bluetooth is ${event.value ? 'available' : 'unavailable'}`);
});
}
function doit() {
console.log('Requesting any Bluetooth Device...');
navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true,
optionalServices: ['d9d9e9e0-aa4e-4797-8151-cb41cedaf2ad']})
.then(device => {
console.log('device:', device)
console.log('Connecting to GATT Server...');
return device.gatt.connect();
})
.then(server => {
console.log('server:', server)
console.log('Getting Device Information Service...');
return server.getPrimaryService('d9d9e9e0-aa4e-4797-8151-cb41cedaf2ad');
// return server.getPrimaryServices();
})
.then(async service => {
console.log('Getting Characteristics...');
console.log('service:', service)
// for (const service of services) {
// console.log('> Service: ' + service.uuid);
const characteristic = await service.getCharacteristic("d9d9e9e1-aa4e-4797-8151-cb41cedaf2ad");
// const characteristics = await service.getCharacteristics();
// // const characteristics = service.getCharacteristics();
// console.log('characteristics:' + characteristics);
// mycharacteristic = null;
// characteristics.forEach(characteristic => {
// console.log('>> Characteristic: ' + characteristic.uuid + ' ' +
// getSupportedProperties(characteristic));
// console.log('characteristic.uuid:', characteristic.uuid)
// if (characteristic.uuid == "d9d9e9e1-aa4e-4797-8151-cb41cedaf2ad") {
// console.log('yep!')
// mycharacteristic = characteristic;
// }
// });
return characteristic;
// }
// return service.getCharacteristics();
})
.then(async characteristic => {
console.log('characteristic:', characteristic)
console.log('Getting Descriptor...');
// myDescriptor = await characteristic.getDescriptor('gatt.characteristic_user_description');
myDescriptor = await characteristic.getDescriptor('00002901-0000-1000-8000-00805f9b34fb');
// myDescriptor = await characteristic.getDescriptor('Bitsnap Control');
console.log('Descriptor:', myDescriptor)
// let encoder = new TextEncoder('utf-8');
let value = "1E0115A364" // beep
try {
console.log('Setting Characteristic User Description...');
// await myDescriptor.writeValue(encoder.encode(value));
// value = hexDecode(value)
console.log('initial value:', value)
value = hexToBytes(value)
console.log('bytes:', value)
// newArray = new Uint8Array
value = new Uint8Array(value).buffer
// value = value.buffer
// debugger
console.log('writing', value)
// await myDescriptor.writeValue(value);
// await myDescriptor.writeValueWithResponse(value).then(res => {
await characteristic.writeValueWithResponse(value).then(res => {
console.log('response:', res)
})
// console.log('> Characteristic User Description changed to: ' + value);
} catch(error) {
console.log('Argh! ' + error);
}
})
// .catch(error => {
// console.log('Argh! ' + trace(error));
// });
}
/* Utils */
function padHex(value) {
return ('00' + value.toString(16).toUpperCase()).slice(-2);
}
function getSupportedProperties(characteristic) {
let supportedProperties = [];
for (const p in characteristic.properties) {
if (characteristic.properties[p] === true) {
supportedProperties.push(p.toUpperCase());
}
}
return '[' + supportedProperties.join(', ') + ']';
}
function onGetBluetoothDevicesButtonClick() {
console.log('Getting existing permitted Bluetooth devices...');
navigator.bluetooth.getDevices()
.then(devices => {
console.log('> Got ' + devices.length + ' Bluetooth devices.');
for (const device of devices) {
console.log(' > ' + device.name + ' (' + device.id + ')');
}
})
.catch(error => {
console.log('Argh! ' + error);
});
}
function onRequestBluetoothDeviceButtonClick() {
console.log('Requesting any Bluetooth device...');
navigator.bluetooth.requestDevice({
// filters: [...] <- Prefer filters to save energy & show relevant devices.
acceptAllDevices: true
})
.then(device => {
console.log('> Requested ' + device.name + ' (' + device.id + ')');
})
.catch(error => {
console.log('Argh! ' + error);
});
}
function hexEncode(input) {
var hex, i;
var result = "";
for (i=0; i<input.length; i++) {
hex = input.charCodeAt(i).toString(16);
result += ("0"+hex).slice(-4);
}
return result
}
function hexDecode(input) {
var j;
var hexes = input.match(/.{1,2}/g) || [];
var back = new Uint8Array(hexes.length/2);
console.log('new bytearray length', back.length)
// debugger
for(j = 0; j<hexes.length; j++) {
back[j] = parseInt(hexes[j], 16);
// back[j] = "\\u"+hexes[j];
console.log(hexes[j], back[j])
}
return back;
}
// Convert a hex string to a byte array
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
// Convert a byte array to a hex string
function bytesToHex(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
var current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
hex.push((current >>> 4).toString(16));
hex.push((current & 0xF).toString(16));
}
return hex.join("");
}
</script>
<button onclick="onGetBluetoothDevicesButtonClick()">get devices</button>
<button onclick="onRequestBluetoothDeviceButtonClick()">request device</button>
<button onclick="doit()">do it</button>