Check for bluetooth device if connected

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
k3nobi
Posts: 16
Joined: 06 Jun 2021, 01:51

Check for bluetooth device if connected

06 Jun 2021, 02:00

Hi fellow people, its my first time here, so I don't really know how things work around here, apologize in advance if I did anything wrong

So I've got a script where when I click on F1 it would run a .bat file where it disconnects my earphones, so I can use my speakers. I then have F2 as a reconnect button.
My question is it possible for me to use the if else statement so that I only need to use one hotkeys for disconnect and reconnect, all i need now is just for AHK to check if my bluetooth earphones are connected or not then decide whether to disconnect it or connect
User avatar
boiler
Posts: 17385
Joined: 21 Dec 2014, 02:44

Re: Check for bluetooth device if connected

06 Jun 2021, 06:13

Yes, you can use if/else. Below is a skeleton of how you would use it:

Code: Select all

F1::
	if (expression that is true if connected)
		Command to run the BAT file to disconnect
	else
		Command to run the BAT file to connect
return
k3nobi
Posts: 16
Joined: 06 Jun 2021, 01:51

Re: Check for bluetooth device if connected

06 Jun 2021, 06:45

boiler wrote:
06 Jun 2021, 06:13
Yes, you can use if/else. Below is a skeleton of how you would use it:

Code: Select all

F1::
	if (expression that is true if connected)
		Command to run the BAT file to disconnect
	else
		Command to run the BAT file to connect
return
Thanks but do you know if there is any way for AHK or other software to check if a certain bluetooth device is connected or not?
k3nobi
Posts: 16
Joined: 06 Jun 2021, 01:51

Re: Check for bluetooth device if connected

10 Jun 2021, 00:09

boiler wrote:
06 Jun 2021, 08:00
This thread may help.
Hey thanks, for linking me this, I'll look into it
k3nobi
Posts: 16
Joined: 06 Jun 2021, 01:51

Re: Check for bluetooth device if connected

10 Jun 2021, 00:14

malcev wrote:
06 Jun 2021, 12:12
You can do it with this api
https://docs.microsoft.com/en-us/windows/win32/api/bluetoothapis/
Some time ago I have written this code
https://www.autohotkey.com/boards/viewtopic.php?p=366357#p366357
You just need to modify it.
It seems that it works, but not at the same time, here's what happened:
When I click on my hotkeys, I hear my bluetooth earphones making the sound where its ready to connect with a device
I then hear it makes the sound of when it succesfully connect with a device
After that it work, my earphones are disconnected and my speaker took over..... for a few seconds, it then continues to use my earphone as the sound device

so yeah, it works for a few seconds, I appreciate the help tho, this is as far I've gotten to this with it being the most efficient and fastest disconnect out of all the method I've tried
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Check for bluetooth device if connected

10 Jun 2021, 05:35

As I remember this script should always only connect the device (jbl clip 3).
Therefore I wrote that You need to modify it.
For example to get info about connection state of device You can with 2 methods.
1) Search only connected devices
https://docs.microsoft.com/en-us/windows/win32/api/bluetoothapis/ns-bluetoothapis-bluetooth_device_search_params
2) Search all devices and check for connection state.
https://docs.microsoft.com/en-us/windows/win32/api/bluetoothapis/ns-bluetoothapis-bluetooth_device_info_struct
Chryron
Posts: 1
Joined: 23 Jan 2022, 16:55

Re: Check for bluetooth device if connected

12 Mar 2022, 06:10

malcev wrote:
10 Jun 2021, 05:35
As I remember this script should always only connect the device (jbl clip 3).
Therefore I wrote that You need to modify it.
For example to get info about connection state of device You can with 2 methods.
1) Search only connected devices
https://docs.microsoft.com/en-us/windows/win32/api/bluetoothapis/ns-bluetoothapis-bluetooth_device_search_params
2) Search all devices and check for connection state.
https://docs.microsoft.com/en-us/windows/win32/api/bluetoothapis/ns-bluetoothapis-bluetooth_device_info_struct
Sorry about reviving this old thread again, but can you share how you parsed the _BLUETOOTH_DEVICE_INFO struct. I tried using Structor for it, but it didn't work since it uses the _BLUETOOTH_ADDRESS struct too. I just need the offset for fConnected bool but the others would be useful too.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Check for bluetooth device if connected

12 Mar 2022, 08:16

Code: Select all

typedef struct _BLUETOOTH_DEVICE_INFO {
0  DWORD             dwSize;
8  BLUETOOTH_ADDRESS Address;
16  ULONG             ulClassofDevice;
20  BOOL              fConnected;
24  BOOL              fRemembered;
28  BOOL              fAuthenticated;
32  SYSTEMTIME        stLastSeen;
48  SYSTEMTIME        stLastUsed;
64  WCHAR             szName[BLUETOOTH_MAX_NAME_SIZE];
} BLUETOOTH_DEVICE_INFO_STRUCT;
chaoscreater
Posts: 60
Joined: 12 Sep 2019, 21:15

Re: Check for bluetooth device if connected

29 Jun 2023, 22:39

How to get the current connection status of the device? I don't see a BluetoothGetServiceState option
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Check for bluetooth device if connected

30 Jun 2023, 06:11

There are no such function BluetoothGetServiceState.
You need to search only for connected devices:
https://www.autohotkey.com/boards/viewtopic.php?p=404352#p404352
JayTe
Posts: 1
Joined: 04 Mar 2024, 18:33

Re: Check for bluetooth device if connected

04 Mar 2024, 18:46

I was searching for a similar solution. I'm using the following PowerShell skript to check, whether a bluetooth device is connected or not.
For that I use btdiscovery integrated in the bluetooth-command-line-tools suite. [URL removed by moderator.]
With the command "btdiscovery -d"%c%" -i1 -bXX:XX:XX:XX:XX:XX" in powershell/cmd it just replys "Yes" or "No".
Save the code below as .ps1 and specify the mac adress of the device to look out for and put desired actions in the if statement.

Code: Select all

# Define Bluetooth address
$bluetoothAddress = "XX:XX:XX:XX:XX:XX"

# Execute the btdiscovery command and redirect the output to a variable
$commandOutput = btdiscovery -d "%c%" -i 1 -b $bluetoothAddress

# Check the output and display appropriate message
if ($commandOutput -eq "Yes") {
    Write-Host "Bluetooth device is connected."
} elseif ($commandOutput -eq "No") {
    Write-Host "Bluetooth device is not connected."
}
pause

[Mod edit: Replaced quote tags with PS1-type code box tags.]

[Mod edit: URL to outside website removed. It's preferred that new members don't link to other sites in their first posts, especially when their offered solution is not AHK based.]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: JKJadan, just me and 267 guests