Windows – How to script a bluetooth device to connect/disconnect

bluetoothconnectionwindows 7

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.

Best Answer

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.

Related Question