How can I toggle Bluetooth on and off via a shortcut on Windows 10 1903

8,114

First create an .ahk shortcut that starts a powershell:

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

Then you create a 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

All credit goes to @Ben N and @Scott Heath


This script works when I launch it from VScode, when I copy-paste it in a powershell, or when I use a cmd to start it. But not when I double-click it or when I start it in .ahk. The work around was to create a .bat file with this

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

And then call this .bat in ahk.

Share:
8,114

Related videos on Youtube

MagTun
Author by

MagTun

Updated on September 18, 2022

Comments

  • MagTun
    MagTun over 1 year

    I would like to emulated with a shortcut the process of 1) opening the Action Center and 2) clicking on the bluetooth icon.

    My solution was to use AHK to map a keyboard shortcut to running a .bat, which contains the script suggested in this question.

    But the suggested service doesn't activate/remove the magic little blue icon of the bluetooth in the tray bar.enter image description here

    I have look for all bluetooth services that are turned on when I click on the bluetooth icon in action center and I have activated them via the suggested .bat, but still it's not working.

    BluetoothUserService_182d916
    bthserv
    BthHFSrv  
    BthHFEnum  
    BthEnum   
    BthHFAud
    BthEnum
    BthA2dp
    Microsoft_Bluetooth_AvrcpTransport
    

    Here are all the services: enter image description here enter image description here

    My script (where I have replace Microsoft_Bluetooth_AvrcpTransport by all the service mentionned above):

    @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
    
  • MagTun
    MagTun over 4 years
    Sorry I was clear enough, I do not want to get rid of the blue bluetooth icon, I want to start /stop the bluetooth, and when the bluetooth is on I want the blue icon, and when the bluetooth is stopped (and only then) I do not want the blue icon.
  • harrymc
    harrymc over 4 years
    What was wrong with the post in a comment above?
  • MagTun
    MagTun over 4 years
    The answer in the post works but I have some difficulties to adapt it so I can use it with a shortcut : superuser.com/questions/1494499
  • harrymc
    harrymc over 4 years
    If that script works, the AutoHotKey command can be improved as Run, powershell C:\path\to\bluetooth.ps1 On and another shortcut for Off.
  • MagTun
    MagTun over 4 years
    Yes, I could do that (which I am already doing) but I would like to use just one shortcut.
  • harrymc
    harrymc over 4 years
    Easy enough to add a toggle to the AutoHotKey script so it does On/Off. Do you need help with that?
  • MagTun
    MagTun over 4 years
    Thanks for your patience @harrymc! Yes, your help is welcome because I could find a way to make my AutoHotKey script check the status of the bluetooth (so that it "knows" if it needs to send Run, powershell C:\path\to\bluetooth.ps1 On or Run, powershell C:\path\to\bluetooth.ps1 Off
  • MagTun
    MagTun over 4 years
    Thanks a lot for your help @harrymc, I ended up using a solution in powershell.
  • Liam Lime
    Liam Lime almost 3 years
    If the quoted powershell script doesn't work, as it didn't in my case, remove ` | Out-Null` from the final line and re-run, if it prints DeniedByUser then search for "Radio Privacy Settings" in start menu and allow apps to control radios.