如何在Win10啟動時關閉和開啟藍牙

如何在Win10啟動時關閉和開啟藍牙

我有一台 Win10 筆記型電腦,需要在家裡和工作之間來回移動。我在每個位置使用相同但獨立的藍牙滑鼠。每次我切換位置(從而切換藍牙滑鼠)時,我都必須進入“設定”來切換並再次打開藍牙“開關”,以使筆記型電腦連接到新滑鼠(即使它已經顯示為“已配對”(在藍牙設備清單中)。請注意,即使重新啟動也是如此。

當然,我的第一個想法是自動化此過程,只是使用腳本在啟動時反彈藍牙服務,以模擬「設定」頁面中藍牙開關的關閉和重新開啟。但似乎我無法確定要切換的正確服務(如果實際上這是正確的方法)。我已經使用 powershell 停止了“bthserv”和“ibtsiva”,但我的滑鼠仍然工作正常,所以顯然這並不等於“關閉”藍牙開關。

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

請參閱此問答...

在 Windows 10 中透過 cmd/powershell 開啟/關閉藍牙無線電/適配器

[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

相關內容