#include #include #include #include int scanTime = 5; // Scan time in seconds BLEScan* pBLEScan; class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice advertisedDevice) { Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str()); if (String(advertisedDevice.getName().c_str()) == "BT04-A") { // Check if the device is HC-05 Serial.println("BT04-A found! Sending a command..."); // Further code to connect and send data would go here } } }; void setup() { Serial.begin(115200); Serial.println("Scanning..."); BLEDevice::init(""); // Initialize BLE device pBLEScan = BLEDevice::getScan(); // Create new scan pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); // Active scan uses more power, but gets results faster pBLEScan->setInterval(100); pBLEScan->setWindow(99); // Less or equal to setInterval value } void loop() { BLEScanResults foundDevices = pBLEScan->start(scanTime, false); // Use object directly Serial.print("Devices found: "); Serial.println(foundDevices.getCount()); // Use object access syntax Serial.println("Scan done!"); pBLEScan->clearResults(); // delete results from BLEScan buffer to release memory delay(2000); }