← Back to Home

Fixing BLE Scan UI Freeze with asyncio

2026.09.21 · Python · 4 min read

Calling bleak's BLE scan from synchronous code freezes the entire UI. Once you bridge it through an asyncio event loop, the UI stays responsive while scanning and connecting.

The Problem: UI Freezes During a BLE Scan

The BLE dashboard I built needs to connect to devices and read/write GATT. bleak is the go-to Python BLE library, but it's asynchronous by design:

BleakScanner.discover()   # coroutine
BleakClient.connect()     # coroutine

These functions return a "coroutine object," not a result. If you call them directly the usual synchronous way, one of two things happens: either there's no event loop to run them, or the whole flow blocks and waits there — and the UI stops responding.

The reason isn't hard to see: a BLE scan is fundamentally "waiting" — waiting for a device to advertise, for a connection to establish, for a GATT response. Synchronous code can't do anything else while it waits, so the screen simply freezes.

The Fix: Bridge in an Event Loop

asyncio fits I/O-bound work (waiting on the network, files, Bluetooth). It works by using a single event loop to schedule tasks: while one task is waiting on I/O, the loop goes off to handle something else.

To run a coroutine from synchronous code, there are only three steps:

import asyncio
from bleak import BleakScanner

# ① Create a new event loop
loop = asyncio.new_event_loop()

# ② Set it as the current thread's event loop
asyncio.set_event_loop(loop)

# ③ Run the coroutine until it completes
devices = loop.run_until_complete(BleakScanner.discover(timeout=5.0))

Wrap it in a function, with resource cleanup:

def scan_ble_devices():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        return loop.run_until_complete(
            BleakScanner.discover(timeout=5.0)
        )
    finally:
        loop.close()

Connecting and reading a GATT characteristic follows the same pattern:

async def read_gatt(address, char_uuid):
    async with BleakClient(address) as client:
        if client.is_connected:
            return await client.read_gatt_char(char_uuid)

def do_read(address, char_uuid):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    try:
        return loop.run_until_complete(read_gatt(address, char_uuid))
    finally:
        loop.close()

What Each Function Does

FunctionPurpose
new_event_loop()Creates a new event loop (the scheduler for async tasks)
set_event_loop()Sets it as the default loop for the current thread
run_until_complete()Runs a coroutine to completion — the bridge between sync and async

Pitfalls I Hit

The Pattern for GUI Scenarios

import threading

def run_async_in_thread(coro):
    def worker():
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        try:
            loop.run_until_complete(coro)
        finally:
            loop.close()

    threading.Thread(target=worker, daemon=True).start()

Result

After switching to the event-loop bridge, the UI stays usable while BLE scanning and connecting, and the scan results come back correctly. Flask handles each request on its own thread, so using this pattern directly inside a route works.

What Could Be Improved

Key Takeaways