diff --git a/firmware/esp32/xterra-controller/bms.cpp b/firmware/esp32/xterra-controller/bms.cpp index b256c7c..a200e81 100644 --- a/firmware/esp32/xterra-controller/bms.cpp +++ b/firmware/esp32/xterra-controller/bms.cpp @@ -104,70 +104,75 @@ int scanBleDevices(uint32_t scanSeconds) { scan->setInterval(100); scan->setWindow(99); - Serial.println("BLE: Scanning..."); - Serial.println("BLE devices:"); + Serial.print("BLE: Scanning for "); + Serial.print(scanSeconds); + Serial.println(" seconds..."); - for (uint32_t second = 0; second < scanSeconds; second++) { - NimBLEScanResults results = scan->getResults(1, false); - int resultCount = results.getCount(); + NimBLEScanResults results = scan->getResults(scanSeconds, false); - for (int i = 0; i < resultCount && bleScanResultCount < MAX_BLE_SCAN_RESULTS; i++) { - const NimBLEAdvertisedDevice* device = results.getDevice(i); + int resultCount = results.getCount(); - String address = device->getAddress().toString().c_str(); - bool alreadySeen = false; + Serial.print("BLE: Raw devices seen: "); + Serial.println(resultCount); - for (int existing = 0; existing < bleScanResultCount; existing++) { - if (bleScanResults[existing].address == address) { - alreadySeen = true; - break; - } + 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(); + bool alreadySeen = false; + + 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++; } - scan->clearResults(); + if (alreadySeen) { + continue; + } - Serial.print("."); + String name = ""; + if (device->haveName()) { + name = device->getName().c_str(); + } + + bleScanResults[bleScanResultCount].address = address; + bleScanResults[bleScanResultCount].name = name; + bleScanResults[bleScanResultCount].rssi = device->getRSSI(); + + bleScanResultCount++; } - Serial.println(); + scan->clearResults(); Serial.print("BLE: Devices found: "); Serial.println(bleScanResultCount); - if (bleScanResultCount > 0) { - Serial.println("Use: select bms "); + if (bleScanResultCount == 0) { + return bleScanResultCount; } + Serial.println("BLE devices:"); + + for (int i = 0; i < bleScanResultCount; i++) { + Serial.print(i + 1); + Serial.print(") "); + + if (bleScanResults[i].name.length() > 0) { + Serial.print(bleScanResults[i].name); + } else { + Serial.print("(no name)"); + } + + Serial.print(" | "); + Serial.print(bleScanResults[i].address); + Serial.print(" | RSSI "); + Serial.println(bleScanResults[i].rssi); + } + + Serial.println("Use: select bms "); + return bleScanResultCount; }