|
| 1 | +Menu, Tray, Icon, C:\WINDOWS\system32\netshell.dll, 104 ; Set the tray icon to a network-related icon from system resources |
| 2 | + |
| 3 | +deviceName := "AirPods Pro" ; Define the name of the Bluetooth device to connect to |
| 4 | + |
| 5 | +; Dynamically loads the Bluetooth Control Panel library to use its functions |
| 6 | +DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr") |
| 7 | + |
| 8 | +; Initialize the toggle state variable and set it to 1 to represent an enabled state |
| 9 | +toggle := toggleOn := 1 |
| 10 | + |
| 11 | +; Initialize the structure for Bluetooth device search parameters with appropriate size and default values |
| 12 | +VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0) |
| 13 | +NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint") ; Set the size of the structure |
| 14 | +NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint") ; Set the search to return authenticated devices |
| 15 | + |
| 16 | +; Initialize the structure to hold information about a Bluetooth device with appropriate size |
| 17 | +VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0) |
| 18 | +NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint") ; Set the size of the structure |
| 19 | + |
| 20 | +; Loop through all found Bluetooth devices to find and connect to the specified device |
| 21 | +loop |
| 22 | +{ |
| 23 | + ; On the first iteration, use BluetoothFindFirstDevice to start the search |
| 24 | + If (A_Index = 1) |
| 25 | + { |
| 26 | + foundDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr") |
| 27 | + if !foundDevice |
| 28 | + { |
| 29 | + msgbox, No bluetooth devices found ; Display message if no Bluetooth devices are found |
| 30 | + return |
| 31 | + } |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + ; On subsequent iterations, use BluetoothFindNextDevice to continue the search |
| 36 | + if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundDevice, "ptr", &BLUETOOTH_DEVICE_INFO) |
| 37 | + { |
| 38 | + msgbox, Target Bluetooth device not found ; Display message if the specific device is not found |
| 39 | + break |
| 40 | + } |
| 41 | + } |
| 42 | + ; Check if the current device is the target device by comparing its name |
| 43 | + if (InStr(StrGet(&BLUETOOTH_DEVICE_INFO+64), deviceName)) |
| 44 | + { |
| 45 | + deviceNameActual := StrGet(&BLUETOOTH_DEVICE_INFO+64) ; Retrieve the actual name of the device |
| 46 | + |
| 47 | + ; Prepare the Handsfree service class ID for enabling/disabling the service |
| 48 | + VarSetCapacity(Handsfree, 16) |
| 49 | + DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree) |
| 50 | + |
| 51 | + ; Prepare the AudioSink service class ID for enabling/disabling the service |
| 52 | + VarSetCapacity(AudioSink, 16) |
| 53 | + DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink) |
| 54 | + |
| 55 | + ; Toggle the Handsfree service state for voice communication |
| 56 | + loop |
| 57 | + { |
| 58 | + hr := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", toggle) |
| 59 | + if (hr = 0) ; If the operation was successful |
| 60 | + { |
| 61 | + if (toggle = toggleOn) |
| 62 | + break ; Exit the loop if the service has been successfully toggled |
| 63 | + toggle := !toggle ; Toggle the state for the next attempt |
| 64 | + } |
| 65 | + if (hr = 87) ; Error code 87 indicates a parameter error, so toggle the state and retry |
| 66 | + toggle := !toggle |
| 67 | + } |
| 68 | + |
| 69 | + ; Toggle the AudioSink service state for music streaming |
| 70 | + loop |
| 71 | + { |
| 72 | + hr := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", toggle) |
| 73 | + if (hr = 0) ; If the operation was successful |
| 74 | + { |
| 75 | + if (toggle = toggleOn) |
| 76 | + { |
| 77 | + msgBox, Bluetooth device %deviceNameActual% connected ; Notify the user that the device has been connected |
| 78 | + break 2 ; Exit both loops |
| 79 | + } |
| 80 | + toggle := !toggle ; Toggle the state for the next attempt |
| 81 | + } |
| 82 | + if (hr = 87) ; Error code 87 indicates a parameter error, so toggle the state and retry |
| 83 | + toggle := !toggle |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
0 commit comments