
Gostaria de emular com um atalho o processo de 1) abrir o Action Center e 2) clicar no ícone do bluetooth.
Minha solução foi usar o AHK para mapear um atalho de teclado para executar um .bat, que contém o script sugerido nestepergunta.
Mas o serviço sugerido não ativa/remove o pequeno ícone azul mágico do bluetooth na barra da bandeja.insira a descrição da imagem aqui
Procurei todos os serviços bluetooth que estão ativados quando clico no ícone do bluetooth na central de ações e os ativei por meio do sugerido .bat
, mas ainda assim não está funcionando.
BluetoothUserService_182d916
bthserv
BthHFSrv
BthHFEnum
BthEnum
BthHFAud
BthEnum
BthA2dp
Microsoft_Bluetooth_AvrcpTransport
Aqui estão todos os serviços:
Meu script (onde substituí Microsoft_Bluetooth_AvrcpTransport por todos os serviços mencionados acima):
@echo off
for /F "tokens=3 delims=: " %%H in ('sc query "Microsoft_Bluetooth_AvrcpTransport" ^| findstr "STATE"') do (
if /I "%%H" NEQ "RUNNING" (
net start "Microsoft_Bluetooth_AvrcpTransport"
) else if /I "%%H" NEQ "STOPPED" (
net stop "Microsoft_Bluetooth_AvrcpTransport"
)
)
@pause
Responder1
Primeiro crie um.ahkatalho que inicia um PowerShell:
#b::
Run, C:\Users\user\Desktop\bluetooth.ps1,,Hide
return
Então você cria um PowerShell:
If ((Get-Service bthserv).Status -eq 'Stopped') { Start-Service bthserv }
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
[Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
[Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
$radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
$bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
[Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
if ($bluetooth.state -eq 'On') {$BluetoothStatus = 'Off'} else {$BluetoothStatus = 'On'}
Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
Todo o crédito vai para@Ben Ne@Scott Heath
Este script funciona quando eu o inicio a partir do VScode, quando eu copio e colo em um PowerShell ou quando uso um cmd para iniciá-lo. Mas não quando clico duas vezes nele ou quando inicio no formato .ahk
. A solução alternativa foi criar um .bat
arquivo com este
Run, C:\Users\user\Desktop\bluetooth.ps1,,Hide
E então chame isso .bat
em ahk.
Responder2
AutoHotkey v2.0 introduz uma nova sintaxe que substitui instruções por funções, portanto a resposta aceita não é mais uma solução de copiar e colar.
Para conseguir isso com AutoHotkey v2.0 no Windows 10.0.19045, precisei criar:
- alternar-bluetooth.ahk
#b::
{
Run( 'C:\Users\<user>\Documents\AutoHotkey\toggle-bluetooth.bat', , 'Hide' )
return
}
(A assinatura #b::
atribui a tecla de atalho Win+B para esta função. Consulte oDocumentos AHKpara obter detalhes sobre notação.)
- alternar-bluetooth.bat
powershell.exe -ExecutionPolicy Bypass -File toggle-bluetooth.ps1
- alternar-bluetooth.ps1
If ((Get-Service bthserv).Status -eq 'Stopped') { Start-Service bthserv }
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
[Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
[Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
$radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
$bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
[Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
if ($bluetooth.state -eq 'On') {$BluetoothStatus = 'Off'} else {$BluetoothStatus = 'On'}
Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
É muito possível que usar o .bat
como intermediário não seja necessário com alguma outra abordagem. Mas esta é a única coisa que consegui fazer funcionar.
Agradecimentos óbvios a @MagTun e @rokdd por desenvolver esta solução.
Responder3
Você não precisa parar/iniciar esses serviços para se livrar do ícone da barra de bandeja do Bluetooth.
A exibição do ícone é controlada na chave de registro
HKEY_CURRENT_USER\Control Panel\Bluetooth
, pelo valor DWORD
Notification Area Icon
cujo valor é 0
para Off e 1
para On. Isso requer a reinicialização do Explorer para ter efeito.
Os dois .bat
arquivos a seguir farão o trabalho.
Desative o ícone da área de notificação do Bluetooth
REG ADD "HKCU\Control Panel\Bluetooth" /V "Notification Area Icon" /T REG_DWORD /D 00000000 /F
taskkill /f /im explorer.exe
start explorer.exe
Ative o ícone da área de notificação do Bluetooth
REG ADD "HKCU\Control Panel\Bluetooth" /V "Notification Area Icon" /T REG_DWORD /D 00000001 /F
taskkill /f /im explorer.exe
start explorer.exe