How to toggle Win10 bluetooth off and on at startup

bluetoothpowershell

I have a Win10 laptop that I move back and forth between home and work. I use identical-but-separate bluetooth mice at each location. Each time I switch locations (and thus switch bluetooth mice), I must go into Settings to toggle of, and back on again, the bluetooth "switch" to make the laptop connect to the new mouse (even though it shows up as already "paired" in the list of bluetooth devices). Note this is the case even across reboots.

Of course my first thought to automate this process was to just use a script to bounce the bluetooth service(s) at boot time to emulate the turning off and back on again the bluetooth switch in the Settings page. But it seems I'm failing to identify the right service (if in fact that is the right approach) to toggle. I've used powershell to stop "bthserv" and "ibtsiva", but my mouse is still happily working fine, so obviously that is not equivalent to turning "off" the bluetooth switch.

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

Is there some other service I should be toggling? Or is the service just completely the wrong approach? Forcing the hardware to shut down and start again works every time, so it definitely includes whatever action I need to do. I just need to find a way to automate it. Any pointers?

Best Answer

See this Q&A …

Turn on/off Bluetooth radio/adapter from cmd/powershell in Windows 10

[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
Related Question