-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (49 loc) · 1.5 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
const noble = require("@abandonware/noble");
const serviceId = "ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6";
const characteristicId = "ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6";
const device = "e7-2e-00-33-8c-6d";
const toBuffer = (date, offset) => {
const buf1 = Buffer.alloc(5);
buf1.writeInt32LE(date.getTime() / 1000, 0);
buf1.writeInt8(offset, 4);
return buf1;
};
noble.on("stateChange", async (state) => {
if (state === "poweredOn") {
console.log("scanning");
await noble.startScanningAsync(
[
serviceId,
"0xfe95",
// "0x1f10",
],
false
);
}
});
noble.on("discover", async (peripheral) => {
await peripheral.connectAsync();
const {
services,
characteristics,
} = await peripheral.discoverSomeServicesAndCharacteristicsAsync(
[serviceId],
[characteristicId]
);
const b = await characteristics[0].readAsync();
console.log(peripheral.advertisement.localName, peripheral.address, b);
if (b.length === 5) {
const time = new Date(1000 * b.readInt32LE(0));
const offset = b.readInt8(4);
console.log(`Got time ${time} and offset ${offset}`);
} else if (b.length === 4) {
const time = new Date(1000 * b.readInt32LE(0));
console.log(`Got time ${time}`);
}
const now = new Date();
const offset = Math.abs(now.getTimezoneOffset() / 60);
const buffer = toBuffer(now, offset);
console.log("Writing new time", now, buffer);
await characteristics[0].writeAsync(buffer, false);
await peripheral.disconnectAsync();
});