Optimize Bluetooth proxy batching and increase scan buffer capacity (#9328)

This commit is contained in:
J. Nick Koston 2025-07-07 14:34:12 -05:00 committed by GitHub
parent e88b8d10ec
commit 138ff749f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -52,7 +52,12 @@ bool BluetoothProxy::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
return true; return true;
} }
static constexpr size_t FLUSH_BATCH_SIZE = 8; // Batch size for BLE advertisements to maximize WiFi efficiency
// Each advertisement is up to 80 bytes when packaged (including protocol overhead)
// Most advertisements are 20-30 bytes, allowing even more to fit per packet
// 16 advertisements × 80 bytes (worst case) = 1280 bytes out of ~1320 bytes usable payload
// This achieves ~97% WiFi MTU utilization while staying under the limit
static constexpr size_t FLUSH_BATCH_SIZE = 16;
static std::vector<api::BluetoothLERawAdvertisement> &get_batch_buffer() { static std::vector<api::BluetoothLERawAdvertisement> &get_batch_buffer() {
static std::vector<api::BluetoothLERawAdvertisement> batch_buffer; static std::vector<api::BluetoothLERawAdvertisement> batch_buffer;
return batch_buffer; return batch_buffer;

View File

@ -25,10 +25,15 @@ namespace esphome {
namespace esp32_ble { namespace esp32_ble {
// Maximum number of BLE scan results to buffer // Maximum number of BLE scan results to buffer
// Sized to handle bursts of advertisements while allowing for processing delays
// With 16 advertisements per batch and some safety margin:
// - Without PSRAM: 24 entries (1.5× batch size)
// - With PSRAM: 36 entries (2.25× batch size)
// The reduced structure size (~80 bytes vs ~400 bytes) allows for larger buffers
#ifdef USE_PSRAM #ifdef USE_PSRAM
static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 32; static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 36;
#else #else
static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 20; static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 24;
#endif #endif
// Maximum size of the BLE event queue - must be power of 2 for lock-free queue // Maximum size of the BLE event queue - must be power of 2 for lock-free queue