-
Notifications
You must be signed in to change notification settings - Fork 5
/
xiaomi-lywsd02-set-time.html
55 lines (48 loc) · 1.63 KB
/
xiaomi-lywsd02-set-time.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
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Xiaomi LYWSD02 Set Time</title>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=2">
</head>
<body>
<button id="set">Set Time</button> <span id="status"></span>
<script>
const status = document.getElementById('status');
document.getElementById('set').addEventListener('click', async () => {
const SERVICE_UUID = 'ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6';
const CHARACTERISTIC_UUID = 'ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6';
const options = {
filters: [
{name: 'LYWSD02'},
{name: 'LYWSD02MMC'}
],
optionalServices: [SERVICE_UUID],
};
try {
status.textContent = 'Requesting device...';
const device = await navigator.bluetooth.requestDevice(options);
status.textContent = 'Connecting...';
const server = await device.gatt.connect();
status.textContent = 'Getting service...';
const service = await server.getPrimaryService(SERVICE_UUID);
status.textContent = 'Getting characteristic...';
const characteristic = await service.getCharacteristic(CHARACTERISTIC_UUID);
const buffer = new ArrayBuffer(5);
const view = new DataView(buffer);
const now = new Date();
// First 4 bytes: Unix timestamp (in seconds, little endian)
view.setUint32(0, now.getTime() / 1000, true);
// Last byte: Offset from UTC (in hours)
view.setInt8(4, -now.getTimezoneOffset() / 60);
status.textContent = 'Setting time...';
await characteristic.writeValue(buffer);
server.disconnect();
status.textContent = 'Done.';
} catch (e) {
status.textContent = `${e.name}: ${e.message}`;
}
});
</script>
</body>
</html>