使用 python 脚本打开蓝牙 On/Off

Turning Bluetooth On/Off using python script

我想使用 python 脚本打开计算机的蓝牙 on/off。这是我目前在 Internet 上找到的内容:

import os
os.system("rfkill block bluetooth")

但它似乎不起作用,因为我有一台 windows 电脑。有谁知道 os.system() 到 on/off 蓝牙的命令?谢谢!

rfkill 是 Linux 用于启用和禁用无线设备的终端命令工具,因此从逻辑上讲,它不适用于 windows。

为了使用 python 在蓝牙上工作,我建议你使用 PyBluez python 库

这里是读取本地蓝牙设备地址的示例代码:

import bluetooth

if __name__ == "__main__":
   print(bluetooth.read_local_bdaddr())

Windows Runtime Python Projection 的蓝牙功能有限。 Python/WinRT 使 Python 开发人员能够以自然而熟悉的方式直接从 Python 访问 Windows 运行时 API。

我能够使用以下代码关闭和打开蓝牙无线电:

import asyncio
from winrt.windows.devices import radios


async def bluetooth_power(turn_on):
    all_radios = await radios.Radio.get_radios_async()
    for this_radio in all_radios:
        if this_radio.kind == radios.RadioKind.BLUETOOTH:
            if turn_on:
                result = await this_radio.set_state_async(radios.RadioState.ON)
            else:
                result = await this_radio.set_state_async(radios.RadioState.OFF)


if __name__ == '__main__':
    asyncio.run(bluetooth_power(False))