Use blocking BLE scan for reliable discovery
This commit is contained in:
@@ -104,13 +104,17 @@ int scanBleDevices(uint32_t scanSeconds) {
|
|||||||
scan->setInterval(100);
|
scan->setInterval(100);
|
||||||
scan->setWindow(99);
|
scan->setWindow(99);
|
||||||
|
|
||||||
Serial.println("BLE: Scanning...");
|
Serial.print("BLE: Scanning for ");
|
||||||
Serial.println("BLE devices:");
|
Serial.print(scanSeconds);
|
||||||
|
Serial.println(" seconds...");
|
||||||
|
|
||||||
|
NimBLEScanResults results = scan->getResults(scanSeconds, false);
|
||||||
|
|
||||||
for (uint32_t second = 0; second < scanSeconds; second++) {
|
|
||||||
NimBLEScanResults results = scan->getResults(1, false);
|
|
||||||
int resultCount = results.getCount();
|
int resultCount = results.getCount();
|
||||||
|
|
||||||
|
Serial.print("BLE: Raw devices seen: ");
|
||||||
|
Serial.println(resultCount);
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@@ -137,37 +141,38 @@ int scanBleDevices(uint32_t scanSeconds) {
|
|||||||
bleScanResults[bleScanResultCount].name = name;
|
bleScanResults[bleScanResultCount].name = name;
|
||||||
bleScanResults[bleScanResultCount].rssi = device->getRSSI();
|
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++;
|
bleScanResultCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
scan->clearResults();
|
scan->clearResults();
|
||||||
|
|
||||||
Serial.print(".");
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
Serial.print("BLE: Devices found: ");
|
Serial.print("BLE: Devices found: ");
|
||||||
Serial.println(bleScanResultCount);
|
Serial.println(bleScanResultCount);
|
||||||
|
|
||||||
if (bleScanResultCount > 0) {
|
if (bleScanResultCount == 0) {
|
||||||
Serial.println("Use: select bms <number>");
|
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 <number>");
|
||||||
|
|
||||||
return bleScanResultCount;
|
return bleScanResultCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user