-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbt.js
49 lines (46 loc) · 1.64 KB
/
wbt.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
var characteristic;
var color_service_uuid = "f815e810-456c-6761-746f-4d756e696368";
var color_characteristic_uuid = "f815e811-456c-6761-746f-4d756e696368";
function findLamp() {
try {
console.log("findLamp");
var options = {filters: [{namePrefix: "Avea", services: [color_service_uuid]}], optinalServices: []};
window.navigator.bluetooth.requestDevice(options)
.then(device => device.gatt.connect())
.then(gatt => gatt.getPrimaryService(color_service_uuid))
.then(service => {
console.log('Getting Service...');
console.log('> UUID: ' + service.uuid);
console.log('> Is primary: ' + service.isPrimary);
console.log('')
console.log('Getting Characteristic...');
service.getCharacteristic(color_characteristic_uuid)
.then(async char => {
characteristic = char;
console.log('> Characteristic UUID: ' + characteristic.uuid);
var button = document.getElementById("connectbutton");
subscribeToBulbNotifications();
button.innerHTML = "Connected";
button.style.backgroundColor = "green";
await sleep(500);
await setbrightnessSilderValue();
});
});
} catch(err) {
console.log(err);
alert(err);
}
}
function writeCharacteristic(value) {
if(!characteristic) {
console.log('No characteristic!');
return;
}
try {
characteristic.writeValue(Uint8Array.from(value))
.then(() => console.log("changed value to: "+value));
} catch(err) {
console.log(err);
alert(err);
}
}