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
Check for bluetooth device if connected
Re: Check for bluetooth device if connected
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
Re: Check for bluetooth device if connected
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?boiler wrote: ↑06 Jun 2021, 06:13Yes, 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
Re: Check for bluetooth device if connected
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.
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.
Re: Check for bluetooth device if connected
Hey thanks, for linking me this, I'll look into it
Re: Check for bluetooth device if connected
It seems that it works, but not at the same time, here's what happened:malcev wrote: ↑06 Jun 2021, 12:12You 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.
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
Re: Check for bluetooth device if connected
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
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
Re: Check for bluetooth device if connected
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 wrote: ↑10 Jun 2021, 05:35As 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
Re: Check for bluetooth device if connected
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;
-
- Posts: 60
- Joined: 12 Sep 2019, 21:15
Re: Check for bluetooth device if connected
How to get the current connection status of the device? I don't see a BluetoothGetServiceState option
Re: Check for bluetooth device if connected
There are no such function BluetoothGetServiceState.
You need to search only for connected devices:
https://www.autohotkey.com/boards/viewtopic.php?p=404352#p404352
You need to search only for connected devices:
https://www.autohotkey.com/boards/viewtopic.php?p=404352#p404352
Re: Check for bluetooth device if connected
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.
[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.]
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.]