/Set-BluetoothStatus.ps1 Secret
Created
October 25, 2020 09:38
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param ( | |
[string]$BluetoothStatus | |
) | |
Add-Type -TypeDefinition @' | |
using System; | |
using System.Runtime.InteropServices; | |
public class MediaHelper | |
{ | |
public static bool IsPlayingSound() | |
{ | |
IMMDeviceEnumerator enumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); | |
IMMDevice speakers = enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); | |
IAudioMeterInformation meter = (IAudioMeterInformation)speakers.Activate(typeof(IAudioMeterInformation).GUID, 0, IntPtr.Zero); | |
float value = meter.GetPeakValue(); | |
return value > 1E-08; | |
} | |
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] | |
private class MMDeviceEnumerator | |
{ | |
} | |
private enum EDataFlow | |
{ | |
eRender, | |
eCapture, | |
eAll, | |
} | |
private enum ERole | |
{ | |
eConsole, | |
eMultimedia, | |
eCommunications, | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")] | |
private interface IMMDeviceEnumerator | |
{ | |
void NotNeeded(); | |
IMMDevice GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role); | |
// the rest is not defined/needed | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("D666063F-1587-4E43-81F1-B948E807363F")] | |
private interface IMMDevice | |
{ | |
[return: MarshalAs(UnmanagedType.IUnknown)] | |
object Activate([MarshalAs(UnmanagedType.LPStruct)] Guid iid, int dwClsCtx, IntPtr pActivationParams); | |
// the rest is not defined/needed | |
} | |
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064")] | |
private interface IAudioMeterInformation | |
{ | |
float GetPeakValue(); | |
// the rest is not defined/needed | |
} | |
} | |
'@ | |
if ([MediaHelper]::IsPlayingSound()) { return; } | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment