Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

86 lines (70 sloc) 2.7 KB
/*
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 <Arduino.h>
#include <sstream>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#define LED_ONBOARD 22
/* Duration of BLE scan
============== ============== ==============
= WINDOW = ===INTERVAL=== = WINDOW = ===INTERVAL=== = WINDOW =
============== ============== ==============
===============================================================================
= SCAN TIME =
===============================================================================
*/
#define SCAN_TIME 1 // seconds
#define INTERVAL_TIME 200 // (mSecs)
#define WINDOW_TIME 100 // less or equal setInterval value
BLEScan* pBLEScan;
String deviceName;
String deviceAddress;
int16_t deviceRSSI;
uint16_t countDevice;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
/* unComment when you want to see devices found */
Serial.printf("Found device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup() {
Serial.begin(115200);
Serial.println("BLEDevice init...");
pinMode(LED_ONBOARD, OUTPUT);
digitalWrite(LED_ONBOARD, LOW);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(INTERVAL_TIME); // Set the interval to scan (mSecs)
pBLEScan->setWindow(WINDOW_TIME); // less or equal setInterval value
}
void loop() {
BLEScanResults foundDevices = pBLEScan->start(SCAN_TIME);
int count = foundDevices.getCount();
for (int i = 0; i < count; i++)
{
BLEAdvertisedDevice d = foundDevices.getDevice(i);
if (d.getName() == "Mi Band 3") {
char deviceBuffer[100];
deviceName = d.getName().c_str();
deviceAddress = d.getAddress().toString().c_str();
deviceRSSI = d.getRSSI();
sprintf(deviceBuffer, "Name: %s| Address: %s| RSSI: %d\n", deviceName.c_str(), deviceAddress.c_str(), deviceRSSI);
Serial.print(deviceBuffer);
if (deviceAddress == "e6:37:63:e7:2f:4b" && deviceRSSI > -80)
{
digitalWrite(LED_ONBOARD, HIGH); // Turn on LED
}
else
{
digitalWrite(LED_ONBOARD, LOW); // Turn off LED
}
}
}
pBLEScan->clearResults();
}
You can’t perform that action at this time.