28

I have bluetooth headphones which I use to both listen to music from my mobile phone and to listen to videos on my laptop. They cannot be connected to both devices at once (note here), so I have to disconnect from one device before connecting to the next device.

To do this on Windows 7, I have to open Devices and Printers, open my bluetooth headphones, and click on the Connect/Disconnect button. I'd love to be able to run a script that triggers that Connect/Disconnect button. That way I could have a shortcut on the desktop or add a keyboard shortcut.

I've streamlined it somewhat by creating a shortcut to the bluetooth heaphones window (in Devices and Printers, right click on your bluetooth device and choose Create Shortcut). But it would be good to streamline further.

David Cook
  • 447
  • 1
  • 4
  • 11
  • 2
    Yes. My computer does not have Bluetooth capabilities, but you can use [AutoHotkey](http://autohotkey.com/) to automate the process. See my answer here for ideas: http://superuser.com/questions/403620/enable-disable-a-network-adapter-with-a-keyboard-shortcut/403834#403834 – iglvzx Apr 04 '12 at 00:37
  • Thanks, good idea. I 'll try it out when I have a spare minute. – David Cook Apr 16 '12 at 01:22
  • @DavidCook Let us know if you get it sorted out, I'd love to grab a copy of that script. – BigElittles Jun 30 '16 at 16:45
  • Sorry @Big, I don't think I ever sorted it out. Glad you found a solution. – David Cook Jul 22 '16 at 03:46
  • I've been trying to find a way to do this programmatically for about 6 months, but none of the bt tools / methods in windows seem to work properly. Using @iglvzx post + autoitv3 works great. While it's not ideal as it's not backgrounded, it's way better than running this manually and since it compiles to an exe I should be able to run this at login. Kudos! – Pavman Jun 29 '23 at 01:19

4 Answers4

9

As it turns out, once all services in use by a device get disabled, device gets released and disconnected by Windows automatically. In my example case below for WH-1000XM3 these are voice and music, and most headphones will work the same way. This will of course depend on device in use.

You will need Bluetooth Command Line Tools.

Voice is actually the hands free service (HFP) and music is just an audio sink (A2DP). Service identifiers will be necessary and they can be discovered through the usage of btdiscovery command from the package above, or via the list of Bluetooth services. HFP voice is 111e, A2DP music is 110b.

Per btcom command line help:

Usage:

btcom {-c|-r} {-bBluetoothAddress | -nFriendlyName} [-s{sp|dun|GUID|UUID}]

 -c  Create association between COM port and a remote service (Enable non-COM service).
 -r  Remove association between COM port and a remote service (Disable non-COM service).
 -s  Remote service to use (Default is Serial Port Service)
 -b  Bluetooth address of remote device in (XX:XX:XX:XX:XX:XX) format. 
 -n  Friendly name of remote device.

To disconnect the device, issue the following (only works when run as administrator in my case, using Windows 10 1809 (17763.437)):

"C:\Program Files (x86)\Bluetooth Command Line Tools\bin\btcom" -n "WH-1000XM3" -r -s111e
"C:\Program Files (x86)\Bluetooth Command Line Tools\bin\btcom" -n "WH-1000XM3" -r -s110b

To connect again, issue the same with -c instead of -r. This works for other devices, not just headphones, as long as all services/profiles connected to by Windows get disabled/enabled.

Note: using -n <friendly name> is much slower than using -b <address> due to performing Bluetooth discovery.

MarcinJ
  • 721
  • 1
  • 7
  • 10
  • Hey! After I use `-r` to disconnect, connecting manually from settings doesn't work. The "Connect" button doesn't show up and connecting through the connect action panel (Win+K) says "Couldn't connect". Using `-c` seems to work. Do you know why? Do you know how I can keep the usual functionality when using this? – Faboor Oct 06 '20 at 19:35
  • @Faboor It's because services get disabled on the device, and if there are no services to connect to, Windows won't let you connect. After issuing `-r`, you will note that in Control Panel > Hardware and Sound > Devices and Printers, if you select your BT device and check its Properties > Services, checkboxes for these services are unchecked. `-c` basically checks them back again, enabling these services and thus allowing connections. – MarcinJ Oct 07 '20 at 20:47
  • Thanks, makes sense. Is there any other way of disconnecting bt rather than disabling the services? – Faboor Oct 08 '20 at 11:42
  • 1
    That's the thing, I haven't found any, not on Windows. Unless you use something like AHK or AutoIt to simulate mouse click on that Disconnect button. – MarcinJ Oct 09 '20 at 19:55
  • 1
    If you get param -87 issues, see this related question: https://superuser.com/questions/1585945/issue-with-script-to-reconnect-bluetooth-device-bluetooth-command-line-tools/1619791#1619791 – agrath Jan 22 '21 at 08:02
  • Also you need both s111e and s110b for some devices, at least I do for my Plantronics Backbeat Fit headphones, with only s111e the audio quality was terrible – agrath Jan 22 '21 at 08:03
  • Direct link to a list of all Bluetooth services and their UUID: https://btprodspecificationrefs.blob.core.windows.net/assigned-numbers/Assigned%20Number%20Types/Service%20Discovery.pdf – Vinayak Jan 20 '22 at 00:57
  • @agrath `0x110B` is `AudioSink` (A2DP audio sink). You should try that. `0x111e` is for `Hands-Free Profile (HFP)` so it will sound terrible. – Vinayak Jan 20 '22 at 01:00
4

I found an a similar question, here is the resolution. https://stackoverflow.com/questions/29724513/automatically-adding-a-bluetooth-device-in-windows-8

@ECHO OFF
if exist toggle.txt goto :2
btpair -u
C:\NIRCMD\NIRCMDC setdefaultsounddevice "CompSpeak" 1
C:\NIRCMD\NIRCMDC setdefaultsounddevice "CompSpeak" 2

echo toggle>>toggle.txt

exit
:2
btpair -n"[Samsung] AirTrack" -p0000
C:\NIRCMD\NIRCMDC setdefaultsounddevice "Headphones" 1
C:\NIRCMD\NIRCMDC setdefaultsounddevice "Headphones" 2

del toggle.txt /q

exit

OP said that he was getting a pop up message box, but he is also on windows 8 so that might be unique to his setup.

BigElittles
  • 320
  • 2
  • 11
  • It has some dependencies to `nircmd` and `btpair` though... – ixe013 Sep 23 '21 at 13:48
  • The popup is if they ran `nircmd.exe` instead of `nircmdc.exe`. Yeah, it's weird. Esp. the read the MS Help file message. I was unable to get this to work and didn't see a connect/disconnect capability in the args list (which looks like it was last updated sometime in 2006). – Pavman Jun 29 '23 at 01:40
4

Autohotkey can do it on Win10, by calling DLL.

  1. install authotkey https://www.autohotkey.com/
  2. Download this script https://github.com/a5sd45a4sd/bluco
  3. At the the top of the script, edit your device name
  4. Run the script
n0tis
  • 133
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 16 '22 at 11:52
  • Welcome to SuperUser! Please do not share link only answers as the linked site might change or be removed entirely, rendering your answer useless. Instead quote the most important parts and provide the link as reference only – DarkDiamond Sep 16 '22 at 12:31
  • The script in the link you gave is not available anymore. Can you provide the script? – Dominique Unruh Oct 12 '22 at 08:33
  • cf reddit comments – n0tis Oct 12 '22 at 09:16
  • Actually, this script works much faster and reliable than `btcom`! Here is the link on github repo (it's not mine) from this reddit post: [blueco-script](https://github.com/a5sd45a4sd/bluco) – Pavel Sapehin Feb 17 '23 at 08:06
0

While this post is a bit stale, the code below in AutoITv3 is what worked for me. It's not ideal, but it sure beats doing it by hand.

I tried the other answers in various posts (bttools, nircmd, pnputil, devcon, wmi via powershell) across stackexchange sites, but none of them seemed to adequately address the already paired but not connected / disabled sound device issue, which is way more important than pairing an already paired device.

; Run the sound cpanel
Run("control.exe mmsys.cpl")
WinWaitActive("Sound","",5)
; might need to Send("{DOWN}") if bt device is not the first entry in the list
; right click on the first entry
ControlClick("Sound","",1000,"right",1,69,27)
Send("{DOWN}")
Send("{DOWN}")
Send("{ENTER}")
Sleep(2000)
WinClose("Sound")
WinWaitClose("Sound","",2000)
Pavman
  • 61
  • 3