欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

python 判断 蓝牙是否 禁用 AG chatgpt 有大用 有大大用

推荐的方法:使用 Windows PowerShell 命令判断蓝牙是否启用

Windows 提供了标准命令:

powershell "Get-PnpDevice -Class Bluetooth"


只要蓝牙被禁用,状态会显示:

  • Disabled

  • 或者设备不存在


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

image.png

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

image.png

下面代码是 # OK  有大用

import subprocess

def is_bluetooth_disabled():
    result = subprocess.run(
        ['powershell', '-Command', r'Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth\(R\)" } | 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("蓝牙已启用")


image.png

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

image.png


image.png

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

image.png



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', r'Get-PnpDevice -Class Bluetooth | Where-Object { $_.FriendlyName -match "Bluetooth\(R\)" } | 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())




在 windows 上如何禁用蓝牙

下面给你提供 Windows 上禁用蓝牙的所有可行方法(含 CMD / PowerShell / UI / Python),你可以按你的环境选择使用。


🟦 方法 1:在 Windows 设置里禁用蓝牙(最直接)#这是关闭蓝牙,不是禁用蓝牙

路径:

设置 → 设备 → 蓝牙和其他设备 → 蓝牙开关 → 关闭



🟦 方法 2:在设备管理器中禁用蓝牙适配器

  1. Win + R → 输入:devmgmt.msc

  2. 找到 蓝牙

  3. 找到蓝牙适配器,例如:

  • Intel Wireless Bluetooth

  • Realtek Bluetooth Adapter

  • 右键 → 禁用设备


  • 🟦 方法 3:使用 PowerShell 禁用蓝牙(强制、可脚本自动化)

    需要管理员权限。

    ✔ 禁用蓝牙(适配器)

    Get-PnpDevice -Class Bluetooth | Disable-PnpDevice -Confirm:$false


    ✔ 启用蓝牙

    Get-PnpDevice -Class Bluetooth | Enable-PnpDevice -Confirm:$false



    🟦 方法 4:使用 CMD 禁用蓝牙(通过 DevCon)

    DevCon 是微软官方工具(Windows Driver Kit 内)

    禁用蓝牙:

    devcon disable *BTH*


    启用蓝牙:

    devcon enable *BTH*



    🟦 方法 5:使用 Python 自动禁用蓝牙(你的场景最可能需要)

    必须在 Python 中用 PowerShell 调用:

    import subprocess def disable_bluetooth():    cmd = (        'powershell -Command "Get-PnpDevice -Class Bluetooth | '        'Disable-PnpDevice -Confirm:$false"'    )    subprocess.run(cmd, shell=True) disable_bluetooth()


    👉 运行前务必:
    右键 Pycharm → 以管理员身份运行
    否则 PowerShell 无法禁用系统设备。


    🟦 方法 6:使用 GPO 禁用蓝牙(企业环境常用)

    适用于公司环境:

    gpedit.msc


    路径:

    计算机配置 → 管理模板 → 网络 → Windows 连接管理器


    禁用:

    • 禁止使用蓝牙

    • 禁止启用移动宽带连接(可影响蓝牙)


    🟦 方法 7:注册表禁用蓝牙(部分系统适用)

    ⚠️ 不推荐,某些版本可能无效。

    HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\BTHPORT\Parameters


    添加:

    Disable = 1 (DWORD)



    🟢 你需要我给你做吗?

    我可以帮你做:

    ✔ 一个 Python 脚本(直接禁用蓝牙)

    ✔ 一个 EXE 工具(点一下禁用蓝牙)

    ✔ 一个 GUI 工具(带按钮:禁用 / 启用)

    ✔ 一个自动检测蓝牙状态 → 自动禁用的工具

    你想要哪一种?



    cmd = (

     'powershell -Command "(Get-NetAdapter -InterfaceDescription *bluetooth* ' 

     '| Select-Object -ExpandProperty Status)"' 

     ) 

    详解

    详解(逐段拆解并解释)

    你给出的 Python 字符串:

    cmd = (    'powershell -Command "(Get-NetAdapter -InterfaceDescription *bluetooth* '    '| Select-Object -ExpandProperty Status)"' )


    这是在 Python 中构建要传给 subprocess 的 PowerShell 命令。下面把整个命令拆开并逐项解释。


    1) 外层:powershell -Command "…"

    • powershell:启动 PowerShell 可执行程序(在 Windows 上)。

    • -Command "...":告诉 PowerShell 执行引号内的命令字符串,然后退出。

    • 在 Python 字符串里要注意引号配对:这里外面用单引号包 Python 字符串,PowerShell 命令用双引号包起来,避免冲突。


    2) ( ... )(括号)

    • 把整个 PowerShell 管道表达式放在一对括号里不改变结果,但有时能避免解析歧义(尤其在复杂命令或内嵌脚本时)。可有可无。


    3) Get-NetAdapter

    • PowerShell 的一个 cmdlet(来自 NetAdapter 模块),用于列出网络适配器及其属性(名称、接口描述、状态、链接速度等)。

    • 返回的是对象(不是纯文本),因此可以通过管道把它交给后续的 cmdlet 做处理。


    4) -InterfaceDescription *bluetooth*

    • 这是 Get-NetAdapter 的一个过滤参数,按 InterfaceDescription 字段进行匹配。

    • *bluetooth* 使用通配符(类似文件名匹配),意思是“接口描述中包含 bluetooth(不区分大小写)”。

    • 例子:InterfaceDescription 可能是 Intel(R) Wireless Bluetooth(R)Bluetooth Device (RFCOMM Protocol TDI) 等,包含 bluetooth 就会匹配到。

    • 注意:如果你的适配器描述里不包含英文 bluetooth(例如中文或厂商自定义描述),这个过滤会找不到任何适配器 -> 返回空。


    5) 管道 |(pipeline)

    • Get-NetAdapter 输出的对象逐个传给管道右端的命令 Select-Object。这是 PowerShell 常见用法。


    6) Select-Object -ExpandProperty Status

    • Select-Object 用来选取对象的属性。

    • -ExpandProperty Status 表示直接取出 Status 属性并将其“展开”成纯量值(字符串),而不是仍然保留对象结构。

    • Status 常见取值:UpDownDisabledNot Present 等(视系统和驱动而定)。

    • 结果:命令输出将只包含适配器的状态文本(一行或多行),例如:

      Up Down



    7) 合并解释(整句含义)

    这个 PowerShell 命令的意思是:

    查找接口描述中包含 bluetooth 的网络适配器,然后只输出它们的 Status(状态),便于在脚本中用这个纯文本结果判断蓝牙是否开启/关闭。


    8) 在 Python subprocess 中的使用建议与示例

    import subprocess cmd = (    'powershell -Command "(Get-NetAdapter -InterfaceDescription *bluetooth* '    '| Select-Object -ExpandProperty Status)"' ) proc = subprocess.run(cmd, shell=True, capture_output=True, text=True, encoding='mbcs', errors='ignore') output = proc.stdout.strip().lower() print(repr(output))


    • shell=True:如果传的是单个字符串并依赖 shell 解析,可使用,但注意安全(不要拼接不受信的输入)。

    • encoding='mbcs':Windows 默认编码,防止解码错误(你之前遇到的问题)。

    • strip().lower():去掉首尾空白并小写,方便比较。

    9) 常见返回值及如何判断(实用逻辑)

    • up → 适配器已启用且正在运行(蓝牙开关可能为 ON)。

    • downdisabled → 适配器被禁用或连接断开(蓝牙为 OFF / 适配器禁用)。

    • 空输出(没匹配到任何适配器) → 系统没有可识别的 Bluetooth 网卡(也视为禁用或不存在)。

    • 多行输出 → 系统有多个“Bluetooth”相关的适配器 / 子接口,每行对应一个,通常检查是否至少有一行为 up 即可判定为“启用”。

    示例判断逻辑(伪码):

    if not output:    bluetooth_present = False elif 'up' in output:    bluetooth_on = True else:    bluetooth_on = False

    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 例子(匹配更多情况):

    Get-NetAdapter | Where-Object { $_.InterfaceDescription -match 'bluetooth' -or $_.Name -match 'bluetooth' } | Select-Object -ExpandProperty Status

    11) 小结(快速备忘)

    • 这条命令就是:找接口描述含 bluetooth 的网卡,输出它们的 Status(Up/Down/...)。

    • 在脚本里要处理:空输出、多行输出、大小写、编码,以及适配器描述可能不是 bluetooth 的情况。

    • 如果你想更稳健,我可以把这条命令改成更鲁棒的版本并给出完整的 Python 检测函数(包括中文/厂商名匹配、多适配器处理和示例输出)。要我直接写吗?





    普通分类: