-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathble-shelly-scanner.js
69 lines (57 loc) · 1.94 KB
/
ble-shelly-scanner.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
/**
* This script uses the BLE scan functionality in scripting
* Will look for Shelly BLU devices fingerprints in BLE advertisements
* Prints device name and address
*/
// Shelly BLU devices:
// SBBT - Shelly BLU Button
// SBDW - Shelly BLU DoorWindow
let ALLTERCO_DEVICE_NAME_PREFIX = ["SBBT", "SBDW", "SBMO"];
let ALLTERCO_MFD_ID_STR = "0ba9";
let BTHOME_SVC_ID_STR = "fcd2";
const SCAN_PARAM_WANT = {
duration_ms: BLE.Scanner.INFINITE_SCAN,
active: true,
}
let SHELLY_BLU_CACHE = {};
function scanCB(ev, res) {
if (ev !== BLE.Scanner.SCAN_RESULT) return;
// skip if there is no service_data member
if (typeof res.service_data === 'undefined' || typeof res.service_data[BTHOME_SVC_ID_STR] === 'undefined') return;
// skip if we have already found this device
if (typeof SHELLY_BLU_CACHE[res.addr] !== 'undefined') return;
if (typeof res.local_name !== 'string') return;
let shellyBluNameIdx = 0;
for (shellyBluNameIdx in ALLTERCO_DEVICE_NAME_PREFIX) {
if (res.local_name.indexOf(ALLTERCO_DEVICE_NAME_PREFIX[shellyBluNameIdx]) === 0) {
console.log('New device found:');
console.log('Address: ', res.addr, ' Name: ', res.local_name);
SHELLY_BLU_CACHE[res.addr] = res.local_name;
}
}
}
function init() {
// get the config of ble component
const BLEConfig = Shelly.getComponentConfig("ble");
// exit if the BLE isn't enabled
if (!BLEConfig.enable) {
console.log(
"Error: The Bluetooth is not enabled, please enable it from settings"
);
return;
}
// check if the scanner is already running
if (BLE.Scanner.isRunning()) {
console.log("Info: The BLE gateway is running, the BLE scan configuration is managed by the device");
}
else {
// start the scanner
const bleScanner = BLE.Scanner.Start(SCAN_PARAM_WANT);
if (!bleScanner) {
console.log("Error: Can not start new scanner");
}
}
// subscribe a callback to BLE scanner
BLE.Scanner.Subscribe(scanCB);
}
init();