推荐的方法:使用 Windows PowerShell 命令判断蓝牙是否启用
Windows 提供了标准命令:
只要蓝牙被禁用,状态会显示:
在设备管理器中启用蓝牙后 执行 powershell "Get-PnpDevice -Class Bluetooth"

在设备管理器中禁用蓝牙后 执行 powershell "Get-PnpDevice -Class Bluetooth"

下面代码是 # OK 有大用
import subprocess
def is_bluetooth_disabled():
result = subprocess.run(
['powershell', '-Command', 'Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth" } | Format-Table -HideTableHeaders Status'],
capture_output=True, text=True, encoding='mbcs', errors='ignore'
)
output = result.stdout.lower().strip()
# 如果包含 OK 就是启用
if "ok" in output:
return False
# 没有输出蓝牙,视为禁用
if not output:
return True
# 任意一行含 error → 禁用
if "error" in output:
return True
# 全部都是 unknown → 也视为禁用
lines = [line.strip() for line in output.splitlines() if line.strip()]
if all(line == "unknown" for line in lines):
return True
# 其他情况 → 按禁用处理
return True
if is_bluetooth_disabled():
print("蓝牙被禁用")
else:
print("蓝牙已启用")

上图蓝牙关闭时 Get-NetAdapter -InterfaceDescription *bluetooth* | Select-Object -ExpandProperty Status 输出为空


上图蓝牙打开时 Get-NetAdapter -InterfaceDescription *bluetooth* | Select-Object -ExpandProperty Status 输出为 Disconnected 不太理解 ,,,为什么 不是 Up 或 Connected

Get-NetAdapter -InterfaceDescription *bluetooth* | Select-Object -ExpandProperty Status
当蓝牙关闭时,,输出为空,,,
当蓝牙关闭时,,输出有内容,,,
用python实现上面的功能
如你需要,我可以提供:
判断蓝牙是否 打开开关
判断蓝牙服务是否 正在运行
启用/禁用蓝牙设备(需管理员权限)
获取已配对蓝牙设备列表
你想继续添加这些功能吗?
想
下面的代码 # OK 有大用
import subprocess
def bluetooth_adapter_exists():
result = subprocess.run(
['powershell', '-Command', 'Get-PnpDevice -Class Bluetooth'],
capture_output=True, text=True, encoding="mbcs", errors="ignore"
)
return "bluetooth" in result.stdout.lower()
def is_bluetooth_disabled():
result = subprocess.run(
['powershell', '-Command', 'Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth" } | Format-Table -HideTableHeaders Status'],
capture_output=True, text=True, encoding="mbcs", errors="ignore"
)
out = result.stdout.lower()
# 如果包含 OK 就是启用
if "ok" in out:
return False
if "error" in out:
return True
if "disabled" in out:
return True
if "bluetooth" not in out:
return True
return False
def is_bluetooth_radio_on():
cmd = (
'powershell -Command "(Get-NetAdapter -InterfaceDescription *bluetooth* '
'| Select-Object -ExpandProperty Status)"'
)
result = subprocess.run(
cmd, capture_output=True, text=True, encoding="mbcs", errors="ignore"
)
out = result.stdout.lower()
if not out:
print(f"蓝牙适配器状态: 关闭")
else:
print("蓝牙适配器状态:打开") # 通常可能是 "Up" 或 "Connected"?感觉不太对 Disconnected 好像也是蓝牙打开的状态
return True
def is_bluetooth_service_running():
result = subprocess.run(
['powershell', '-Command', 'Get-Service bthserv'],
capture_output=True, text=True, encoding="mbcs", errors="ignore"
)
return "running" in result.stdout.lower()
def is_bluetooth_service_running():
result = subprocess.run(
['powershell', '-Command', 'Get-Service bthserv'],
capture_output=True, text=True, encoding="mbcs", errors="ignore"
)
return "running" in result.stdout.lower()
if __name__ == "__main__":
print("蓝牙适配器存在:", bluetooth_adapter_exists())
print("蓝牙设备是否禁用:", is_bluetooth_disabled())
print("蓝牙开关是否打开:", is_bluetooth_radio_on())
print("蓝牙服务是否运行:", is_bluetooth_service_running())
:: 单独查询远程桌面服务
sc query TermService | findstr /i "SERVICE_NAME STATE" > "%USERPROFILE%\桌面\远程桌面状态.txt"
:: 单独查询 Wi-Fi 服务
sc query WlanSvc | findstr /i "SERVICE_NAME STATE" > "%USERPROFILE%\桌面\Wi-Fi状态.txt"
:: 单独查询蓝牙服务
sc query bthserv | findstr /i "SERVICE_NAME STATE" > "%USERPROFILE%\桌面\蓝牙状态.txt"
:: 单独查询打印服务
sc query Spooler | findstr /i "SERVICE_NAME STATE" > "%USERPROFILE%\桌面\打印服务状态.txt"
在 windows 上如何禁用蓝牙
下面给你提供 Windows 上禁用蓝牙的所有可行方法(含 CMD / PowerShell / UI / Python),你可以按你的环境选择使用。
🟦 方法 1:在 Windows 设置里禁用蓝牙(最直接)#这是关闭蓝牙,不是禁用蓝牙
路径:
🟦 方法 2:在设备管理器中禁用蓝牙适配器
Win + R → 输入:devmgmt.msc
找到 蓝牙
找到蓝牙适配器,例如:
右键 → 禁用设备
🟦 方法 3:使用 PowerShell 禁用蓝牙(强制、可脚本自动化)
需要管理员权限。
✔ 禁用蓝牙(适配器)
✔ 启用蓝牙
🟦 方法 4:使用 CMD 禁用蓝牙(通过 DevCon)
DevCon 是微软官方工具(Windows Driver Kit 内)
禁用蓝牙:
启用蓝牙:
🟦 方法 5:使用 Python 自动禁用蓝牙(你的场景最可能需要)
必须在 Python 中用 PowerShell 调用:
👉 运行前务必:
右键 Pycharm → 以管理员身份运行
否则 PowerShell 无法禁用系统设备。
🟦 方法 6:使用 GPO 禁用蓝牙(企业环境常用)
适用于公司环境:
路径:
禁用:
🟦 方法 7:注册表禁用蓝牙(部分系统适用)
⚠️ 不推荐,某些版本可能无效。
添加:
🟢 你需要我给你做吗?
我可以帮你做:
✔ 一个 Python 脚本(直接禁用蓝牙)
✔ 一个 EXE 工具(点一下禁用蓝牙)
✔ 一个 GUI 工具(带按钮:禁用 / 启用)
✔ 一个自动检测蓝牙状态 → 自动禁用的工具
你想要哪一种?
cmd = (
'powershell -Command "(Get-NetAdapter -InterfaceDescription *bluetooth* '
'| Select-Object -ExpandProperty Status)"'
)
详解
详解(逐段拆解并解释)
你给出的 Python 字符串:
这是在 Python 中构建要传给 subprocess 的 PowerShell 命令。下面把整个命令拆开并逐项解释。
1) 外层:powershell -Command "…"
powershell:启动 PowerShell 可执行程序(在 Windows 上)。
-Command "...":告诉 PowerShell 执行引号内的命令字符串,然后退出。
在 Python 字符串里要注意引号配对:这里外面用单引号包 Python 字符串,PowerShell 命令用双引号包起来,避免冲突。
2) ( ... )(括号)
3) Get-NetAdapter
4) -InterfaceDescription *bluetooth*
这是 Get-NetAdapter 的一个过滤参数,按 InterfaceDescription 字段进行匹配。
*bluetooth* 使用通配符(类似文件名匹配),意思是“接口描述中包含 bluetooth(不区分大小写)”。
例子:InterfaceDescription 可能是 Intel(R) Wireless Bluetooth(R)、Bluetooth Device (RFCOMM Protocol TDI) 等,包含 bluetooth 就会匹配到。
注意:如果你的适配器描述里不包含英文 bluetooth(例如中文或厂商自定义描述),这个过滤会找不到任何适配器 -> 返回空。
5) 管道 |(pipeline)
6) Select-Object -ExpandProperty Status
Select-Object 用来选取对象的属性。
-ExpandProperty Status 表示直接取出 Status 属性并将其“展开”成纯量值(字符串),而不是仍然保留对象结构。
Status 常见取值:Up、Down、Disabled、Not Present 等(视系统和驱动而定)。
结果:命令输出将只包含适配器的状态文本(一行或多行),例如:
7) 合并解释(整句含义)
这个 PowerShell 命令的意思是:
查找接口描述中包含 bluetooth 的网络适配器,然后只输出它们的 Status(状态),便于在脚本中用这个纯文本结果判断蓝牙是否开启/关闭。
8) 在 Python subprocess 中的使用建议与示例
shell=True:如果传的是单个字符串并依赖 shell 解析,可使用,但注意安全(不要拼接不受信的输入)。
encoding='mbcs':Windows 默认编码,防止解码错误(你之前遇到的问题)。
strip().lower():去掉首尾空白并小写,方便比较。
9) 常见返回值及如何判断(实用逻辑)
up → 适配器已启用且正在运行(蓝牙开关可能为 ON)。
down 或 disabled → 适配器被禁用或连接断开(蓝牙为 OFF / 适配器禁用)。
空输出(没匹配到任何适配器) → 系统没有可识别的 Bluetooth 网卡(也视为禁用或不存在)。
多行输出 → 系统有多个“Bluetooth”相关的适配器 / 子接口,每行对应一个,通常检查是否至少有一行为 up 即可判定为“启用”。
示例判断逻辑(伪码):
10) 注意事项和替代写法
适配器描述可能不是英文 bluetooth:若你的系统/驱动把描述写成中文或厂商自定义字符串,-InterfaceDescription *bluetooth* 可能匹配不到。可以改用 Get-NetAdapter | Where-Object { $_.InterfaceDescription -match 'bluetooth' -or $_.Name -match '蓝牙' } 之类更灵活的匹配。
Get-NetAdapter 输出基于 NetAdapter 模块(现代 Windows 支持)。在非常旧的 Windows 版本上可能不存在。
Get-NetAdapter 通常不需要管理员权限 来读取状态,但某些环境或策略可能限制。
如果你只关心设备管理器的“是否被禁用”而不是网络 Status,可用 Get-PnpDevice -Class Bluetooth(你之前在用的),但它输出方式更复杂,需要解析 Status/Problem 字段。
替代更鲁棒的 PowerShell 例子(匹配更多情况):
11) 小结(快速备忘)
这条命令就是:找接口描述含 bluetooth 的网卡,输出它们的 Status(Up/Down/...)。
在脚本里要处理:空输出、多行输出、大小写、编码,以及适配器描述可能不是 bluetooth 的情况。
如果你想更稳健,我可以把这条命令改成更鲁棒的版本并给出完整的 Python 检测函数(包括中文/厂商名匹配、多适配器处理和示例输出)。要我直接写吗?