Created by
David Stacer
| /*
Based on Neil Kolban example for Arduino IDE: https://github.com/nkolban/ESP32_BLE_Arduino/tree/master/examples/BLE_scan
Ported to Arduino ESP32 by Evandro Copercini
*/
// dumps the ServiceData for 0x1a18 BLE advertisement.
// For the wonderful firmware located https://github.com/atc1441/ATC_MiThermometer
// uses BLE library https://github.com/nkolban/ESP32_BLE_Arduino
//
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
// Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
// //Serial.printf("getServiceDataUUID() %s\n", advertisedDevice.getServiceDataUUID());
// if ( advertisedDevice.haveServiceUUID()) {
// Serial.println("has service UUID");
// }
// if ( advertisedDevice.haveName()) {
// Serial.println("has Name");
// }
if ( advertisedDevice.haveServiceData()) {
char buff[20];
int datalen;
Serial.printf(">>>> ServiceDataUUID 0x");
datalen = (*advertisedDevice.getServiceDataUUID().getNative()).len;
memcpy(buff, &(*advertisedDevice.getServiceDataUUID().getNative()).uuid, datalen);
//Serial.printf("UUID Len %d \n", (*advertisedDevice.getServiceDataUUID().getNative()).len);
//Serial.printf("Service Data UUID length %d\n", datalen);
for (int i = 0; i < datalen; i++) {
Serial.printf("%0x", buff[i]);
}
Serial.println();
char tempUUID[] = { 0x1a, 0x18, 0x0} ;
if ( strncmp( buff, tempUUID, datalen) == 0 ) {
Serial.println("Found a ServiceDataUUID for Temperature");
datalen = advertisedDevice.getServiceData().length();
memcpy(buff, advertisedDevice.getServiceData().c_str(), datalen);
Serial.printf(">>>> Serrvice Data ");
for (int i = 0; i < datalen; i++) {
//Serial.printf("%02.2x ", buff[i]);
Serial.printf("%0x ",advertisedDevice.getServiceData().c_str()[i]);
}
Serial.println();
Serial.println(advertisedDevice.getRSSI());
}
else {
Serial.println(" ServiceDataUUID doesn't match");
}
}
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
//esp_log_level_set("*", ESP_LOG_DEBUG);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(false); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.print(foundDevices.getCount());
Serial.println(" Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(5000);
}
|