Advertisement
Guest User

esp32 ble scan

a guest
May 27th, 2018
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. /*
  2.    Based on Neil Kolban example for IDF:
  3.    https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
  4.    Ported to Arduino ESP32 by Evandro Copercini
  5. */
  6.  
  7. #include <Arduino.h>
  8. #include <sstream>
  9.  
  10. #include <BLEAdvertisedDevice.h>
  11. #include <BLEDevice.h>
  12. #include <BLEScan.h>
  13. #include <BLEUtils.h>
  14.  
  15. std::stringstream ss;
  16. BLEScan *pBLEScan;
  17.  
  18. #ifndef LED_BUILTIN
  19. // Set LED_BUILTIN if it is not defined by Arduino framework
  20. #define LED_BUILTIN 2
  21. #endif
  22.  
  23. int scanTime = 30; // In seconds
  24.  
  25. class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
  26.   void onResult(BLEAdvertisedDevice advertisedDevice) {
  27.     Serial.printf("Advertised Device: %s \n",
  28.                   advertisedDevice.toString().c_str());
  29.   }
  30. };
  31.  
  32. void setup() {
  33.   Serial.begin(57600);
  34.   Serial.setDebugOutput(true);
  35.   Serial.println("Start Scanning...");
  36.  
  37.   pinMode(LED_BUILTIN, OUTPUT);
  38.   digitalWrite(LED_BUILTIN, HIGH);
  39.  
  40.   BLEDevice::init("");
  41.   // BLEScan *pBLEScan = BLEDevice::getScan(); // create new scan
  42.   pBLEScan = BLEDevice::getScan(); // create new scan
  43.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  44.   pBLEScan->setActiveScan(
  45.       true); // active scan uses more power, but get results faster
  46.   pBLEScan->setInterval(0x50);
  47.   pBLEScan->setWindow(0x30);
  48. }
  49.  
  50. void loop() {
  51.  
  52.   Serial.printf("\r\nNew scan start at %lu\r\n", millis());
  53.  
  54.   BLEScanResults foundDevices = pBLEScan->start(scanTime);
  55.   Serial.print("Devices found: ");
  56.   Serial.println(foundDevices.getCount());
  57.   Serial.println("Scan done!");
  58.  
  59.   int count = foundDevices.getCount();
  60.   ss.str("");
  61.   ss.clear();
  62.   ss << "[";
  63.   for (int i = 0; i < count; i++) {
  64.     if (i > 0) {
  65.       ss << ",";
  66.     }
  67.     BLEAdvertisedDevice d = foundDevices.getDevice(i);
  68.     ss << "{\"Address\":\"" << d.getAddress().toString()
  69.        << "\",\"Rssi\":" << d.getRSSI();
  70.  
  71.     if (d.haveName()) {
  72.       ss << ",\"Name\":\"" << d.getName() << "\"";
  73.     }
  74.  
  75.     if (d.haveAppearance()) {
  76.       ss << ",\"Appearance\":" << d.getAppearance();
  77.     }
  78.  
  79.     if (d.haveManufacturerData()) {
  80.       std::string md = d.getManufacturerData();
  81.       uint8_t *mdp = (uint8_t *)d.getManufacturerData().data();
  82.       char *pHex = BLEUtils::buildHexData(nullptr, mdp, md.length());
  83.       ss << ",\"ManufacturerData\":\"" << pHex << "\"";
  84.       free(pHex);
  85.     }
  86.  
  87.     if (d.haveServiceUUID()) {
  88.       ss << ",\"ServiceUUID\":\"" << d.getServiceUUID().toString() << "\"";
  89.     }
  90.  
  91.     if (d.haveTXPower()) {
  92.       ss << ",\"TxPower\":" << (int)d.getTXPower();
  93.     }
  94.  
  95.     ss << "}";
  96.   }
  97.   ss << "]";
  98.  
  99.   Serial.println(String(ss.str().c_str()));
  100.  
  101.   digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  102.   delay(2000);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement