简体   繁体   English

如何使用 Windows UWP API 连接到配对的音频蓝牙设备?

[英]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 对话框相同的事情。 在此处输入图像描述

Update更新

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:但是,要使其发挥作用,需要考虑以下因素:

  • Bluetooth audio devices can be registered as several PnP devices, and you have to toggle all of them蓝牙音频设备可以注册为多个 PnP 设备,您必须切换所有这些设备
  • It seems that waiting for 10 seconds between enabling and disabling is not necessary.似乎没有必要在启用和禁用之间等待 10 秒。
  • "Disable-PnpDevice" and "Enable-PnpDevice" commands require admin rights. “Disable-PnpDevice”和“Enable-PnpDevice”命令需要管理员权限。

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.

相关问题 如何在Windows 10 UWP中丢失连接后以编程方式连接到配对的蓝牙设备 - How to programmatically connect to paired Bluetooth device once connection is lost in Windows 10 UWP 如何在 UWP 中将蓝牙设备映射到音频设备? - How to map bluetooth device to audio device in UWP? 如何连接到 Windows 上的蓝牙设备? - How to connect to bluetooth device on Windows? 是否有 Windows 事件来查明蓝牙设备是否正在配对 - Is there a Windows event to find out if a bluetooth device is getting paired 在 Windows 11 上将已配对的蓝牙耳机与 .NET Core 连接 - Connect an already paired bluetooth headphone with .NET Core on Windows 11 使用32Feet查找配对的蓝牙设备(如果在范围内) - Finding paired Bluetooth device if its in range using 32Feet 如何在Windows Store应用程序上连接到第三方蓝牙设备? - How To Connect To A 3rd Party Bluetooth Device On A Windows Store App? 如何识别当前断开连接的配对的蓝牙设备? - How do I identify a paired Bluetooth device that's currently disconnected? 如何获取已配对(在移动设置中已连接)蓝牙设备的名称? - How to get the name of paired(Connected in mobile settings) bluetooth device? Windows UWP在发现后连接到BLE设备 - Windows UWP connect to BLE device after discovery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM