2

I'm trying to write a cmd executable for all of my games that when started, logs into the respective launcher account, launches the game, and enables bluetooth. However, being super new to this, I'm not sure of a command that turns bluetooth on. I'm not sure if I need to be messing with pnputil, because that looks like it has something to do with bluetooth drivers. I just want to save myself the clicks of clicking the taskbar and turning bluetooth on from there, is there a way to do this in cmd?

Thanks

[I did try to research it before asking but the answers I saw didn't make sense to me :(]

emzed
  • 71

1 Answers1

4

I figured it out.

Paste the following code into notepad:

    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

Source 1

Save this as a Windows Powershell file (.ps1) with a name of your choice. Create a folder and drop this .ps1 file into it.

Then, paste the following into notepad:

@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%YourPowerShellScript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";

Source 2

Replace "YourPowerShellScript.ps1" at the end of the 3rd line with the filename you gave to your .ps1 file e.g "BluetoothSwitcher.ps1".

Save this as a Batch file (.bat) and save it into the same folder as before.

Now, in the .bat file that you want this bluetooth switcher to run, paste the following:

cd /d %YourFoldersDirectory%
start "" YourBatchFile.bat

Source 3

Replace "%YourFoldersDirectory%" with the directory of the folder with your .ps1 and .bat file - this can be found by navigating to the folder in File Explorer and selecting the directory from the address bar above. You can then paste it straight into where "%YourFoldersDirectory%" is.

Replace "YourBatchFile.bat" with the name you gave to the Batch file (.bat) created earlier e.g "btswitcher.bat".

Save and run your desired .bat and you'll see that Bluetooth alternate between ON and OFF every time you run it. If you don't want it to toggle then you'll need to click @Ben N in Source 1 and replace the powershell script with his.

Realised I did it a bit messily by running a .bat from within a .bat but it still works fine.

emzed
  • 71