Skip to content

Instantly share code, notes, and snippets.

@cotestatnt
Created April 17, 2023 09:19
Show Gist options
  • Save cotestatnt/cf5ca2f2ff62fa130dfe1eb991b8a826 to your computer and use it in GitHub Desktop.
Save cotestatnt/cf5ca2f2ff62fa130dfe1eb991b8a826 to your computer and use it in GitHub Desktop.
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
BLEScan* pBLEScan;
void print_raw_data(const char* data, size_t size)
{
for(uint8_t i=0; i<size; i++) {
Serial.printf("%02X ", data[i]);
}
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
if (advertisedDevice.haveName()) {
if (advertisedDevice.getName() == "Mi 10T Lite") {
// Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
Serial.printf("Device name: %s, ", advertisedDevice.getName().c_str());
Serial.printf(" MAC address: %s, ", advertisedDevice.getAddress().toString().c_str());
// Manufacter data: <company reserved ID (2 bytes LSB first)> + <data>
print_raw_data(advertisedDevice.getManufacturerData().c_str(), advertisedDevice.getManufacturerData().length());
Serial.println();
}
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
static uint32_t scanTime;
if (millis() -scanTime > 1000) {
scanTime = millis();
BLEScanResults foundDevices = pBLEScan->start(1, false);
// Serial.print("Devices found: ");
// Serial.println(foundDevices.getCount());
// Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment