Print BLE scan results as devices are discovered
This commit is contained in:
@@ -104,31 +104,70 @@ int scanBleDevices(uint32_t scanSeconds) {
|
|||||||
scan->setInterval(100);
|
scan->setInterval(100);
|
||||||
scan->setWindow(99);
|
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++) {
|
for (int i = 0; i < resultCount && bleScanResultCount < MAX_BLE_SCAN_RESULTS; i++) {
|
||||||
const NimBLEAdvertisedDevice* device = results.getDevice(i);
|
const NimBLEAdvertisedDevice* device = results.getDevice(i);
|
||||||
|
|
||||||
String address = device->getAddress().toString().c_str();
|
String address = device->getAddress().toString().c_str();
|
||||||
String name = "";
|
bool alreadySeen = false;
|
||||||
|
|
||||||
if (device->haveName()) {
|
for (int existing = 0; existing < bleScanResultCount; existing++) {
|
||||||
name = device->getName().c_str();
|
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;
|
scan->clearResults();
|
||||||
bleScanResults[bleScanResultCount].name = name;
|
|
||||||
bleScanResults[bleScanResultCount].rssi = device->getRSSI();
|
Serial.print(".");
|
||||||
bleScanResultCount++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
scan->clearResults();
|
Serial.println();
|
||||||
|
|
||||||
Serial.print("BLE: Devices found: ");
|
Serial.print("BLE: Devices found: ");
|
||||||
Serial.println(bleScanResultCount);
|
Serial.println(bleScanResultCount);
|
||||||
|
|
||||||
|
if (bleScanResultCount > 0) {
|
||||||
|
Serial.println("Use: select bms <number>");
|
||||||
|
}
|
||||||
|
|
||||||
return bleScanResultCount;
|
return bleScanResultCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user