Print BLE scan results as devices are discovered

This commit is contained in:
2026-06-04 02:15:19 -06:00
parent 038b142971
commit e5a78b2fb3
+52 -13
View File
@@ -104,31 +104,70 @@ int scanBleDevices(uint32_t scanSeconds) {
scan->setInterval(100);
scan->setWindow(99);
NimBLEScanResults results = scan->getResults(scanSeconds, false);
Serial.println("BLE: Scanning...");
Serial.println("BLE devices:");
int resultCount = results.getCount();
for (uint32_t second = 0; second < scanSeconds; second++) {
NimBLEScanResults results = scan->getResults(1, false);
int resultCount = results.getCount();
for (int i = 0; i < resultCount && bleScanResultCount < MAX_BLE_SCAN_RESULTS; i++) {
const NimBLEAdvertisedDevice* device = results.getDevice(i);
for (int i = 0; i < resultCount && bleScanResultCount < MAX_BLE_SCAN_RESULTS; i++) {
const NimBLEAdvertisedDevice* device = results.getDevice(i);
String address = device->getAddress().toString().c_str();
String name = "";
String address = device->getAddress().toString().c_str();
bool alreadySeen = false;
if (device->haveName()) {
name = device->getName().c_str();
for (int existing = 0; existing < bleScanResultCount; existing++) {
if (bleScanResults[existing].address == address) {
alreadySeen = true;
break;
}
}
if (alreadySeen) {
continue;
}
String name = "";
if (device->haveName()) {
name = device->getName().c_str();
}
bleScanResults[bleScanResultCount].address = address;
bleScanResults[bleScanResultCount].name = name;
bleScanResults[bleScanResultCount].rssi = device->getRSSI();
Serial.print(bleScanResultCount + 1);
Serial.print(") ");
if (name.length() > 0) {
Serial.print(name);
} else {
Serial.print("(no name)");
}
Serial.print(" | ");
Serial.print(address);
Serial.print(" | RSSI ");
Serial.println(device->getRSSI());
bleScanResultCount++;
}
bleScanResults[bleScanResultCount].address = address;
bleScanResults[bleScanResultCount].name = name;
bleScanResults[bleScanResultCount].rssi = device->getRSSI();
bleScanResultCount++;
scan->clearResults();
Serial.print(".");
}
scan->clearResults();
Serial.println();
Serial.print("BLE: Devices found: ");
Serial.println(bleScanResultCount);
if (bleScanResultCount > 0) {
Serial.println("Use: select bms <number>");
}
return bleScanResultCount;
}