-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (36 loc) · 1.4 KB
/
index.js
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
function test(event) {
let commandValue = event.target.value.getUint8(0);
write = document.getElementById('value');
write.innerHTML = commandValue;
console.log(commandValue);
}
function onRequestBluetoothDeviceButtonClick() {
const controlServiceUUID = '4fafc201-1fb5-459e-8fcc-c5c9c331914b'; // Full UUID
const commandCharacteristicUUID = 'beb5483e-36e1-4688-b7f5-ea07361b26a8';
navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: [controlServiceUUID]
})
.then(device => {
console.log("Got device name: ", device.name);
console.log("id: ", device.id);
return device.gatt.connect();
})
.then(server => {
// Step 3: Get the Service
serverInstance = server;
console.log("Getting PrimaryService");
return server.getPrimaryService(controlServiceUUID);
})
.then(service => {
console.log("Getting Characteristic");
return service.getCharacteristic(commandCharacteristicUUID);
})
.then (characteristic => {
characteristic.startNotifications();
characteristic.addEventListener('characteristicvaluechanged', test);
})
.catch(function(error) {
console.log("Something went wrong. " + error);
});
}