'How to connect to a paired audio Bluetooth device using Windows UWP API?

I want to create a console alternative to buit-in into Windows "Bluetooth and Other devices" dialog for controlling Bluetooth devices. Using Windows.Devices.Enumeration API I can pair and unpair devices. However, audio Bluetooth devices also have a connection feature (see the screenshot bellow). I would like to know how to do the same thing as this UI dialog does, when I press a "connect" button, in my application. enter image description here

Update

Currently there is no API that could be used to solve my problem. There is a very good discussion on Github about this topic, it also contains description of available solutions and why they don't work.



Solution 1:[1]

So far I have fond a workaround which allows to make Windows connect to audio Bluetooth devices. It is based on an answer from Bernard Moeskops. However, to make it work the following needs to be taken into account:

  • Bluetooth audio devices can be registered as several PnP devices, and you have to toggle all of them
  • It seems that waiting for 10 seconds between enabling and disabling is not necessary.
  • "Disable-PnpDevice" and "Enable-PnpDevice" commands require admin rights.

I have created a PowerShell script which worked for me (you need to change the $headphonesName variable to the name of your audio device):

# "Disable-PnpDevice" and "Enable-PnpDevice" commands require admin rights
#Requires -RunAsAdministrator

# Substitute it with the name of your audio device.
# The audio device you are trying to connect to should be paired.
$headphonesName = "WH-1000XM3"

$bluetoothDevices = Get-PnpDevice -class Bluetooth

# My headphones are recognized as 3 devices:
# * WH-1000XM3
# * WH-1000XM3 Avrcp Transport
# * WH-1000XM3 Avrcp Transport
# It is not enough to toggle only 1 of them. We need to toggle all of them.
$headphonePnpDevices = $bluetoothDevices | Where-Object { $_.Name.StartsWith("$headphonesName") }

if(!$headphonePnpDevices) {
    Write-Host "Coudn't find any devices related to the '$headphonesName'"
    Write-Host "Whole list of available devices is:"
    $bluetoothDevices
    return
}

Write-Host "The following PnP devices related to the '$headphonesName' headphones found:"
ForEach($d in $headphonePnpDevices) { $d }

Write-Host "`nDisable all these devices"
ForEach($d in $headphonePnpDevices) { Disable-PnpDevice -InstanceId $d.InstanceId -Confirm:$false }

Write-Host "Enable all these devices"
ForEach($d in $headphonePnpDevices) { Enable-PnpDevice -InstanceId $d.InstanceId -Confirm:$false }

Write-Host "The headphones should be connected now."

# After toggling, Windows "Bluetooth devices" dialog shows the state of the headphones as "Connected" but not as "Connected voice, music"
# However, after some time (around 5 seconds), the state changes to "Connected voice, music".
Write-Host "It may take around 10 seconds until the Windows starts showing audio devices related to these headphones."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1