Skip to content

Instantly share code, notes, and snippets.

@Iaotle
Last active April 20, 2024 11:29
Show Gist options
  • Save Iaotle/cd3c90cee641b021d43dab8618d5db54 to your computer and use it in GitHub Desktop.
Save Iaotle/cd3c90cee641b021d43dab8618d5db54 to your computer and use it in GitHub Desktop.
AutoHotkey for managing bluetooth audio along with a speaker-headset pair.
; Requires VA: https://www.autohotkey.com/board/topic/21984-vista-audio-control-functions/
; Requires Bluetooth Command Line Tools: https://bluetoothinstaller.com/bluetooth-command-line-tools/BluetoothCLTools-1.2.0.56.exe
#SingleInstance
#Include %A_ScriptDir%\VA.ahk
SendMode Input
; Some logic to connect/disconnect to the BT headset, requires bluetooth command line tools
; gist taken from https://gist.github.com/joshjm/69dcef304386e928c10c9534c73c3a04
#c::
; connext to the XM4s
Run, "C:\Program Files (x86)\Bluetooth Command Line Tools\bin\btcom.exe" -r -b "88:C9:E8:67:1E:CF" -s111e ; don't use hands-free profile, audio quality sucks on it
RunWait, "C:\Program Files (x86)\Bluetooth Command Line Tools\bin\btcom.exe" -r -b "88:C9:E8:67:1E:CF" -s110b
Run, "C:\Program Files (x86)\Bluetooth Command Line Tools\bin\btcom.exe" -c -b "88:C9:E8:67:1E:CF" -s110b
Return
#+c::
; disconnect from XM4s
Run, "C:\Program Files (x86)\Bluetooth Command Line Tools\bin\btcom.exe" -r -b "88:C9:E8:67:1E:CF" -s111e
Run, "C:\Program Files (x86)\Bluetooth Command Line Tools\bin\btcom.exe" -r -b "88:C9:E8:67:1E:CF" -s110b
Return
; My thumb button on g502 is bound to F24
F24 & WheelUp::send {Volume_Up}
F24 & WheelDown::send {Volume_Down}
; Requires VA >= 2.3
getCurrentDevice()
{
return va_getdevicename(va_getdevice("playback"))
}
F24 & MButton::
; msgbox % getCurrentDevice()
current_device := getCurrentDevice() ; Sennheiser HD650 (Realtek(R) Audio) || Speakers (Logitech USB Speaker) || Headphones (WH-1000XM4)
if (current_device = "Sennheiser HD650 (Realtek(R) Audio)")
{
; wired headphones. we want to switch to the wireless headset if possible:
run nircmd setdefaultsounddevice "Headphones"
; check if it worked
sleep, 100 ; need to wait for the switcher to update the default device
current_device := getCurrentDevice()
if !(current_device = "Headphones (WH-1000XM4)")
{
; didn't work, switch to speakers
run nircmd setdefaultsounddevice "Speakers"
}
}
else if (current_device = "Speakers (Logitech USB Speaker)")
{
; speakers. same as above:
run nircmd setdefaultsounddevice "Headphones"
; check if it worked
sleep, 100 ; need to wait for the switcher to update the default device
current_device := getCurrentDevice()
if !(current_device = "Headphones (WH-1000XM4)")
{
; didn't work, switch to wired headphones
run nircmd setdefaultsounddevice "Sennheiser HD650"
}
}
else if (current_device = "Headphones (WH-1000XM4)")
{
; bluetooth headphones. want to switch to speakers 90% of the time
run nircmd setdefaultsounddevice "Speakers"
return
}
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment