Merge branch 'main' into audio-player

This commit is contained in:
pelgraine
2026-02-14 10:17:58 +11:00

View File

@@ -1,12 +1,4 @@
#include "SerialBLEInterface.h"
#include <SPIFFS.h>
// FIRMWARE_VERSION is defined in MyMesh.h which may not be in our include
// chain. If missing, fall back to "unknown" which will safely force a
// bond clear on every boot (conservative but harmless).
#ifndef FIRMWARE_VERSION
#define FIRMWARE_VERSION "unknown"
#endif
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
@@ -35,42 +27,11 @@ void SerialBLEInterface::begin(const char* prefix, char* name, uint32_t pin_code
BLEDevice::setSecurityCallbacks(this);
BLEDevice::setMTU(MAX_FRAME_SIZE);
// Clear stale BLE bonds when firmware version changes.
// After a flash, the phone still has old bonding keys cached which causes
// an authentication error on first connect. By wiping bonds only when
// the version string changes, normal reboots keep their pairing intact.
{
bool clear_bonds = false;
File f = SPIFFS.open("/ble_ver", "r");
if (f) {
char stored[24] = {0};
f.readBytes(stored, sizeof(stored) - 1);
f.close();
clear_bonds = (strcmp(stored, FIRMWARE_VERSION) != 0);
} else {
clear_bonds = true; // first boot after flash
}
if (clear_bonds) {
int n = esp_ble_get_bond_device_num();
if (n > 0) {
esp_ble_bond_dev_t *devs = (esp_ble_bond_dev_t *)malloc(sizeof(esp_ble_bond_dev_t) * n);
if (devs) {
esp_ble_get_bond_device_list(&n, devs);
for (int i = 0; i < n; i++) esp_ble_remove_bond_device(devs[i].bd_addr);
free(devs);
BLE_DEBUG_PRINTLN("Cleared %d stale BLE bond(s) (firmware changed)", n);
}
}
File fw = SPIFFS.open("/ble_ver", "w", true);
if (fw) { fw.print(FIRMWARE_VERSION); fw.close(); }
}
}
BLESecurity sec;
sec.setStaticPIN(pin_code);
sec.setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);
BLEDevice::setPower(ESP_PWR_LVL_N3); // -3 dBm — saves power, plenty for nearby phone
//BLEDevice::setPower(ESP_PWR_LVL_N8);
// Create the BLE Server
pServer = BLEDevice::createServer();
@@ -176,10 +137,10 @@ void SerialBLEInterface::enable() {
// Start the service
pService->start();
// Start advertising — relaxed interval saves power while waiting for
// a phone to connect, but stays responsive enough for quick pairing
pServer->getAdvertising()->setMinInterval(480); // 480 * 0.625ms = 300ms
pServer->getAdvertising()->setMaxInterval(960); // 960 * 0.625ms = 600ms
// Start advertising
//pServer->getAdvertising()->setMinInterval(500);
//pServer->getAdvertising()->setMaxInterval(1000);
pServer->getAdvertising()->start();
adv_restart_time = 0;
@@ -261,8 +222,8 @@ size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) {
BLE_DEBUG_PRINTLN("SerialBLEInterface -> disconnecting...");
pServer->getAdvertising()->setMinInterval(480);
pServer->getAdvertising()->setMaxInterval(960);
//pServer->getAdvertising()->setMinInterval(500);
//pServer->getAdvertising()->setMaxInterval(1000);
adv_restart_time = millis() + ADVERT_RESTART_DELAY;
} else {