-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (82 loc) · 3.03 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
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
if (!navigator.bluetooth) {
alert('Este browser nao tem suporte para Web Bluetooth');
}
const MY_BLUETOOTH_NAME = 'BT05';
const SEND_SERVICE = 0xFFE0;
const SEND_SERVICE_CHARACTERISTIC = 0xFFE1;
const controlButtonsListElements = document.querySelectorAll('.control-buttons > li');
const connectButton = document.getElementById('connectButton');
const disconnectButton = document.getElementById('disconnectButton');
const lightOffButton = document.getElementById('lightOff');
const toggleRedLightButton = document.getElementById('toggleRedLight');
const toggleBlueLightButton = document.getElementById('toggleBlueLight');
const toggleGreenLightButton = document.getElementById('toggleGreenLight');
const runBlinkLightButton = document.getElementById('runBlinkLight');
let toggleLigthCharacteristic;
let myDevice;
connectButton.addEventListener('pointerup', connectButtonPointerUpHandler);
function connectButtonPointerUpHandler() {
navigator.bluetooth.requestDevice({
//filters:
// [
// name: MY_BLUETOOTH_NAME
// //{ name: MY_BLUETOOTH_NAME },
// //{ services: [SEND_SERVICE] },
// ],
acceptAllDevices:true
})
/*
.then(device => {
myDevice = device;
return device.gatt.connect();
})
.then(server => server.getPrimaryService(SEND_SERVICE))
.then(service => service.getCharacteristic(SEND_SERVICE_CHARACTERISTIC))
.then(characteristic => {
toggleLigthCharacteristic = characteristic;
toggleButtonsVisible();
toggleItemsEventListeners('addEventListener');
})
.catch(error => {
console.error(error);
});
*/
}
function lightOffButtonClickHandler() {
return toggleLigthCharacteristic.writeValue(Uint8Array.of(0));
}
function toggleLightButtonClickHandler(event) {
const code = Number(event.target.dataset.code);
if (code === 1) {
toggleLigthCharacteristic.writeValue(Uint8Array.of(code));
return;
}
toggleLigthCharacteristic.readValue()
.then(currentCode => {
const convertedCode = currentCode.getUint8(0);
toggleLigthCharacteristic.writeValue(Uint8Array.of(convertedCode === code ? 0 : code));
});
}
function toggleButtonsVisible() {
Array.prototype.forEach.call(controlButtonsListElements, listElement => {
listElement.classList.toggle('visible');
});
}
function disconnectButtonClickHandler() {
lightOffButtonClickHandler()
.then( () => {
myDevice.gatt.disconnect();
toggleItemsEventListeners('removeEventListener');
toggleButtonsVisible();
toggleLigthCharacteristic = undefined;
myDevice = undefined;
});
}
function toggleItemsEventListeners(action) {
disconnectButton[action]('click', disconnectButtonClickHandler);
lightOffButton[action]('click', lightOffButtonClickHandler);
runBlinkLightButton[action]('click', toggleLightButtonClickHandler);
toggleGreenLightButton[action]('click', toggleLightButtonClickHandler);
toggleRedLightButton[action]('click', toggleLightButtonClickHandler);
toggleBlueLightButton[action]('click', toggleLightButtonClickHandler);
}