-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbluetooth-hr.js
55 lines (48 loc) · 1.47 KB
/
bluetooth-hr.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
//const noble = require('noble');
const noble = require("@abandonware/noble");
noble.on("discover", peripheral => {
noble.on("scanStop", () => {
console.log("scan stopped");
peripheral.once("connect", () => {
console.log("connected");
peripheral.once("servicesDiscover", services => {
console.log("discovered listener");
console.log(services);
});
// peripheral.discoverServices(["180d"], (error, services) => {
peripheral.discoverSomeServicesAndCharacteristics(
["180d"],
["2a37"],
(error, services, characteristics) => {
console.log("discovered");
if (error) {
console.log("err2", error);
}
console.log(services);
console.log(characteristics);
const hrChar = characteristics[0];
if (hrChar) {
hrChar.on("data", (data, isNotification) => {
console.log("HR data", parseInt(data.toString("hex"), 16));
});
hrChar.subscribe(err => {
if (err) {
console.log("error subscribing to hrchar");
}
});
}
}
);
});
peripheral.connect(error => {
if (error) {
console.log("error");
console.log(error);
}
});
});
console.log(peripheral);
noble.stopScanning();
});
noble.startScanning(["180d"]); // HR services only
//noble.startScanning(); // any service UUID, no duplicates