起動時にWin10のBluetoothをオン/オフに切り替える方法

起動時にWin10のBluetoothをオン/オフに切り替える方法

私は自宅と職場を行き来する Win10 ラップトップを持っています。それぞれの場所で、同一だが別々の Bluetooth マウスを使用しています。場所を変えるたびに (つまり Bluetooth マウスを切り替えるたびに)、設定に移動して Bluetooth の「スイッチ」をオフにしてから再びオンにして、ラップトップを新しいマウスに接続する必要があります (Bluetooth デバイスのリストでは既に「ペアリング済み」として表示されますが)。再起動してもこの状態が続くことに注意してください。

もちろん、このプロセスを自動化するために最初に考えたのは、スクリプトを使用して起動時に Bluetooth サービスにバウンスし、設定ページで Bluetooth スイッチをオフにしてから再びオンにする操作をエミュレートすることでした。しかし、切り替える適切なサービス (実際にそれが適切なアプローチである場合) を特定できていないようです。PowerShell を使用して「bthserv」と「ibtsiva」を停止しましたが、マウスは引き続き正常に動作しているので、明らかに Bluetooth スイッチを「オフ」にすることと同じではありません。

PS C:\WINDOWS\system32> get-service -DisplayName *Bluetooth*

Status   Name               DisplayName
------   ----               -----------
Stopped  BluetoothUserSe... Bluetooth User Support Service_3b07...
Stopped  BTAGService        Bluetooth Audio Gateway Service
Stopped  bthserv            Bluetooth Support Service
Stopped  ibtsiva            Intel Bluetooth Service

切り替えるべき他のサービスはありますか? それとも、そのサービスはまったく間違ったアプローチなのでしょうか? ハードウェアを強制的にシャットダウンして再起動すると毎回機能するので、必要なアクションは間違いなく含まれています。自動化する方法を見つける必要があります。何かヒントはありますか?

答え1

このQ&Aをご覧ください…

Windows 10 の cmd/powershell から Bluetooth 無線/アダプターをオン/オフにする

[CmdletBinding()] 
Param 
(
    [Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$BluetoothStatus
)

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
Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null

関連情報