Showing 2 changed files with 172 additions and 0 deletions
+80
BLEBeaconScan.ino
... ...
@@ -0,0 +1,80 @@
1
+/*
2
+   Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
3
+   Ported to Arduino ESP32 by Evandro Copercini
4
+*/
5
+
6
+#include <Arduino.h>
7
+#include <sstream>
8
+
9
+#include <BLEDevice.h>
10
+#include <BLEUtils.h>
11
+#include <BLEScan.h>
12
+#include <BLEAdvertisedDevice.h>
13
+
14
+#include "soc/soc.h"
15
+#include "soc/rtc_cntl_reg.h"
16
+
17
+#include "serial.h"
18
+
19
+class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
20
+{
21
+    void onResult(BLEAdvertisedDevice advertisedDevice)
22
+    {
23
+      uint8_t process_entry = 0;
24
+      if( true == gNoFilter ) {
25
+        process_entry = 1;
26
+      } else if( 0 == strncmp( advertisedDevice.getAddress().toString().c_str(), gFilter, strlen(gFilter) ) ) {
27
+        process_entry = 1;
28
+      }
29
+      if(0 != process_entry) {
30
+        Serial.printf("MAC Address      : %s\r\n", advertisedDevice.getAddress().toString().c_str());
31
+        {
32
+          uint8_t* mdp = (uint8_t*)advertisedDevice.getPayload();
33
+          char *pHex = BLEUtils::buildHexData(nullptr, mdp, advertisedDevice.getPayloadLength());
34
+          Serial.printf("Payload          : %s\r\n",pHex);
35
+          free(pHex);
36
+        }
37
+        if (advertisedDevice.haveManufacturerData()) {
38
+          std::string md = advertisedDevice.getManufacturerData();
39
+          uint8_t* mdp = (uint8_t*)advertisedDevice.getManufacturerData().data();
40
+          char *pHex = BLEUtils::buildHexData(nullptr, mdp, md.length());
41
+          Serial.printf("ManufacturerData : %s\r\n", pHex);
42
+          free(pHex);
43
+        }
44
+        if (advertisedDevice.haveServiceUUID()) {
45
+          Serial.printf("Service UUID     : %s\r\n", advertisedDevice.getServiceUUID().toString().c_str());
46
+        }
47
+        Serial.printf("--------------------------------------------------\r\n");
48
+      }
49
+      pollSerial();
50
+    }
51
+};
52
+
53
+void setup()
54
+{
55
+  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
56
+  Serial.begin(115200);
57
+  Serial.println("ESP32 BLE Scanner");
58
+  BLEDevice::init("");
59
+}
60
+
61
+void loop()
62
+{
63
+  // put your main code here, to run repeatedly:
64
+  BLEScan *pBLEScan = BLEDevice::getScan(); //create new scan
65
+  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
66
+  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
67
+  pBLEScan->setInterval(gInterval);
68
+  pBLEScan->setWindow(gWindow);
69
+  Serial.printf("Start BLE scan for %d seconds...\r\n", gScanTime);
70
+  Serial.printf("setInterval %d\r\n", gInterval);
71
+  Serial.printf("setWindow %d\r\n", gWindow);
72
+  if( 0 != strcmp(gFilter,"none")) {
73
+    Serial.printf("Filter on [%s]...\r\n", gFilter);
74
+  } else {
75
+    Serial.printf("No Filter\r\n");
76
+  }
77
+  BLEScanResults foundDevices = pBLEScan->start(gScanTime);
78
+
79
+  pollSerial();
80
+}
+92
serial.h
... ...
@@ -0,0 +1,92 @@
1
+#ifndef _SERIAL_H
2
+#define _SERIAL_H
3
+
4
+#define SCAN_TIME  10 // seconds
5
+#define SCAN_TIME_MIN  2 // seconds
6
+#define WINDOW_TIME_MIN  192 // seconds
7
+#define INTERVAL_TIME_MIN  200 // seconds
8
+
9
+uint8_t gScanTime = SCAN_TIME;
10
+uint8_t gNoFilter = true;
11
+uint16_t gWindow = WINDOW_TIME_MIN;
12
+uint16_t gInterval = INTERVAL_TIME_MIN;
13
+char gFilter[18] = "";
14
+
15
+#define SERIAL_MAX_LINES 32
16
+void serialHelp() {
17
+  Serial.println("Available commands: ");
18
+  Serial.println("- help: Display this screen");
19
+  Serial.println("- scan XX : Set scan time to XX (default: 10s)");
20
+  Serial.println("- window XX : Set scan window to XX (default: 192)");
21
+  Serial.println("- interval XX : Set scan window to XX (default: 200)");
22
+  Serial.println("- filter none : remove all filters");
23
+  Serial.println("- filter 00:11:22:33:44:55 : filter only on 00:11:22:33:44:55 (works on prefix too)");
24
+}
25
+
26
+void serialFilter(char *newFilter) {
27
+  String serialOut = "serialFilter() : ";
28
+  memset(gFilter,0,18);
29
+  strncpy(gFilter,newFilter,strlen(newFilter));
30
+  serialOut += gFilter;
31
+  if( 0 == strcmp(newFilter,"none") ) {
32
+    gNoFilter = true;
33
+  } else {
34
+    gNoFilter = false;
35
+  }
36
+  Serial.println(serialOut);
37
+}
38
+
39
+void serialScanTime(uint8_t newTime) {
40
+  String serialOut = "{\"command\": \"scan\", \"status\": { ";
41
+  serialOut += "\"value\": \""+String(newTime)+"\"";
42
+  serialOut += "}";
43
+  gScanTime = newTime;
44
+  Serial.println(serialOut);
45
+}
46
+
47
+void serialWindow(uint16_t newTime) {
48
+  String serialOut = "{\"command\": \"window\", \"status\": { ";
49
+  serialOut += "\"value\": \""+String(newTime)+"\"";
50
+  serialOut += "}";
51
+  gWindow = newTime;
52
+  Serial.println(serialOut);
53
+}
54
+
55
+void serialInterval(uint16_t newTime) {
56
+  String serialOut = "{\"command\": \"interval\", \"status\": { ";
57
+  serialOut += "\"value\": \""+String(newTime)+"\"";
58
+  serialOut += "}";
59
+  gInterval = newTime;
60
+  Serial.println(serialOut);
61
+}
62
+
63
+void pollSerial() {
64
+  if (Serial.available() > 0) {
65
+    char command[SERIAL_MAX_LINES];
66
+    int sizeCommand = Serial.readBytesUntil('\r', command, sizeof(command) / sizeof(char) );
67
+    command[sizeCommand]='\0';
68
+    if(command[sizeCommand-1]=='\n') command[sizeCommand-1]='\0';
69
+    Serial.println("[CMD] received: "+String(command));
70
+    if( 0 == strcmp(command,"help") ) {
71
+      serialHelp();
72
+    } else if( 0 == strncmp(command,"filter",6) ) {
73
+      char *newFilter = (command+7);
74
+      serialFilter(newFilter);
75
+    } else if( 0 == strncmp(command,"scan",4) ) {
76
+      int newTime = atoi((command+5));
77
+      if( newTime < SCAN_TIME_MIN ) newTime = SCAN_TIME_MIN;
78
+      serialScanTime(newTime);
79
+    } else if( 0 == strncmp(command,"window",6) ) {
80
+      uint16_t newTime = atoi(&command[7]);
81
+      if( newTime < WINDOW_TIME_MIN ) newTime = WINDOW_TIME_MIN;
82
+      serialWindow(newTime);
83
+    } else if( 0 == strncmp(command,"interval",8) ) {
84
+      uint16_t newTime = atoi(&command[9]);
85
+      if( newTime < INTERVAL_TIME_MIN ) newTime = INTERVAL_TIME_MIN;
86
+      serialInterval(newTime);
87
+    } else {
88
+      Serial.println("{\"command\": \""+String(command)+"\", \"status\": \"error\" }");
89
+    }
90
+  }
91
+}
92
+#endif //_SERIAL_H