如何在 Windows 10 1903 上透過捷徑開啟和關閉藍牙

如何在 Windows 10 1903 上透過捷徑開啟和關閉藍牙

我想用捷徑模擬 1) 開啟操作中心和 2) 點選藍牙圖示的過程。

我的解決方案是使用 AHK 將鍵盤快捷鍵對應到執行 .bat,其中包含此中建議的腳本問題

但建議的服務不會啟動/刪除托盤欄中藍牙的神奇藍色小圖示。在此輸入影像描述

我已經查找了當我單擊操作中心中的藍牙圖標時打開的所有藍牙服務,並通過建議激活了它們.bat,但仍然無法正常工作。

BluetoothUserService_182d916
bthserv
BthHFSrv  
BthHFEnum  
BthEnum   
BthHFAud
BthEnum
BthA2dp
Microsoft_Bluetooth_AvrcpTransport

以下是所有服務: 在此輸入影像描述 在此輸入影像描述

我的腳本(我已將 Microsoft_Bluetooth_AvrcpTransport 替換為上述所有服務):

@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

答案1

首先創建一個.ahk啟動 powershell 的捷徑:

#b::
Run, C:\Users\user\Desktop\bluetooth.ps1,,Hide 
return

然後建立一個 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

所有功勞歸於@本·N@斯科特希思


當我從 VScode 啟動該腳本、將其複製並貼上到 powershell 中或使用 cmd 啟動它時,該腳本就會起作用。但當我雙擊它或在.ahk.解決方法是用.bat這個建立一個文件

Run, C:\Users\user\Desktop\bluetooth.ps1,,Hide

然後.bat用 ahk 調用它。

答案2

AutoHotkey v2.0 引入了一種新語法,以函數取代語句,因此公認的答案不再是複製貼上解決方案。

為了在 Windows 10.0.19045 上使用 AutoHotkey v2.0 實現這一目標,我需要建立:

  1. 切換藍牙.ahk
#b::
{
    Run( 'C:\Users\<user>\Documents\AutoHotkey\toggle-bluetooth.bat', , 'Hide' )
    return
}

(簽名#b::將熱鍵 Win+B 指派給該函數。請參閱AHK 文件有關符號的詳細資訊。

  1. 切換藍牙.bat
powershell.exe -ExecutionPolicy Bypass -File toggle-bluetooth.ps1
  1. 切換藍牙.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

.bat使用其他方法很可能不需要使用作為中間人。但這是我唯一能開始工作的事情。

顯然要感謝@MagTun 和@rokdd 開發這個解決方案。

答案3

您無需停止/啟動這些服務即可擺脫藍牙托盤欄圖示。

圖示的顯示 HKEY_CURRENT_USER\Control Panel\Bluetooth由登錄項目 中的 DWORD 值 Notification Area Icon(其值分別0為 Off 和1On)控制。這需要重新啟動資源管理器才能生效。

以下兩個.bat文件將完成這項工作。

禁用藍牙通知區域圖標

REG ADD "HKCU\Control Panel\Bluetooth" /V "Notification Area Icon" /T REG_DWORD /D 00000000 /F
taskkill /f /im explorer.exe
start explorer.exe

啟用藍牙通知區域圖標

REG ADD "HKCU\Control Panel\Bluetooth" /V "Notification Area Icon" /T REG_DWORD /D 00000001 /F
taskkill /f /im explorer.exe
start explorer.exe

相關內容