Scripting connecting/disconnecting a paired Bluetooth device

22,985

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.

Solution 4

Maybe Get-PnPDevice, Disable-PnPDevice and Enable-PnPDevice will do the trick for you. I havent been able to test it out though.

Example:

$DeviceName = "YourDevice"
$BTDevice =  Get-PnpDevice | Where-Object {$_.FriendlyName -eq $DeviceName -and $_.class -eq "Bluetooth"} 

Disable-PnpDevice -InstanceId $BTDevice.DeviceID -Confirm:$false

Enable-PnpDevice -InstanceId $BTDevice.DeviceID -Confirm:$false

Change the $DeviceName variable to the name of your BT device.

Get-PnPDevice Enable-PnPDevice Disable-PnPDevice

Share:
22,985

Related videos on Youtube

MarcinJ
Author by

MarcinJ

Updated on September 18, 2022

Comments

  • MarcinJ
    MarcinJ almost 2 years

    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.

    • user232548
      user232548 about 4 years
      LOL, my exact problem. The sony headset handling multi device usecase too poorly and windows having cluncy interface for connect/disconnect.
  • MarcinJ
    MarcinJ about 5 years
    What I meant by "baiscally press the connect/disconnect button" is perform the same thing Windows performs when that button is pressed, except from whatever code, e.g. PowerShell, bat, C#, C++, whatever. Turning off the whole BT radio won't work, there could be other devices connected, I just want to connect to or disconnect from a specific BT device (headphones).
  • MarcinJ
    MarcinJ about 5 years
    It doesn't, I've stumbled upon Disable-PnPDevice myself too. Depending on what gets disabled (as there are multiple devices, depending on BT profiles), either nothing happens, bt stack crashes or audio output gets disabled but the device stays connected.
  • MarcinJ
    MarcinJ about 5 years
  • MarcinJ
    MarcinJ about 5 years
  • postanote
    postanote about 5 years
    coolness. Glad you worked it out. I was still digging at this myself. I don't much mess with device in this manner. So, interesting little digging event.
  • InTwoMinds
    InTwoMinds about 5 years
    I've scripted your solution in form of clickable/bindable VBS scripts, so one can bind it to e.g. some media button on keyboard: github.com/stanleyguevara/win10-bluetooth-headphones
  • Will Ediger
    Will Ediger over 4 years
    My edit to the answer is pending, but just in case, here is an archive.org mirror to the broken link to the "Bluetooth Command Line Tools" page: web.archive.org/web/20190603121131/http://…
  • MarcinJ
    MarcinJ over 4 years
    @WillEdiger site still seems to be working fine, at least for me, not need for an archive.
  • Will Ediger
    Will Ediger over 4 years
    @MarcinJ - It's working again for me too! It must have been fixed quickly. Someone else noticed it 4 days ago: github.com/stanleyguevara/win10-bluetooth-headphones/issues/‌​… Strange!
  • shieldgenerator7
    shieldgenerator7 about 4 years
    Yeah the link to the Bluetooth command line tools is still broken
  • MarcinJ
    MarcinJ about 4 years
    It works for me, but if it doesn't then it seems there's a solution in the github issue @WillEdiger linked.
  • xdhmoore
    xdhmoore almost 4 years
    Too bad that CLI tool isn't on github. The current site's ad/popup behavior is past my acceptable threshold...
  • EJSawyer
    EJSawyer over 3 years
    The link worked for me, 2020-12-29. I am able to connect and disconnect without invoking admin mode, but it works much faster in admin mode. I'm running Win10 Home, v2004 build 19041.685.
  • agrath
    agrath over 3 years
    Confirmed working with my PLT_BBFIT headphones which I switch between computer (zwift) and phone (running). I needed both s111e and s110b to make the audio not sound like it was in a tin can (only s111e at first sounded horrible)
  • Fahri Güreşçi
    Fahri Güreşçi over 3 years
    My device not connect automatically. bat script with: btpair -u and btpair -n"deviceName" -p0000 practical method. Thank you @MarcinJ
  • Mureinik
    Mureinik over 2 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
  • PolarBear
    PolarBear over 2 years
    I have managed to make this answer work for me, please check my answer
  • Admin
    Admin about 2 years
    For Win11, this and Win-K only bring up the "Cast Display" side-menu, unfortunately, no longer does this include bluetooth devices. Instead explorer.exe ms-settings:bluetooth brings up the correct bluetooth-connection window.