-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
64 lines (53 loc) · 1.55 KB
/
main.cpp
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
// Bricks
#include <Bricks.Brick.h>
#include <Bricks.Inbox.h>
#include <Bricks.Outbox.h>
using namespace Bricks;
// Local
#include <NimBLEDevice.h>
BLEScan *pBLEScan;
const int8_t MINIMUM_RSSI = -80;
class BLECallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice* device) {
if(device->getRSSI() > MINIMUM_RSSI) {
char value[100];
sprintf(value, "%s,%s,%d", device->getName().c_str(), device->getAddress().toString().c_str(), device->getRSSI());
gOutbox.send("found", value);
}
}
};
void scan(const uint8_t scanTime, const bool activeScan = false) {
Log.notice("BLES: Scanning for %d second(s)" CR, scanTime);
pBLEScan->setActiveScan(activeScan);
pBLEScan->start(scanTime, false);
pBLEScan->clearResults();
Log.notice("BLES: Scan done" CR);
}
void passiveScan(BRICKS_CALLBACK_SIGNATURE) {
const uint8_t scanTime = atoi(message.value);
scan(scanTime, false);
}
// active scan required to get names?
void activeScan(BRICKS_CALLBACK_SIGNATURE) {
const uint8_t scanTime = atoi(message.value);
scan(scanTime, true);
}
//Main
void setup() {
// Logging
Serial.begin(115200);
Log.begin(LOG_LEVEL_NOTICE, &Serial);
// Configure BLE
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new BLECallbacks());
// Configure ESPNOW
gBrick.init();
// Enable receiving messages
gInbox.init("BLE Scanner");
gInbox.skills[0] = new Skill("scan", &passiveScan);
gInbox.skills[1] = new Skill("activeScan", &activeScan);
}
void loop() {
gInbox.loop();
}