Windows 10 1903 でショートカットを使用して Bluetooth のオン/オフを切り替えるにはどうすればいいですか

Windows 10 1903 でショートカットを使用して Bluetooth のオン/オフを切り替えるにはどうすればいいですか

1) アクション センターを開き、2) Bluetooth アイコンをクリックするプロセスをショートカットでエミュレートしたいと思います。

私の解決策は、AHKを使用して、この中で提案されているスクリプトを含む.batを実行するキーボードショートカットをマッピングすることでした。質問

しかし、提案されたサービスでは、トレイ バーにある Bluetooth の魔法の小さな青いアイコンがアクティブ化/削除されません。ここに画像の説明を入力してください

アクション センターの Bluetooth アイコンをクリックしたときにオンになっているすべての Bluetooth サービスを探し、提案された方法でそれらをアクティブ化しました.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

まず最初に.ahkPowerShell を起動するショートカット:

#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

そしてこれを.batahk で呼び出します。

答え2

AutoHotkey v2.0 では、ステートメントを関数に置き換える新しい構文が導入されているため、受け入れられる答えはコピー アンド ペースト ソリューションではなくなりました。

Windows 10.0.19045 で AutoHotkey v2.0 を使用してこれを実現するには、以下を作成する必要がありました。

  1. トグル-bluetooth.ahk
#b::
{
    Run( 'C:\Users\<user>\Documents\AutoHotkey\toggle-bluetooth.bat', , 'Hide' )
    return
}

(署名では、#b::この機能にホットキーWin+Bを割り当てています。AHK ドキュメント表記の詳細についてはこちらをご覧ください。

  1. トグル-bluetooth.bat
powershell.exe -ExecutionPolicy Bypass -File toggle-bluetooth.ps1
  1. トグル-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

他のアプローチでは、 を仲介者として使用する必要がない可能性が非常に高いです.bat。しかし、これが私が機能させることができた唯一の方法です。

このソリューションを開発してくれた @MagTun と @rokdd に心から感謝します。

答え3

Bluetooth トレイバー アイコンを削除するためにこれらのサービスを停止/開始する必要はありません。

アイコンの表示は HKEY_CURRENT_USER\Control Panel\Bluetooth、レジストリ キー の DWORD 値によって 制御されます。Notification Area Iconこの値は、0オフの場合はオフ、1オンの場合はオンです。これを有効にするには、エクスプローラーを再起動する必要があります。

次の 2 つの.batファイルで目的を達成できます。

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

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

関連情報