Add serial BLE scan and BMS selection

This commit is contained in:
2026-06-04 01:48:26 -06:00
parent 83b7c73dc6
commit 2411e91f8d
3 changed files with 142 additions and 0 deletions
+60
View File
@@ -21,6 +21,9 @@ static const unsigned long READ_INTERVAL_MS = 5000;
BmsData bmsData;
static BleScanResult bleScanResults[MAX_BLE_SCAN_RESULTS];
static int bleScanResultCount = 0;
static uint8_t JBD_STATUS_REQUEST[] = {
0xDD, 0xA5, 0x03, 0x00, 0xFF, 0xFD, 0x77
};
@@ -62,6 +65,63 @@ static void handleNotify(
}
}
int getBleScanResultCount() {
return bleScanResultCount;
}
const BleScanResult* getBleScanResult(int index) {
if (index < 0 || index >= bleScanResultCount) {
return nullptr;
}
return &bleScanResults[index];
}
int scanBleDevices(uint32_t scanSeconds) {
Serial.println("BLE: Starting scan...");
if (client && client->isConnected()) {
Serial.println("BLE: Disconnecting BMS before scan");
client->disconnect();
bmsData.connected = false;
}
bleScanResultCount = 0;
NimBLEScan* scan = NimBLEDevice::getScan();
scan->setActiveScan(true);
scan->setInterval(100);
scan->setWindow(99);
NimBLEScanResults results = scan->getResults(scanSeconds, false);
int resultCount = results.getCount();
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 = "";
if (device->haveName()) {
name = device->getName().c_str();
}
bleScanResults[bleScanResultCount].address = address;
bleScanResults[bleScanResultCount].name = name;
bleScanResults[bleScanResultCount].rssi = device->getRSSI();
bleScanResultCount++;
}
scan->clearResults();
Serial.print("BLE: Devices found: ");
Serial.println(bleScanResultCount);
return bleScanResultCount;
}
static bool connectBms() {
if (client && client->isConnected()) {
bmsData.connected = true;