[英]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.我想创建一个控制台替代方案来嵌入 Windows“蓝牙和其他设备”对话框以控制蓝牙设备。 Using Windows.Devices.Enumeration API I can pair and unpair devices.使用Windows.Devices.Enumeration API 我可以配对和取消配对设备。 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.当我在我的应用程序中按下“连接”按钮时,我想知道如何做与此 UI 对话框相同的事情。
Currently there is no API that could be used to solve my problem.目前没有 API 可以用来解决我的问题。 There is a very good discussion on Github about this topic, it also contains description of available solutions and why they don't work.在 Github 上有一个关于这个主题的很好的讨论,它还包含可用解决方案的描述以及它们为什么不起作用。
So far I have fond a workaround which allows to make Windows connect to audio Bluetooth devices.到目前为止,我喜欢一种解决方法,它允许使 Windows 连接到音频蓝牙设备。 It is based on an answer from Bernard Moeskops.它基于 Bernard Moeskops 的回答。 However, to make it work the following needs to be taken into account:但是,要使其发挥作用,需要考虑以下因素:
I have created a PowerShell script which worked for me (you need to change the $headphonesName
variable to the name of your audio device):我创建了一个适用于我的 PowerShell 脚本(您需要将$headphonesName
变量更改为音频设备的名称):
# "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."
I have the same situation, any updates on it?我也有同样的情况,有更新吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.