I want to scan a device with specific name and get 2 variables advertised by the other device at the same time, the same code will be used in both the device Please find below the code for your ready reference
#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEAdvertising.h>
// #include <BLEService.h>
#include <BLEServer.h>
#define MAX_DEVICES 5
#define SCAN_DURATION_MS 2
#define MIN_RSSI_THRESHOLD -75 // Adjust this threshold as needed
BLEAdvertising *pAdvertising;
String hostname;
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
struct DeviceInfo {
String hostnameFounded;
int rssi;
int variable1;
int variable2;
};
String hostnameFounded1;
int blerssivalue;
DeviceInfo devices[MAX_DEVICES];
int numDevices = 0;
unsigned long previousScanMillis = 0;
const int scanDurationMs = 500;
void TaskBLEScan( void *pvParameters );
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
String deviceName = advertisedDevice.getName().c_str();
if (deviceName.startsWith("ABCD")) {
// Check if device already exists
bool found = false;
for (int i = 0; i < numDevices; i++) {
if (devices[i].hostnameFounded == deviceName) {
found = true;
break;
}
}
// Add device if not found
if (!found && numDevices < MAX_DEVICES) {
devices[numDevices].hostnameFounded = deviceName;
devices[numDevices].rssi = advertisedDevice.getRSSI();
devices[numDevices].variable1 = 1;
devices[numDevices].variable2 = 10;
numDevices++;
}
}
}
};
void setup() {
BLEDevice::init(hostname.c_str());
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
BLEDevice::setPower(ESP_PWR_LVL_N12);
// Create a service and characteristic for advertising
// BLEService *pService = BLEDevice::createService("0000ffff-0000-1000-8000-00805f9b34fb");
// BLECharacteristic *pCharacteristic = pService->createCharacteristic("00001111-0000-1000-8000-00805f9b34fb", BLECharacteristic::PROPERTY_READ);
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
// Set the initial values for the characteristic
pCharacteristic->setValue(String("Variable1: ") + String(variable1.c_str()) + "\nVariable2: " + String(variable2.c_str()));
// Add the service to the advertising data
BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addService(pService);
pAdvertising->setScanResponse(true); // Optional: Set scan response data
pAdvertising->start();
xTaskCreatePinnedToCore(
TaskBLEScan,
"AnalogBLEScan",
1024, // Stack size
NULL,
3, // Priority
NULL,
ARDUINO_RUNNING_CORE);
}
void loop() {
}
void TaskBLEScan(void *pvParameters) {
(void) pvParameters;
unsigned long currentMillis = millis();
for (;;) {
if (currentMillis - previousScanMillis >= scanDurationMs) {
BLEScanResults foundDevices = pBLEScan->start(SCAN_DURATION_MS); // Adjust scan time as needed
// Process detected devices
int maxRssiIndex = -1;
int maxRssi = INT_MIN; // Initialize with minimum possible value
for (int i = 0; i < numDevices; i++) {
if (devices[i].rssi > MIN_RSSI_THRESHOLD && devices[i].rssi > maxRssi) {
maxRssi = devices[i].rssi;
maxRssiIndex = i;
}
}
if (maxRssiIndex != -1) {
Serial.print("Device Nearby: ");
hostnameFounded1 = devices[maxRssiIndex].hostnameFounded;
Serial.println(hostnameFounded1);
Serial.print("RSSI: ");
blerssivalue = devices[maxRssiIndex].rssi;
Serial.println(blerssivalue);
Serial.print("Variable 1: ");
Serial.println(devices[maxRssiIndex].variable1);
Serial.print("Variable 2: ");
Serial.println(devices[maxRssiIndex].variable2);
}
//wifirssivalue = WiFi.RSSI();
}
numDevices = 0; // Clear detected devices for next scan
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
vTaskDelay(50);
}
}