Fix Bluetooth pairing, menu crashes, and improve UX
Major fixes and improvements to Bluetooth device management and menu navigation: **Bluetooth Device Pairing** - Fixed discovered devices not being saved as paired after connection - Only save devices to NVS when successfully connected (not just discovered) - Auto-pair discovered devices on successful A2DP connection - Update device list to show paired status immediately after connection **Critical Bug Fixes** - Fixed dangling pointer crash in NVS request/response mechanism - Was sending pointer to stack variable, now sends result value directly - Prevents crash when connecting to Bluetooth devices - Fixed use-after-free crash when clicking menu items after dynamic updates - Menu context now properly synchronized after adding/removing items - Prevents InstructionFetchError crashes in menu navigation - Fixed memory exhaustion by reducing MAX_BT_DEVICES from 20 to 8 - Prevents heap allocation failures when populating device list **Menu & UX Improvements** - "Clear Paired" button now properly disconnects active connections - "Clear Paired" button always visible when paired devices exist - GUI updates immediately after clearing paired devices - Paired devices marked with asterisk prefix (* Device Name) - Removed redundant "(paired)" suffix text - Long device names scroll smoothly when selected (3-second animation) - Refresh button preserved during menu updates to prevent crashes - Menu focus state properly maintained across all dynamic updates **Technical Details** - bt_add_discovered_device() no longer saves to NVS - Added currentFocusIndex() calls after all menu modifications - Improved clear_bt_device_list() to avoid deleting active buttons - Added bt_disconnect_current_device() for clean disconnections - Fixed NVS notification mechanism to avoid stack variable pointers 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
466
main/gui.c
466
main/gui.c
@@ -71,6 +71,7 @@ static void show_volume_control(void);
|
||||
static lv_obj_t* create_volume_page(void);
|
||||
static void update_volume_display(int volume);
|
||||
static void ensure_menu_styles(void);
|
||||
static void bt_device_click_cb(lv_event_t * e);
|
||||
|
||||
// Menu stack management functions
|
||||
static void menu_stack_push(lv_obj_t *page);
|
||||
@@ -116,6 +117,18 @@ static lv_style_t _styleFocusedBtn;
|
||||
|
||||
static menu_context_t _menuContext;
|
||||
|
||||
// Bluetooth page state
|
||||
static lv_obj_t* _bt_page = NULL;
|
||||
static lv_obj_t* _bt_status_item = NULL; // Top status item (unselectable)
|
||||
static lv_obj_t* _bt_device_container = NULL; // Container for device list items
|
||||
static TickType_t _bt_scan_start_time = 0; // When scan was started
|
||||
|
||||
// Volume page state
|
||||
static lv_obj_t* _volume_page = NULL;
|
||||
static lv_obj_t* _volume_bar = NULL;
|
||||
static lv_obj_t* _volume_label = NULL;
|
||||
static int _current_volume = 50; // Default volume (0-100)
|
||||
|
||||
// Hide *all* headers/back buttons that LVGL may show for this menu
|
||||
static void menu_hide_headers(lv_obj_t *menu) {
|
||||
if (!menu) return;
|
||||
@@ -208,6 +221,10 @@ static void cleanup_menu(void) {
|
||||
lv_obj_del(_menu);
|
||||
_menu = NULL;
|
||||
_currentPage = NULL;
|
||||
_bt_page = NULL;
|
||||
_bt_status_item = NULL;
|
||||
_bt_device_container = NULL;
|
||||
_bt_scan_start_time = 0;
|
||||
ESP_LOGI(TAG, "Menu cleaned up to free memory");
|
||||
}
|
||||
}
|
||||
@@ -493,6 +510,13 @@ static void menuInc(int inc)
|
||||
|
||||
ESP_LOGI(TAG, "Current Index: %d", _menuContext.selected);
|
||||
|
||||
// Safety check: if no focused object found, bail out
|
||||
if (_menuContext.selected < 0 || _menuContext.obj == NULL) {
|
||||
ESP_LOGW(TAG, "No focused object found, cannot navigate");
|
||||
UNLOCK();
|
||||
return;
|
||||
}
|
||||
|
||||
lv_obj_t *next = NULL;
|
||||
|
||||
// check if we are at the first or last in the page
|
||||
@@ -501,14 +525,16 @@ static void menuInc(int inc)
|
||||
if (_menuContext.obj != test)
|
||||
{
|
||||
next = lv_obj_get_child(_currentPage, _menuContext.selected + inc);
|
||||
lv_obj_add_state(next, LV_STATE_FOCUSED);
|
||||
lv_obj_clear_state(_menuContext.obj, LV_STATE_FOCUSED);
|
||||
lv_obj_scroll_to_view(next, LV_ANIM_ON);
|
||||
if (next) {
|
||||
lv_obj_add_state(next, LV_STATE_FOCUSED);
|
||||
lv_obj_clear_state(_menuContext.obj, LV_STATE_FOCUSED);
|
||||
lv_obj_scroll_to_view(next, LV_ANIM_ON);
|
||||
|
||||
_menuContext.obj = next;
|
||||
_menuContext.selected += inc;
|
||||
_menuContext.obj = next;
|
||||
_menuContext.selected += inc;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
UNLOCK();
|
||||
}
|
||||
|
||||
@@ -593,7 +619,7 @@ static lv_obj_t * addMenuItem(lv_obj_t *page, const char *text)
|
||||
lv_obj_set_width(lbl, LV_PCT(95)); // Set width to allow scrolling
|
||||
lv_obj_set_style_radius(lbl, 0, LV_PART_MAIN | LV_STATE_ANY);
|
||||
lv_obj_set_style_border_width(lbl, 0, LV_PART_MAIN | LV_STATE_ANY);
|
||||
lv_obj_set_style_anim_time(lbl, 4000, LV_PART_MAIN); // Set scrolling duration (4 seconds per cycle - slower)
|
||||
lv_obj_set_style_anim_time(lbl, 3000, LV_PART_MAIN); // Set scrolling duration (3 seconds per cycle)
|
||||
lv_obj_align(lbl, LV_ALIGN_LEFT_MID, 0, 0); // Center vertically, align left horizontally
|
||||
|
||||
// Add focus event handler to control scrolling
|
||||
@@ -699,14 +725,132 @@ static lv_obj_t* create_menu_container(void) {
|
||||
|
||||
|
||||
// ───── BLUETOOTH DEVICE LIST PAGE ─────
|
||||
static lv_obj_t* _bt_page = NULL;
|
||||
static int _bt_selected_device = 0;
|
||||
|
||||
// ───── VOLUME CONTROL PAGE ─────
|
||||
static lv_obj_t* _volume_page = NULL;
|
||||
static lv_obj_t* _volume_bar = NULL;
|
||||
static lv_obj_t* _volume_label = NULL;
|
||||
static int _current_volume = 50; // Default volume (0-100)
|
||||
// Update the status item text (e.g., "Scanning...", "No devices found")
|
||||
static void update_bt_status(const char* status_text) {
|
||||
if (_bt_status_item) {
|
||||
lv_obj_t* label = lv_obj_get_child(_bt_status_item, 0);
|
||||
if (label) {
|
||||
ESP_LOGI(TAG, "Updating BT status to: %s", status_text);
|
||||
lv_label_set_text(label, status_text);
|
||||
lv_obj_invalidate(label); // Force redraw
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Status item has no label child");
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Status item is NULL, cannot update");
|
||||
}
|
||||
}
|
||||
|
||||
// Clear all device items and action buttons from the container (keep status and Back only)
|
||||
static void clear_bt_device_list(void) {
|
||||
if (!_bt_device_container) return;
|
||||
|
||||
// Delete all children except status (index 0) and Back (last item)
|
||||
uint32_t child_count = lv_obj_get_child_count(_bt_device_container);
|
||||
ESP_LOGI(TAG, "Clearing BT device list, child_count=%d", (int)child_count);
|
||||
|
||||
// Work backwards to avoid index shifting issues
|
||||
// Keep: status (0) and Back (last)
|
||||
for (int i = child_count - 2; i > 0; i--) {
|
||||
lv_obj_t* child = lv_obj_get_child(_bt_device_container, i);
|
||||
if (child && child != _bt_status_item) {
|
||||
ESP_LOGI(TAG, " Deleting child at index %d", i);
|
||||
lv_obj_del(child);
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "Clear complete, remaining children: %d", (int)lv_obj_get_child_count(_bt_device_container));
|
||||
}
|
||||
|
||||
// Populate the device list with current devices and add action buttons
|
||||
static void populate_bt_device_list(void) {
|
||||
if (!_bt_device_container) return;
|
||||
|
||||
// Clear old device list items first (keeps status and Back button)
|
||||
clear_bt_device_list();
|
||||
|
||||
bt_device_list_t* device_list = bt_get_device_list();
|
||||
if (!device_list) {
|
||||
update_bt_status("Error getting devices");
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Populating BT list with %d devices", device_list->count);
|
||||
|
||||
// The page has: [status, Back]
|
||||
// We'll add: [status, ...devices..., Refresh, Clear?, Back]
|
||||
uint32_t insert_index = 1; // After status item
|
||||
bool has_paired_devices = false;
|
||||
|
||||
if (device_list->count == 0) {
|
||||
update_bt_status("No devices found");
|
||||
// Still add Refresh button so user can try again
|
||||
} else {
|
||||
update_bt_status("Available Devices:");
|
||||
|
||||
// Add device items (limit to MAX_BT_DEVICES to avoid memory issues)
|
||||
bool first = true;
|
||||
int max_items = (device_list->count < MAX_BT_DEVICES) ? device_list->count : MAX_BT_DEVICES;
|
||||
for (int i = 0; i < max_items; i++) {
|
||||
char device_text[64];
|
||||
const char* paired_prefix = device_list->devices[i].is_paired ? "* " : "";
|
||||
snprintf(device_text, sizeof(device_text), "%s%s",
|
||||
paired_prefix,
|
||||
device_list->devices[i].name);
|
||||
|
||||
if (device_list->devices[i].is_paired) {
|
||||
has_paired_devices = true;
|
||||
}
|
||||
|
||||
lv_obj_t* btn = addMenuItem(_bt_device_container, device_text);
|
||||
lv_obj_add_event_cb(btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
// Move to correct position (after status, before Back button)
|
||||
lv_obj_move_to_index(btn, insert_index++);
|
||||
|
||||
if (first) {
|
||||
lv_obj_add_state(btn, LV_STATE_FOCUSED);
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear focus from all items first
|
||||
uint32_t child_count = lv_obj_get_child_count(_bt_device_container);
|
||||
for (uint32_t i = 0; i < child_count; i++) {
|
||||
lv_obj_t* child = lv_obj_get_child(_bt_device_container, i);
|
||||
if (child) {
|
||||
lv_obj_clear_state(child, LV_STATE_FOCUSED);
|
||||
}
|
||||
}
|
||||
|
||||
// Always add Refresh button after devices (but before Back)
|
||||
lv_obj_t* refresh_btn = addMenuItem(_bt_device_container, "Refresh");
|
||||
lv_obj_add_event_cb(refresh_btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
lv_obj_move_to_index(refresh_btn, insert_index++);
|
||||
|
||||
// Only add Clear Paired button if there are actually paired devices
|
||||
if (has_paired_devices) {
|
||||
lv_obj_t* clear_btn = addMenuItem(_bt_device_container, "Clear Paired");
|
||||
lv_obj_add_event_cb(clear_btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
lv_obj_move_to_index(clear_btn, insert_index++);
|
||||
}
|
||||
|
||||
// Set focus on appropriate item
|
||||
if (device_list->count == 0) {
|
||||
// No devices found - focus on Refresh
|
||||
lv_obj_add_state(refresh_btn, LV_STATE_FOCUSED);
|
||||
} else {
|
||||
// Devices found - focus on first device (already done above, but devices list was cleared)
|
||||
lv_obj_t* first_device = lv_obj_get_child(_bt_device_container, 1); // After status
|
||||
if (first_device) {
|
||||
lv_obj_add_state(first_device, LV_STATE_FOCUSED);
|
||||
}
|
||||
}
|
||||
|
||||
// Update menu context to track the newly focused item
|
||||
currentFocusIndex(&_menuContext);
|
||||
}
|
||||
|
||||
static void bt_device_click_cb(lv_event_t * e) {
|
||||
if (!e) {
|
||||
@@ -741,20 +885,95 @@ static void bt_device_click_cb(lv_event_t * e) {
|
||||
return;
|
||||
} else if (strcmp(txt, "Refresh") == 0) {
|
||||
LOCK();
|
||||
// Recreate the BT page with discovery enabled to show "Scanning..."
|
||||
show_bt_device_list();
|
||||
// Update status first
|
||||
update_bt_status("Scanning...");
|
||||
_bt_scan_start_time = xTaskGetTickCount();
|
||||
|
||||
// Don't delete the current button while we're in its event handler!
|
||||
// Instead, delete only the device items, keeping Refresh and Back buttons
|
||||
uint32_t child_count = lv_obj_get_child_count(_bt_device_container);
|
||||
ESP_LOGI(TAG, "Refresh clicked, clearing device items (not buttons), child_count=%d", (int)child_count);
|
||||
|
||||
// Delete children between status (0) and the last items
|
||||
// Work backwards to avoid index shifting
|
||||
// Keep: status (0), Refresh, Back - delete everything else (devices, Clear Paired)
|
||||
for (int i = child_count - 2; i > 0; i--) {
|
||||
lv_obj_t* child = lv_obj_get_child(_bt_device_container, i);
|
||||
if (!child || child == _bt_status_item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the button's text to decide if we should delete it
|
||||
const char* child_text = NULL;
|
||||
lv_obj_t* child_label = lv_obj_get_child(child, 0);
|
||||
if (child_label) {
|
||||
child_text = lv_label_get_text(child_label);
|
||||
}
|
||||
|
||||
// Only keep Refresh and Back buttons
|
||||
if (child_text) {
|
||||
if (strcmp(child_text, "Refresh") != 0 && strcmp(child_text, "Back") != 0) {
|
||||
ESP_LOGI(TAG, " Deleting item: %s", child_text);
|
||||
lv_obj_del(child);
|
||||
} else {
|
||||
ESP_LOGI(TAG, " Keeping button: %s", child_text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear all focus states and set focus on Back button
|
||||
child_count = lv_obj_get_child_count(_bt_device_container);
|
||||
for (uint32_t i = 0; i < child_count; i++) {
|
||||
lv_obj_t* child = lv_obj_get_child(_bt_device_container, i);
|
||||
if (child) {
|
||||
lv_obj_clear_state(child, LV_STATE_FOCUSED);
|
||||
}
|
||||
}
|
||||
// Focus on Back button (last child)
|
||||
lv_obj_t* back_btn = lv_obj_get_child(_bt_device_container, -1);
|
||||
if (back_btn) {
|
||||
lv_obj_add_state(back_btn, LV_STATE_FOCUSED);
|
||||
}
|
||||
|
||||
// Update menu context to track the newly focused item
|
||||
currentFocusIndex(&_menuContext);
|
||||
|
||||
bt_start_discovery();
|
||||
UNLOCK();
|
||||
return;
|
||||
} else if (strcmp(txt, "Clear Paired") == 0) {
|
||||
LOCK();
|
||||
// Disconnect from any connected device first
|
||||
bt_disconnect_current_device();
|
||||
|
||||
// Clear all paired devices from NVS and device list
|
||||
esp_err_t ret = system_clearAllPairedDevices();
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Successfully cleared all paired devices from NVS");
|
||||
// Also clear the in-memory device list used by GUI
|
||||
bt_clear_all_devices();
|
||||
// Refresh the device list to show updated state
|
||||
system_requestBtRefresh();
|
||||
|
||||
// Immediately update GUI to show empty list
|
||||
clear_bt_device_list();
|
||||
update_bt_status("No devices found");
|
||||
|
||||
// Add back the Refresh button
|
||||
lv_obj_t* refresh_btn = addMenuItem(_bt_device_container, "Refresh");
|
||||
lv_obj_add_event_cb(refresh_btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
lv_obj_move_to_index(refresh_btn, 1); // After status, before Back
|
||||
|
||||
// Focus on Refresh button
|
||||
uint32_t child_count = lv_obj_get_child_count(_bt_device_container);
|
||||
for (uint32_t i = 0; i < child_count; i++) {
|
||||
lv_obj_t* child = lv_obj_get_child(_bt_device_container, i);
|
||||
if (child) {
|
||||
lv_obj_clear_state(child, LV_STATE_FOCUSED);
|
||||
}
|
||||
}
|
||||
lv_obj_add_state(refresh_btn, LV_STATE_FOCUSED);
|
||||
|
||||
// Update menu context to track the newly focused item
|
||||
currentFocusIndex(&_menuContext);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Failed to clear paired devices: %s", esp_err_to_name(ret));
|
||||
}
|
||||
@@ -762,31 +981,39 @@ static void bt_device_click_cb(lv_event_t * e) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find which device was clicked
|
||||
// Find which device was clicked by matching the device name
|
||||
bt_device_list_t* device_list = bt_get_device_list();
|
||||
if (!device_list) {
|
||||
ESP_LOGE(TAG, "Null device list in bt_device_click_cb");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_bt_page) {
|
||||
ESP_LOGE(TAG, "Null _bt_page in bt_device_click_cb");
|
||||
return;
|
||||
|
||||
// Extract device name from button text (remove "* " prefix if present)
|
||||
char device_name[64];
|
||||
const char* name_start = txt;
|
||||
|
||||
// Skip "* " prefix if present
|
||||
if (txt[0] == '*' && txt[1] == ' ') {
|
||||
name_start = txt + 2;
|
||||
}
|
||||
|
||||
|
||||
strncpy(device_name, name_start, sizeof(device_name) - 1);
|
||||
device_name[sizeof(device_name) - 1] = '\0';
|
||||
|
||||
// Find the device index by name
|
||||
for (int i = 0; i < device_list->count; i++) {
|
||||
lv_obj_t * child = lv_obj_get_child(_bt_page, i);
|
||||
if (child == btn) {
|
||||
ESP_LOGI(TAG, "Requesting connection to device %d: %s", i, device_list->devices[i].name);
|
||||
// Use system event instead of direct BT call
|
||||
if (strcmp(device_list->devices[i].name, device_name) == 0) {
|
||||
ESP_LOGI(TAG, "Requesting connection to device %d: %s", i, device_name);
|
||||
system_requestBtConnect(i);
|
||||
|
||||
|
||||
// Return to bubble mode after selection
|
||||
_mode = GUI_BUBBLE;
|
||||
show_bubble();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGW(TAG, "Device not found in list: %s", device_name);
|
||||
}
|
||||
|
||||
static lv_obj_t* create_bt_device_page(bool start_discovery) {
|
||||
@@ -798,86 +1025,79 @@ static lv_obj_t* create_bt_device_page(bool start_discovery) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Delete old BT page if it exists to prevent memory leaks
|
||||
if (_bt_page) {
|
||||
ESP_LOGI(TAG, "Deleting old Bluetooth page");
|
||||
lv_obj_del(_bt_page);
|
||||
_bt_page = NULL;
|
||||
}
|
||||
// Only create the page structure once, or recreate if it was deleted
|
||||
if (_bt_page == NULL) {
|
||||
ESP_LOGI(TAG, "Creating new Bluetooth page structure");
|
||||
|
||||
_bt_page = lv_menu_page_create(menu, NULL);
|
||||
if (!_bt_page) {
|
||||
ESP_LOGE(TAG, "Failed to create Bluetooth page");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lv_obj_set_style_radius(_bt_page, 0, LV_PART_MAIN | LV_STATE_ANY);
|
||||
lv_obj_set_style_border_width(_bt_page, 0, LV_PART_MAIN | LV_STATE_ANY);
|
||||
lv_obj_set_style_pad_all(_bt_page, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_pad_row(_bt_page, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_pad_column(_bt_page, 0, LV_PART_MAIN);
|
||||
lv_obj_set_scrollbar_mode(_bt_page, LV_SCROLLBAR_MODE_AUTO);
|
||||
lv_obj_set_size(_bt_page, lv_pct(100), lv_pct(100));
|
||||
|
||||
bool discovery_started = false;
|
||||
if (start_discovery) {
|
||||
ESP_LOGI(TAG, "Starting Bluetooth discovery");
|
||||
// Try to start discovery (may fail if BT stack is busy)
|
||||
discovery_started = bt_start_discovery();
|
||||
|
||||
if (!discovery_started) {
|
||||
ESP_LOGW(TAG, "Discovery not started - will show paired devices only");
|
||||
_bt_page = lv_menu_page_create(menu, NULL);
|
||||
if (!_bt_page) {
|
||||
ESP_LOGE(TAG, "Failed to create Bluetooth page");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
lv_obj_set_style_radius(_bt_page, 0, LV_PART_MAIN | LV_STATE_ANY);
|
||||
lv_obj_set_style_border_width(_bt_page, 0, LV_PART_MAIN | LV_STATE_ANY);
|
||||
lv_obj_set_style_pad_all(_bt_page, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_pad_row(_bt_page, 0, LV_PART_MAIN);
|
||||
lv_obj_set_style_pad_column(_bt_page, 0, LV_PART_MAIN);
|
||||
lv_obj_set_scrollbar_mode(_bt_page, LV_SCROLLBAR_MODE_AUTO);
|
||||
lv_obj_set_size(_bt_page, lv_pct(100), lv_pct(100));
|
||||
|
||||
// Use the page itself as the container
|
||||
_bt_device_container = _bt_page;
|
||||
|
||||
// Create status item (unselectable, shows scan state)
|
||||
_bt_status_item = addMenuItem(_bt_device_container, "Initializing...");
|
||||
lv_obj_add_state(_bt_status_item, LV_STATE_DISABLED);
|
||||
lv_obj_clear_state(_bt_status_item, LV_STATE_FOCUSED);
|
||||
|
||||
// Create Back button (always visible)
|
||||
lv_obj_t* back_btn = addMenuItem(_bt_device_container, "Back");
|
||||
lv_obj_add_event_cb(back_btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
// Set initial focus on Back button (will be moved to first device when they appear)
|
||||
lv_obj_add_state(back_btn, LV_STATE_FOCUSED);
|
||||
}
|
||||
|
||||
// Get device list
|
||||
ESP_LOGI(TAG, "Getting device list");
|
||||
// Get device list first to decide if we should scan
|
||||
bt_device_list_t* device_list = bt_get_device_list();
|
||||
if (!device_list) {
|
||||
ESP_LOGE(TAG, "Failed to get device list");
|
||||
update_bt_status("Error");
|
||||
return _bt_page;
|
||||
}
|
||||
|
||||
if (device_list->count == 0) {
|
||||
// Show appropriate message based on discovery status
|
||||
const char* msg = discovery_started ? "Scanning..." : "No devices found";
|
||||
lv_obj_t* tmpObj = addMenuItem(_bt_page, msg);
|
||||
lv_obj_add_state(tmpObj, LV_STATE_DISABLED);
|
||||
lv_obj_clear_state(tmpObj, LV_STATE_FOCUSED); // Remove focus from disabled item
|
||||
} else {
|
||||
// Add devices to the page
|
||||
bool first = true;
|
||||
for (int i = 0; i < device_list->count; i++) {
|
||||
char device_text[64];
|
||||
snprintf(device_text, sizeof(device_text), "%s%s",
|
||||
device_list->devices[i].name,
|
||||
device_list->devices[i].is_paired ? " (paired)" : "");
|
||||
|
||||
lv_obj_t* btn = addMenuItem(_bt_page, device_text);
|
||||
lv_obj_add_event_cb(btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
if (first) {
|
||||
lv_obj_add_state(btn, LV_STATE_FOCUSED);
|
||||
first = false;
|
||||
}
|
||||
|
||||
// Only auto-start discovery if requested AND there are no paired devices
|
||||
bool discovery_started = false;
|
||||
if (start_discovery && device_list->count == 0) {
|
||||
ESP_LOGI(TAG, "Starting Bluetooth discovery (no paired devices)");
|
||||
update_bt_status("Scanning...");
|
||||
_bt_scan_start_time = xTaskGetTickCount(); // Record when scan started
|
||||
discovery_started = bt_start_discovery();
|
||||
|
||||
if (!discovery_started) {
|
||||
ESP_LOGW(TAG, "Discovery not started");
|
||||
update_bt_status("Scan failed");
|
||||
_bt_scan_start_time = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Add back/refresh/clear options
|
||||
lv_obj_t* refresh_btn = addMenuItem(_bt_page, "Refresh");
|
||||
lv_obj_add_event_cb(refresh_btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
// If no devices were listed, focus on Refresh button
|
||||
if (device_list->count == 0) {
|
||||
lv_obj_add_state(refresh_btn, LV_STATE_FOCUSED);
|
||||
// Populate with current device list
|
||||
if (device_list->count > 0) {
|
||||
// Clear focus from Back button, will be set on first device
|
||||
lv_obj_t* back_btn = lv_obj_get_child(_bt_device_container, 1); // Index 1 is Back (after status)
|
||||
if (back_btn) {
|
||||
lv_obj_clear_state(back_btn, LV_STATE_FOCUSED);
|
||||
}
|
||||
populate_bt_device_list();
|
||||
} else if (!discovery_started) {
|
||||
update_bt_status("No devices found");
|
||||
// Update menu context since Back button has focus
|
||||
currentFocusIndex(&_menuContext);
|
||||
} else {
|
||||
// Scanning in progress, Back button has focus
|
||||
currentFocusIndex(&_menuContext);
|
||||
}
|
||||
|
||||
lv_obj_t* clear_btn = addMenuItem(_bt_page, "Clear Paired");
|
||||
lv_obj_add_event_cb(clear_btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
lv_obj_t* back_btn = addMenuItem(_bt_page, "Back");
|
||||
lv_obj_add_event_cb(back_btn, bt_device_click_cb, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
return _bt_page;
|
||||
}
|
||||
|
||||
@@ -903,6 +1123,9 @@ static void show_bt_device_list(void) {
|
||||
_currentPage = bt_page;
|
||||
_mode = GUI_MENU; // Keep in menu mode
|
||||
|
||||
// Initialize menu context for this page
|
||||
currentFocusIndex(&_menuContext);
|
||||
|
||||
menu_hide_headers(menu);
|
||||
|
||||
|
||||
@@ -916,35 +1139,11 @@ static void show_bt_device_list(void) {
|
||||
}
|
||||
|
||||
static void refresh_bt_device_list(void) {
|
||||
ESP_LOGI(TAG, "Refreshing Bluetooth device list without new discovery");
|
||||
|
||||
// Don't start new discovery, just refresh the display with current devices
|
||||
bool should_start_discovery = false;
|
||||
|
||||
lv_obj_t* menu = create_menu_container();
|
||||
if (!menu) {
|
||||
ESP_LOGE(TAG, "Failed to create menu container for Bluetooth list");
|
||||
return;
|
||||
}
|
||||
|
||||
lv_obj_t* bt_page = create_bt_device_page(should_start_discovery);
|
||||
if (!bt_page) {
|
||||
ESP_LOGE(TAG, "Failed to create Bluetooth device page");
|
||||
return;
|
||||
}
|
||||
|
||||
lv_menu_set_page(menu, bt_page);
|
||||
_currentPage = bt_page;
|
||||
_mode = GUI_MENU; // Keep in menu mode
|
||||
|
||||
menu_hide_headers(menu);
|
||||
|
||||
|
||||
lv_obj_remove_flag(menu, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(_bubble, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
|
||||
ESP_LOGI(TAG, "Refreshing Bluetooth device list with discovered devices");
|
||||
|
||||
// Clear old devices and repopulate with discovered devices
|
||||
clear_bt_device_list();
|
||||
populate_bt_device_list();
|
||||
|
||||
ESP_LOGI(TAG, "Bluetooth device list refreshed");
|
||||
}
|
||||
@@ -1345,13 +1544,20 @@ static void gui_task(void *pvParameters)
|
||||
pdMS_TO_TICKS(10));
|
||||
|
||||
if (notifiedBits & EM_EVENT_BT_DISCOVERY_COMPLETE) {
|
||||
// Discovery completed - refresh the BT page if we're on it
|
||||
if (_mode == GUI_MENU && _currentPage == _bt_page) {
|
||||
ESP_LOGI(TAG, "Discovery complete, refreshing Bluetooth page");
|
||||
LOCK();
|
||||
// Recreate the BT page with updated device list (without starting new discovery)
|
||||
refresh_bt_device_list();
|
||||
UNLOCK();
|
||||
// Discovery completed - refresh the BT page if we're on it and scan has been running long enough
|
||||
// Ignore spurious early "complete" events from stopping old discovery (must be > 500ms since start)
|
||||
if (_mode == GUI_MENU && _currentPage == _bt_page && _bt_scan_start_time > 0) {
|
||||
TickType_t elapsed = xTaskGetTickCount() - _bt_scan_start_time;
|
||||
if (elapsed > pdMS_TO_TICKS(500)) {
|
||||
ESP_LOGI(TAG, "Discovery complete after %dms, refreshing Bluetooth page", (int)pdTICKS_TO_MS(elapsed));
|
||||
LOCK();
|
||||
_bt_scan_start_time = 0; // Clear scan timestamp
|
||||
// Recreate the BT page with updated device list (without starting new discovery)
|
||||
refresh_bt_device_list();
|
||||
UNLOCK();
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Ignoring spurious discovery complete event (only %dms elapsed)", (int)pdTICKS_TO_MS(elapsed));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user