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

问题描述 投票:0回答:1

我想创建一个控制台替代方案来内置到 Windows“蓝牙和其他设备”对话框中来控制蓝牙设备。使用 Windows.Devices.Enumeration API 我可以配对和取消配对设备。但是,音频蓝牙设备也具有连接功能(请参见下面的屏幕截图)。我想知道当我在应用程序中按下“连接”按钮时,如何执行与此 UI 对话框相同的操作。

更新

目前没有API可以用来解决我的问题。 Github 上有一个关于这个主题的非常好的 讨论 , 它还包含可用解决方案的描述以及它们为什么不起作用。

c# windows uwp bluetooth
1个回答
5
投票
到目前为止,我已经找到了一种解决方法,允许 Windows 连接到音频蓝牙设备。 它基于 Bernard Moeskops 的

answer。然而,要使其发挥作用,需要考虑以下因素:

    蓝牙音频设备可以注册为多个 PnP 设备,您必须切换所有设备
  • 看来启用和禁用之间等待10秒是没有必要的。
  • “Disable-PnpDevice”和“Enable-PnpDevice”命令需要管理员权限。
我创建了一个对我有用的 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."
    
© www.soinside.com 2019 - 2024. All rights reserved.