Connect to bluetooth headset via shortcut Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gsf81
Posts: 5
Joined: 20 Jul 2021, 13:52

Re: Connect to bluetooth headset via shortcut

Post by gsf81 » 21 Jul 2021, 05:41

malcev wrote:
20 Jul 2021, 19:45
This script just turns on and turns off Handsfree and audio sink services for bluetooth device.
So You need to see which services are used by Your device, turn off them manually and check if it disables sound.
If You can disable them manually then You need to test does script disables them.
Also check errors for BluetoothSetServiceState dllcalls.
I appreciate the help mate! Realised certain AVRCP profiles remained connected and so didn't allow the headphones to disconnect, it's working fine now with this code to disconnect:

Code: Select all

deviceName := "ATH-ANC500BT"

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint") ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO)
      if !foundedDevice
      {
         msgbox "No bluetooth radios found"
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox "Device not found"
         break
      }
   }
   if (StrGet(&BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree) ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink)
      VarSetCapacity(GenAudServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001203-0000-1000-8000-00805F9B34FB}", "ptr", &GenAudServ)
      VarSetCapacity(HdstServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001108-0000-1000-8000-00805F9B34FB}", "ptr", &HdstServ)
      VarSetCapacity(AVRCTarget, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110C-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCTarget)
      VarSetCapacity(AVRC, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110E-0000-1000-8000-00805F9B34FB}", "ptr", &AVRC)
      VarSetCapacity(AVRCController, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110F-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCController)
      VarSetCapacity(PnP, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001200-0000-1000-8000-00805F9B34FB}", "ptr", &PnP)

      hr1 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 0) ; voice
      hr2 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 0) ; music
      ;hr3 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &GenAudServ, "int", 0) ; music
      hr4 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &HdstServ, "int", 0) ; music
      hr5 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCTarget, "int", 0) ; music
      hr6 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRC, "int", 0) ; music
      ;hr7 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCController, "int", 0) ; music
      ;hr8 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &PnP, "int", 0) ; music

      if (hr1 = 0) and (hr2 = 0) and (hr4 = 0) and (hr5 = 0) and (hr6 = 0){
         MsgBox, "Break"
         break
      }
   }
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
ExitApp
and this code to connect:

Code: Select all

deviceName := "ATH-ANC500BT"

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint") ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO)
      if !foundedDevice
      {
         msgbox "No bluetooth radios found"
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox "Device not found"
         break
      }
   }
   if (StrGet(&BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree) ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink)
      VarSetCapacity(GenAudServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001203-0000-1000-8000-00805F9B34FB}", "ptr", &GenAudServ)
      VarSetCapacity(HdstServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001108-0000-1000-8000-00805F9B34FB}", "ptr", &HdstServ)
      VarSetCapacity(AVRCTarget, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110C-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCTarget)
      VarSetCapacity(AVRC, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110E-0000-1000-8000-00805F9B34FB}", "ptr", &AVRC)
      VarSetCapacity(AVRCController, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110F-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCController)
      VarSetCapacity(PnP, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001200-0000-1000-8000-00805F9B34FB}", "ptr", &PnP)

      hr1 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 1) ; voice
      hr2 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 1) ; music
      ;hr3 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &GenAudServ, "int", 0) ; music
      hr4 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &HdstServ, "int", 1) ; music
      hr5 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCTarget, "int", 1) ; music
      hr6 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRC, "int", 1) ; music
      ;hr7 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCController, "int", 0) ; music
      ;hr8 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &PnP, "int", 0) ; music

      if (hr1 = 0) and (hr2 = 0) and (hr4 = 0) and (hr5 = 0) and (hr6 = 0){
         break
      }
   }
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
ExitApp

TheViking733n
Posts: 1
Joined: 30 Dec 2021, 11:25
Contact:

Re: Connect to bluetooth headset via shortcut

Post by TheViking733n » 30 Dec 2021, 11:33

Thank you, @malcev! You are genius
There are next level intelligent people in this world :clap:

WhenInWome
Posts: 8
Joined: 10 Sep 2020, 11:59

Re: Connect to bluetooth headset via shortcut

Post by WhenInWome » 23 Mar 2022, 12:20

gsf81 wrote:
21 Jul 2021, 05:41
malcev wrote:
20 Jul 2021, 19:45
This script just turns on and turns off Handsfree and audio sink services for bluetooth device.
So You need to see which services are used by Your device, turn off them manually and check if it disables sound.
If You can disable them manually then You need to test does script disables them.
Also check errors for BluetoothSetServiceState dllcalls.
I appreciate the help mate! Realised certain AVRCP profiles remained connected and so didn't allow the headphones to disconnect, it's working fine now with this code to disconnect:

Code: Select all

deviceName := "ATH-ANC500BT"

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint") ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO)
      if !foundedDevice
      {
         msgbox "No bluetooth radios found"
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox "Device not found"
         break
      }
   }
   if (StrGet(&BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree) ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink)
      VarSetCapacity(GenAudServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001203-0000-1000-8000-00805F9B34FB}", "ptr", &GenAudServ)
      VarSetCapacity(HdstServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001108-0000-1000-8000-00805F9B34FB}", "ptr", &HdstServ)
      VarSetCapacity(AVRCTarget, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110C-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCTarget)
      VarSetCapacity(AVRC, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110E-0000-1000-8000-00805F9B34FB}", "ptr", &AVRC)
      VarSetCapacity(AVRCController, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110F-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCController)
      VarSetCapacity(PnP, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001200-0000-1000-8000-00805F9B34FB}", "ptr", &PnP)

      hr1 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 0) ; voice
      hr2 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 0) ; music
      ;hr3 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &GenAudServ, "int", 0) ; music
      hr4 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &HdstServ, "int", 0) ; music
      hr5 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCTarget, "int", 0) ; music
      hr6 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRC, "int", 0) ; music
      ;hr7 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCController, "int", 0) ; music
      ;hr8 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &PnP, "int", 0) ; music

      if (hr1 = 0) and (hr2 = 0) and (hr4 = 0) and (hr5 = 0) and (hr6 = 0){
         MsgBox, "Break"
         break
      }
   }
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
ExitApp
and this code to connect:

Code: Select all

deviceName := "ATH-ANC500BT"

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint") ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO)
      if !foundedDevice
      {
         msgbox "No bluetooth radios found"
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox "Device not found"
         break
      }
   }
   if (StrGet(&BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree) ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink)
      VarSetCapacity(GenAudServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001203-0000-1000-8000-00805F9B34FB}", "ptr", &GenAudServ)
      VarSetCapacity(HdstServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001108-0000-1000-8000-00805F9B34FB}", "ptr", &HdstServ)
      VarSetCapacity(AVRCTarget, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110C-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCTarget)
      VarSetCapacity(AVRC, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110E-0000-1000-8000-00805F9B34FB}", "ptr", &AVRC)
      VarSetCapacity(AVRCController, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110F-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCController)
      VarSetCapacity(PnP, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001200-0000-1000-8000-00805F9B34FB}", "ptr", &PnP)

      hr1 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 1) ; voice
      hr2 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 1) ; music
      ;hr3 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &GenAudServ, "int", 0) ; music
      hr4 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &HdstServ, "int", 1) ; music
      hr5 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCTarget, "int", 1) ; music
      hr6 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRC, "int", 1) ; music
      ;hr7 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCController, "int", 0) ; music
      ;hr8 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &PnP, "int", 0) ; music

      if (hr1 = 0) and (hr2 = 0) and (hr4 = 0) and (hr5 = 0) and (hr6 = 0){
         break
      }
   }
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
ExitApp
Hi there, trying to get this to work but I can't figure out what to put in as the Device Name. Is that located in the Device Manager somewhere? I've put in exactly what it states in the Bluetooth menu but it says device not found when I run the connect script.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Connect to bluetooth headset via shortcut  Topic is solved

Post by malcev » 24 Mar 2022, 17:44

Code: Select all

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint") ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr")
      if !foundedDevice
      {
         msgbox "No bluetooth radios found"
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox "Device not found"
         break
      }
   }
   msgbox % StrGet(&BLUETOOTH_DEVICE_INFO+64)
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
ExitApp

StormtrooperDan
Posts: 1
Joined: 08 May 2022, 02:29

Re: Connect to bluetooth headset via shortcut

Post by StormtrooperDan » 08 May 2022, 02:32

Thank you for your great script. I was able to get your script to work with AHK1.1, but was not able to convert it to v2. Would be very grateful if you could update it for v2.

DesertDwarf
Posts: 3
Joined: 19 Jan 2019, 15:49

Re: Connect to bluetooth headset via shortcut

Post by DesertDwarf » 02 Jul 2022, 18:04

malcev wrote:
24 Nov 2020, 22:52
Found time to read about this api.
https://docs.microsoft.com/en-us/windows/win32/api/_bluetooth/
It does not really connects, but enable/disable bluetooth services for device - therefore connects/disconnects it.
The same api uses btcom I think.
Connection can be done like this

Code: Select all

deviceName := "jbl clip 3"

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
toggle := toggleOn := 1
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint")   ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr")
      if !foundedDevice
      {
         msgbox no bluetooth devices
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox no found
         break
      }
   }
   if (StrGet(&BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree)   ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink)
      loop
      {
         hr := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", toggle)   ; voice
         if (hr = 0)
         {
            if (toggle = toggleOn)
               break
            toggle := !toggle
         }
         if (hr = 87)
            toggle := !toggle
      }
      loop
      {
         hr := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", toggle)   ; music
         if (hr = 0)
         {
            if (toggle = toggleOn)
               break 2
            toggle := !toggle
         }
         if (hr = 87)
            toggle := !toggle
      }
   }
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
msgbox done
ExitApp
Also connection could be done using winsock bluetooth api.
https://docs.microsoft.com/en-us/windows/win32/bluetooth/bluetooth-programming-with-windows-sockets
Strange that connect method returns error with device address error, but really connects but We should send it in loop for about 10 seconds
Dont know why - complex api...

Code: Select all

deviceName := "jbl clip 3"

DllCall("LoadLibrary", "str", "ws2_32.dll", "ptr")
VarSetCapacity(WSADATA, 394 + (A_PtrSize - 2) + A_PtrSize, 0)
DllCall("ws2_32\WSAStartup", "ushort", 0x0202, "ptr", &WSADATA)
size := 1024
VarSetCapacity(WSAQUERYSETW, size, 0)
NumPut(size, WSAQUERYSETW, 0, "uint")
NumPut(NS_BTH := 16, WSAQUERYSETW, A_PtrSize*5, "uint")
DllCall("ws2_32\WSALookupServiceBeginW", "ptr", &WSAQUERYSETW, "uint", LUP_CONTAINERS := 0x0002, "ptr*", lphLookup)
loop
{
   if (DllCall("ws2_32\WSALookupServiceNextW", "ptr", lphLookup, "uint", 0x0002|0x0010|0x0100, "uint*", size, "ptr", &WSAQUERYSETW) = -1)   ; LUP_CONTAINERS|LUP_RETURN_NAME|LUP_RETURN_ADDR
   {
      error := DllCall("ws2_32\WSAGetLastError")
      if (error = 10110)   ; WSA_E_NO_MORE https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2
         msgbox device not found
      else
         msgbox % error
      exitapp
   }
   if (strget(numget(WSAQUERYSETW, A_PtrSize)) = deviceName)
   {
      socket := DllCall("ws2_32\socket", "int", AF_BTH := 32, "int", SOCK_STREAM := 1, "int", BTHPROTO_RFCOMM := 3)
      VarSetCapacity(SOCKADDR_BTH, 36, 0)
      NumPut(AF_BTH := 32, SOCKADDR_BTH, 0, "ushort")
      DllCall("RtlMoveMemory", "ptr", &SOCKADDR_BTH+2, "ptr", numget(numget(WSAQUERYSETW, A_PtrSize*12)+0, A_PtrSize*2)+2, int, 6)   ; lpcsaBuffer.RemoteAddr.lpSockaddr.BLUETOOTH_ADDRESS_STRUCT
      DllCall("ole32\CLSIDFromString", "wstr", "{00000003-0000-1000-8000-00805F9B34FB}", "ptr", &SOCKADDR_BTH+16)   ; RFCOMM
      loop 10
      {
         DllCall("ws2_32\connect", "int", socket, "ptr", &SOCKADDR_BTH, "int", 36)
         sleep 1500
      }
      DllCall("ws2_32\closesocket", "int", socket)
      DllCall("ws2_32\WSALookupServiceEnd", "ptr", lphLookup)
      break
   }
}
msgbox done
exitapp

I'm trying to use this but I'm curious about something. I can't figure out what the "A_Index" variable is for?

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Connect to bluetooth headset via shortcut

Post by boiler » 02 Jul 2022, 18:09

Click on it for an explanation in the documentation.

DesertDwarf
Posts: 3
Joined: 19 Jan 2019, 15:49

Re: Connect to bluetooth headset via shortcut

Post by DesertDwarf » 02 Jul 2022, 21:28

boiler wrote:
02 Jul 2022, 18:09
Click on it for an explanation in the documentation.
Click on what? The Microsoft documentation? That implies that a dllcall assigns a value to a variable named A_Index inside an AHK script. There is only one use of A_Index anywhere, which is in the if statement it's used. My question should really have been, "Where does A_Index have its value assigned?"

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Connect to bluetooth headset via shortcut

Post by boiler » 02 Jul 2022, 21:31

Click on A_Index in the code box. Most AHK keywords in the code boxes are linked to the page where they are described in the documentation. It directly answers where A_Index gets its value assigned.

DesertDwarf
Posts: 3
Joined: 19 Jan 2019, 15:49

Re: Connect to bluetooth headset via shortcut

Post by DesertDwarf » 02 Jul 2022, 21:33

boiler wrote:
02 Jul 2022, 21:31
Click on A_Index in the code box. Most AHK keywords in the code boxes are linked to the page where they are described in the documentation. It directly answers where A_Index gets its value assigned.
Well, I'll be jiggered. I didn't know that. Sorry. :-)

ahkonfused
Posts: 9
Joined: 29 Jun 2022, 21:22

Re: Connect to bluetooth headset via shortcut

Post by ahkonfused » 03 Jul 2022, 10:40

gsf81 wrote:
21 Jul 2021, 05:41
malcev wrote:
20 Jul 2021, 19:45
This script just turns on and turns off Handsfree and audio sink services for bluetooth device.
So You need to see which services are used by Your device, turn off them manually and check if it disables sound.
If You can disable them manually then You need to test does script disables them.
Also check errors for BluetoothSetServiceState dllcalls.
I appreciate the help mate! Realised certain AVRCP profiles remained connected and so didn't allow the headphones to disconnect, it's working fine now with this code to disconnect:

Code: Select all

deviceName := "ATH-ANC500BT"

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint") ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO)
      if !foundedDevice
      {
         msgbox "No bluetooth radios found"
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox "Device not found"
         break
      }
   }
   if (StrGet(&BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree) ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink)
      VarSetCapacity(GenAudServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001203-0000-1000-8000-00805F9B34FB}", "ptr", &GenAudServ)
      VarSetCapacity(HdstServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001108-0000-1000-8000-00805F9B34FB}", "ptr", &HdstServ)
      VarSetCapacity(AVRCTarget, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110C-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCTarget)
      VarSetCapacity(AVRC, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110E-0000-1000-8000-00805F9B34FB}", "ptr", &AVRC)
      VarSetCapacity(AVRCController, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110F-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCController)
      VarSetCapacity(PnP, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001200-0000-1000-8000-00805F9B34FB}", "ptr", &PnP)

      hr1 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 0) ; voice
      hr2 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 0) ; music
      ;hr3 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &GenAudServ, "int", 0) ; music
      hr4 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &HdstServ, "int", 0) ; music
      hr5 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCTarget, "int", 0) ; music
      hr6 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRC, "int", 0) ; music
      ;hr7 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCController, "int", 0) ; music
      ;hr8 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &PnP, "int", 0) ; music

      if (hr1 = 0) and (hr2 = 0) and (hr4 = 0) and (hr5 = 0) and (hr6 = 0){
         MsgBox, "Break"
         break
      }
   }
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
ExitApp
and this code to connect:

Code: Select all

deviceName := "ATH-ANC500BT"

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint") ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO)
      if !foundedDevice
      {
         msgbox "No bluetooth radios found"
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox "Device not found"
         break
      }
   }
   if (StrGet(&BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree) ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink)
      VarSetCapacity(GenAudServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001203-0000-1000-8000-00805F9B34FB}", "ptr", &GenAudServ)
      VarSetCapacity(HdstServ, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001108-0000-1000-8000-00805F9B34FB}", "ptr", &HdstServ)
      VarSetCapacity(AVRCTarget, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110C-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCTarget)
      VarSetCapacity(AVRC, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110E-0000-1000-8000-00805F9B34FB}", "ptr", &AVRC)
      VarSetCapacity(AVRCController, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110F-0000-1000-8000-00805F9B34FB}", "ptr", &AVRCController)
      VarSetCapacity(PnP, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{00001200-0000-1000-8000-00805F9B34FB}", "ptr", &PnP)

      hr1 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 1) ; voice
      hr2 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 1) ; music
      ;hr3 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &GenAudServ, "int", 0) ; music
      hr4 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &HdstServ, "int", 1) ; music
      hr5 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCTarget, "int", 1) ; music
      hr6 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRC, "int", 1) ; music
      ;hr7 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AVRCController, "int", 0) ; music
      ;hr8 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &PnP, "int", 0) ; music

      if (hr1 = 0) and (hr2 = 0) and (hr4 = 0) and (hr5 = 0) and (hr6 = 0){
         break
      }
   }
}
DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
ExitApp
Thanks @gsf81! This is the one I got working for me.

@malcev Amazing work man!! I had some makeshift solutions to this and you've blown them all out of the water!
One thing though is despite both codes working, I still get the "Device not found" message box. Does anyone have any idea why that'd be the case?

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Connect to bluetooth headset via shortcut

Post by BoBo » 10 Jul 2022, 12:14

The above code from @malcev put into a function. HTH :shifty:
BTPaired()
@ahkonfused
@malcev Amazing work man!! I had some makeshift solutions to this and you've blown them all out of the water!
One thing though is despite both codes working, I still get the "Device not found" message box. Does anyone have any idea why that'd be the case?
Same issue. While the respective device is listed in the output of the "status" script (and of course visible within the systems configuration pane) the other script isn't pairing the device and is responding with "Device not found" :think:

PS. I've once found :arrow: this (that is probably the successor of :arrow: that) - and its documentation "will be updated soon" (2017) :lol:

b0bi
Posts: 7
Joined: 07 Apr 2016, 13:11

Re: Connect to bluetooth headset via shortcut

Post by b0bi » 16 Sep 2022, 06:08

This sample works on my Windows 10 as of 16.09.22 in order to connect to an already paired device.

Code: Select all

deviceName := "Galaxy Buds2 Pro"

DllCall("LoadLibrary", "str", "Bthprops.cpl", "ptr")
VarSetCapacity(BLUETOOTH_DEVICE_SEARCH_PARAMS, 24+A_PtrSize*2, 0)
NumPut(24+A_PtrSize*2, BLUETOOTH_DEVICE_SEARCH_PARAMS, 0, "uint")
NumPut(1, BLUETOOTH_DEVICE_SEARCH_PARAMS, 4, "uint")   ; fReturnAuthenticated
VarSetCapacity(BLUETOOTH_DEVICE_INFO, 560, 0)
NumPut(560, BLUETOOTH_DEVICE_INFO, 0, "uint")
loop
{
   If (A_Index = 1)
   {
      foundedDevice := DllCall("Bthprops.cpl\BluetoothFindFirstDevice", "ptr", &BLUETOOTH_DEVICE_SEARCH_PARAMS, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr")
      if !foundedDevice
      {
         msgbox no bluetooth devices
         return
      }
   }
   else
   {
      if !DllCall("Bthprops.cpl\BluetoothFindNextDevice", "ptr", foundedDevice, "ptr", &BLUETOOTH_DEVICE_INFO)
      {
         msgbox no found maybw
         break
      }
   }
   dev := StrGet(&BLUETOOTH_DEVICE_INFO+64)
;   msgbox dev
   if (StrGet(&BLUETOOTH_DEVICE_INFO+64) = deviceName)
   {
      VarSetCapacity(Handsfree, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000111e-0000-1000-8000-00805f9b34fb}", "ptr", &Handsfree)   ; https://www.bluetooth.com/specifications/assigned-numbers/service-discovery/
      VarSetCapacity(AudioSink, 16)
      DllCall("ole32\CLSIDFromString", "wstr", "{0000110b-0000-1000-8000-00805f9b34fb}", "ptr", &AudioSink)
   
      hr1 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 0)   ; voice
      hr2 := DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 0)   ; music
     
      if (hr1 = 0) and (hr2 = 0)
         break
      else
      {
         DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 1)   ; voice
         DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 1)   ; music
     
         ;DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &Handsfree, "int", 0)   ; voice
         ;DllCall("Bthprops.cpl\BluetoothSetServiceState", "ptr", 0, "ptr", &BLUETOOTH_DEVICE_INFO, "ptr", &AudioSink, "int", 0)   ; music
      
         break
      }
   }
}

DllCall("Bthprops.cpl\BluetoothFindDeviceClose", "ptr", foundedDevice)
msgbox done
ExitApp

chaoscreater
Posts: 59
Joined: 12 Sep 2019, 21:15

Re: Connect to bluetooth headset via shortcut

Post by chaoscreater » 29 Jun 2023, 22:40

How to get the current connection status of the device? I don't see a BluetoothGetServiceState option

Post Reply

Return to “Ask for Help (v1)”