-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresence.ino
69 lines (58 loc) · 1.6 KB
/
presence.ino
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
/*
* Presence
*/
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice Device) {
// Serial.print("BLE Advertised device found: ");
// Serial.println(Device.toString().c_str());
pServerAddress = new BLEAddress(Device.getAddress());
for (int i = 0; i < (sizeof(knownMACs) / sizeof(knownMACs[0])); i++) {
if (strcmp(pServerAddress->toString().c_str(), knownMACs[i].c_str()) == 0) {
if (Device.getRSSI() > minRSSI) {
deviceFound = true;
Device.getScan()->stop();
break;
}
}
}
}
};
void setupPresence() {
BLEDevice::init("Ghia");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
delay(100);
DEBUG_PRINTLN("Done initializing BLE for presence");
}
void hello() {
recentPresence = true;
Serial.println("Running hello");
/// code here...
}
void goodbye() {
recentPresence = false;
Serial.println("Running goodbye");
/// code here...
}
void bluetooth() {
unsigned long scan_timer;
Serial.println();
Serial.println("Scanning for 60 seconds (or until found)...");
deviceFound = false;
scan_timer = millis();
BLEScanResults scanResults = pBLEScan->start(60);
if (deviceFound) {
Serial.print("Found known device in ");
Serial.print((millis() - scan_timer)/1000);
Serial.println(" seconds");
if (!recentPresence) {
hello();
}
} else {
Serial.println("No known devices found");
if (recentPresence) {
goodbye();
}
}
}