-
Notifications
You must be signed in to change notification settings - Fork 5
/
lighthouse-v2.html
150 lines (138 loc) · 4.43 KB
/
lighthouse-v2.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html>
<head>
<title>Lighthouse v2</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table id="table">
<tr><th>Name</th><th>Status</th><th>State</th></tr>
</table>
<p>
<button id="add">Add Lighthouse</button>
</p>
<script>
const POWER_SERVICE = '00001523-1212-efde-1523-785feabcd124';
const POWER_CHARACTERISTIC = '00001525-1212-efde-1523-785feabcd124';
const addButton = document.getElementById('add');
const table = document.getElementById('table');
async function addDevice(device, watchAdvertisements) {
const row = document.createElement('tr');
const name = document.createElement('td');
name.textContent = device.name;
row.appendChild(name);
const status = document.createElement('td');
row.appendChild(status);
const state = document.createElement('td');
row.appendChild(state);
const buttonHolder = document.createElement('td');
const button = document.createElement('button');
button.textContent = 'Power on';
button.disabled = true;
buttonHolder.appendChild(button);
row.appendChild(buttonHolder);
table.appendChild(row);
let characteristic = null;
function onValueChanged() {
const value = characteristic.value.getUint8(0);
switch (value) {
case 0x00:
state.textContent = 'Sleeping';
button.disabled = false;
button.textContent = 'Power on';
break;
case 0x01:
case 0x08:
case 0x09:
state.textContent = 'Booting';
button.disabled = true;
break;
case 0x0B:
state.textContent = 'On';
button.disabled = false;
button.textContent = 'Power off';
break;
default:
state.textContent = `Unknown state: ${value}`;
button.disabled = true;
break;
}
}
async function setPowerState(state) {
try {
status.textContent = 'Sending command...';
await characteristic.writeValue(new Uint8Array([state]));
status.textContent = 'Command sent';
} catch (e) {
status.textContent = e;
}
}
button.addEventListener('click', () => {
try {
if (button.textContent == 'Power on') {
setPowerState(1);
}
if (button.textContent == 'Power off') {
setPowerState(0);
}
} catch (e) {
status.textContent = e;
}
});
async function connect() {
try {
status.textContent = 'Connecting...';
const server = await device.gatt.connect();
status.textContent = 'Getting power service...';
const service = await server.getPrimaryService(POWER_SERVICE);
status.textContent = 'Getting power characteristic...';
characteristic = await service.getCharacteristic(POWER_CHARACTERISTIC);
characteristic.addEventListener('characteristicvaluechanged', () => {
onValueChanged(characteristic.value);
});
status.textContent = 'Reading power status...';
await characteristic.readValue();
status.textContent = 'Subscribing to notifications...';
await characteristic.startNotifications();
status.textContent = 'Connected';
} catch (e) {
status.textContent = e;
}
}
if (watchAdvertisements) {
try {
status.textContent = 'Scanning...';
const controller = new AbortController();
device.addEventListener('advertisementreceived', () => {
controller.abort();
connect();
});
await device.watchAdvertisements({signal: controller.signal});
} catch (e) {
status.textContent = e;
}
} else {
connect();
}
}
addButton.addEventListener('click', async () => {
const device = await navigator.bluetooth.requestDevice({
filters: [{ namePrefix: "LHB-" }],
optionalServices: [POWER_SERVICE],
});
addDevice(device, false);
});
if (navigator.bluetooth.getDevices) {
navigator.bluetooth.getDevices().then((devices) => {
for (const device of devices) {
addDevice(device, true);
}
});
}
</script>
</body>
</html>