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("蓝牙已启用")
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())