23

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
  • 367
  • 1
  • 2
  • 10
  • 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

2 Answers2

8

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
  • 551
  • 1
  • 4
  • 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
2

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
  • 300
  • 1
  • 11