我想在Windows中关闭蓝牙适配器,因为它有时会干扰其他设备。
我在Windows中使用python 3.6和PyQt5。
我尝试停止使用服务。
net stop bthserv /y
这只会停止服务,但不会关闭控制面板或任务栏中的蓝牙
无论如何,我是否可以在Windows中使用命令行或使用Python / PyQt5将其关闭?
from subprocess import TimeoutExpired, Popen, run, call, check_output, STARTUPINFO, STARTF_USESHOWWINDOW, SW_HIDE, PIPE, STDOUT
class BT:
def process_f(self, command, bsize=4096):
print('1a) Process started...')
startupinfo = STARTUPINFO()
startupinfo.dwFlags |= STARTF_USESHOWWINDOW
startupinfo.wShowWindow = SW_HIDE
p = Popen(command, stdout=PIPE, stderr=PIPE,
startupinfo=startupinfo, bufsize=bsize, universal_newlines=True)
try:
out, err = p.communicate(timeout=300)
except TimeoutExpired:
p.kill()
out, err = p.communicate()
return out.split(), err.split()
def service_switch(self, service, on=False, off=False):
""" Start or stop a service in windows """
if on:
switch = 'start'
elif off:
switch = 'stop'
else:
return False
command = 'net {} {} /y'.format(switch, service)
p = self.process_f(command)
out, err = p[0], p[1]
print('STDOUT: {}'.format(out))
print('STDERR: {}'.format(err))
bt = BT()
bt.service_switch('bthserv', on=True)
# bt.service_switch('bthserv', off=True)