-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbluetooth_device_connector.ahk
More file actions
183 lines (156 loc) · 6.24 KB
/
Copy pathbluetooth_device_connector.ahk
File metadata and controls
183 lines (156 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#Requires AutoHotkey v2.0
#SingleInstance Force
TraySetIcon("C:\WINDOWS\system32\netshell.dll", 104)
; =====================================================================
; Configuration
; =====================================================================
; These defaults are used when the script is launched without arguments.
; audioProfile: "a2dp" keeps stereo playback only and disables Hands-Free.
; "a2dp-hfp" enables stereo playback plus the microphone.
deviceName := "AirPods Pro"
action := "connect"
audioProfile := "a2dp-hfp"
; Optional command line:
; AutoHotkey64.exe bluetooth_device_connector.ahk "Device name" [connect|disconnect] [a2dp|a2dp-hfp]
if (A_Args.Length > 0)
deviceName := A_Args[1]
if (A_Args.Length > 1)
action := A_Args[2]
if (A_Args.Length > 2)
audioProfile := A_Args[3]
action := StrLower(action)
audioProfile := StrLower(audioProfile)
if (Trim(deviceName) = "")
{
MsgBox("Set deviceName to the name of a paired Bluetooth device.", "Bluetooth Device Connector", "Iconx")
ExitApp(64)
}
if (action != "connect" && action != "disconnect")
{
MsgBox("Unknown action '" . action . "'. Expected connect or disconnect.", "Bluetooth Device Connector", "Iconx")
ExitApp(64)
}
if (audioProfile != "a2dp" && audioProfile != "a2dp-hfp")
{
MsgBox("Unknown audio profile '" . audioProfile . "'. Expected a2dp or a2dp-hfp.", "Bluetooth Device Connector", "Iconx")
ExitApp(64)
}
; Load the Windows Bluetooth Control Panel API used to manage audio services.
DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
maxRetries := 10
device := FindDeviceByName(deviceName)
if (!device)
{
MsgBox("Target Bluetooth device '" . deviceName . "' not found. Pair it in Windows first and verify its name.", "Bluetooth Device Connector", "Iconx")
ExitApp(2)
}
deviceNameActual := StrGet(device.Ptr + 64, "UTF-16")
if (action = "connect")
{
; Stereo-only mode explicitly disables HFP before enabling A2DP, preventing
; Windows from switching playback to the low-quality call profile.
hfToggleOn := (audioProfile = "a2dp-hfp") ? 1 : 0
hfStatus := ToggleBluetoothService(device, "{0000111e-0000-1000-8000-00805f9b34fb}", hfToggleOn, maxRetries)
asStatus := ToggleBluetoothService(device, "{0000110b-0000-1000-8000-00805f9b34fb}", 1, maxRetries)
}
else
{
; Disconnect both services so HFP cannot keep the device connected after
; a stereo-only session.
hfStatus := ToggleBluetoothService(device, "{0000111e-0000-1000-8000-00805f9b34fb}", 0, maxRetries)
asStatus := ToggleBluetoothService(device, "{0000110b-0000-1000-8000-00805f9b34fb}", 0, maxRetries)
}
if (IsSuccessfulOperation(action, audioProfile, hfStatus, asStatus))
{
operationLabel := (action = "connect") ? "connected" : "disconnected"
profileDetails := (action = "connect") ? "`nProfile: " . AudioProfileLabel(audioProfile) : ""
MsgBox("Bluetooth device '" . deviceNameActual . "' " . operationLabel . "." . profileDetails, "Bluetooth Device Connector", "Iconi")
ExitApp(0)
}
MsgBox("Failed to " . action . " '" . deviceNameActual . "'.`nHands-Free: " . hfStatus . "`nAudioSink: " . asStatus, "Bluetooth Device Connector", "Iconx")
ExitApp(3)
; =====================================================================
; Helper functions
; =====================================================================
AudioProfileLabel(audioProfile)
{
return (audioProfile = "a2dp") ? "Stereo only (A2DP)" : "Stereo + microphone (A2DP + Hands-Free)"
}
; Build search parameters that enumerate paired (authenticated) devices.
MakeSearchParams()
{
structSize := 24 + A_PtrSize * 2
searchParams := Buffer(structSize, 0)
NumPut("uint", structSize, searchParams, 0)
NumPut("uint", 1, searchParams, 4)
return searchParams
}
; Return the first paired device whose name contains targetName, or 0.
FindDeviceByName(targetName)
{
searchParams := MakeSearchParams()
deviceInfo := Buffer(560, 0)
NumPut("uint", 560, deviceInfo, 0)
searchHandle := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", searchParams, "ptr", deviceInfo, "ptr")
if !searchHandle
return 0
match := 0
loop
{
if (InStr(StrGet(deviceInfo.Ptr + 64, "UTF-16"), targetName))
{
match := deviceInfo
break
}
if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", searchHandle, "ptr", deviceInfo)
break
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", searchHandle)
return match
}
; Stereo-only success requires A2DP. Combined connects and disconnects require
; every exposed service to reach the requested state, while allowing devices
; that legitimately lack either Hands-Free or AudioSink.
IsSuccessfulOperation(action, audioProfile, hfStatus, asStatus)
{
if (action = "connect" && audioProfile = "a2dp")
return (asStatus = "ok" && (hfStatus = "ok" || hfStatus = "absent"))
allExposedSucceeded := (hfStatus = "ok" || hfStatus = "absent")
&& (asStatus = "ok" || asStatus = "absent")
atLeastOneProfileExists := (hfStatus = "ok" || asStatus = "ok")
return (allExposedSucceeded && atLeastOneProfileExists)
}
; Toggle one Bluetooth audio service to the desired state.
; Returns "ok", "absent", or "fail:0x...".
ToggleBluetoothService(deviceInfo, serviceGuidStr, toggleOn, maxRetries)
{
serviceGuid := Buffer(16)
DllCall("ole32\CLSIDFromString", "wstr", serviceGuidStr, "ptr", serviceGuid)
toggle := toggleOn
retryCount := 0
lastHr := 0
loop
{
hr := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", deviceInfo, "ptr", serviceGuid, "int", toggle, "uint")
lastHr := hr
if (hr = 0)
{
if (toggle = toggleOn)
return "ok"
toggle := !toggle
}
else if (hr = 87 || hr = 0x80070057)
{
if (toggle = toggleOn && toggleOn = 0)
return "ok"
toggle := !toggle
}
else if (hr = 1060)
return "absent"
else if (hr = 1168 && toggleOn = 0 && StrLower(serviceGuidStr) = "{0000111e-0000-1000-8000-00805f9b34fb}")
return "absent"
retryCount++
if (retryCount >= maxRetries)
return "fail:0x" . Format("{:08X}", lastHr)
}
}