Scripting connecting/disconnecting a paired Bluetooth device

Is there a way, through powershell, or any other tools to connect and disconnect a paired Bluetooth device? Basically press the Connect/Disconnect button in Bluetooth & other devices, except from command line of whatever sort (Powershell, bat using some command line tools, code in C#, C++, etc.):

Bluetooth & other devices

I found this answer but it involves unpairing and pairing again, which won't work because my headphones need to be in pairing mode to accept new pairing. I'd rather not simulate keypresses and mouse clicks via AutoIt or some other software like that.


Solution 1:

I wrote C++ code to do it using Win32 Bluetooth API's BluetoothSetServiceState, but it's actually enough to use Bluetooth Command Line Tools.

As it turns out, once all services in use by a device get disabled, device gets released and disconnected by Windows automatically. In my case these are voice and music, as per the screenshot, and most headphones will work the same way.
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.

Solution 2:

In case the solution from @MarcinJ with Bluetooth Command Line Tools is too slow for you, especially in case you want to pair the device meanwhile an incoming call try creating a windows shortcut:
(Right click > New > Shortcut)

%windir%\explorer.exe ms-settings-connectabledevices:devicediscovery

It will pop up a display and audio panel right on the screen, which is one click away from connect.
And nice BT icon for the shortcut can be found for example on C:\Windows\System32\fsquirt.exe.

Solution 3:

win 10 shortcuts https://www.windowscentral.com/best-windows-10-keyboard-shortcuts

win-key + K opens the sidebar menu and instantly searches for bluetooth devices. then click on your already paired device and connect.