bleak icon indicating copy to clipboard operation
bleak copied to clipboard

bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Operation already in progress

Open cgleiyucun opened this issue 4 months ago • 4 comments

树莓派4b python3.9.2 程序只进行扫描,不进行蓝牙连接,但出现扫描报错 bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Operation already in progress The program only performs scanning and does not establish a Bluetooth connection, but a scanning error occurs bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Operation already in progress

code:

    while True:
        try:
            devices: dict = await BleakScanner.discover(timeout=duration,
                                                        return_adv=True)  # , detection_callback=callback
            device_obj_list = fileter_device(devices)
            logger.info(f"device_mac_list:{device_obj_list}")
            task_list = [process(device) for device in device_obj_list]  # 无连接逻辑 No connect
            # run the tasks
            await asyncio.gather(*task_list)

            if (datetime.datetime.now() - start_time).seconds > 60 * 60 * 12:
                cleaner = BluetoothCacheCleaner()
                await cleaner.fast_cleanup()
                await asyncio.sleep(5)
                start_time = datetime.datetime.now()
                restart_bluetooth()
                check_restart_success()
            if (datetime.datetime.now() - ng_release_time).seconds > 60 * 60 * 24 * 3:
                ng_cgm_list.clear()
                ng_release_time = datetime.datetime.now()
            await asyncio.sleep(30)
            err_num = 0
        except Exception as e:
            logger.warning(f"扫描失败,{e}")  # 抛出异常 bleak.exc.BleakDBusError: [org.bluez.Error.Failed] Operation already in progress
            if "org.bluez.Error" in str(e) or "No powered Bluetooth adapters found" in str(e):
                start_time = datetime.datetime.now()
                restart_bluetooth()
                check_restart_success()

cgleiyucun avatar Sep 11 '25 02:09 cgleiyucun

task_list = [process(device) for device in device_obj_list]

Trying to connect to more than one device at a time is asking for trouble. Just connect to one device, then when it is connected, start the connection to the next and so on.

dlech avatar Sep 11 '25 12:09 dlech

task_list = [process(device) for device in device_obj_list]

Trying to connect to more than one device at a time is asking for trouble. Just connect to one device, then when it is connected, start the connection to the next and so on.

task_list = [process(device) for device in device_obj_list] There is no logic to connect to the device in the process method, and only the address property of BLEDevice is used

cgleiyucun avatar Sep 12 '25 01:09 cgleiyucun

I see. I should have read more carefully. Can you share the logs with the environment variable set BLEAK_LOGGING=1. Also, what does restart_bluetooth() do?

dlech avatar Sep 12 '25 01:09 dlech

The device is not with me, so I cannot provide detailed logs at the moment

def restart_bluetooth():
    # 重启蓝牙服务
    logger.warning("sudo systemctl restart bluetooth")
    try:
        subprocess.run(f"sudo systemctl restart bluetooth", shell=True,check=True)
    except Exception as e:
        logger.error(f"restart bluetooth error: {e}")

cgleiyucun avatar Sep 12 '25 01:09 cgleiyucun