我想创建一个控制台替代方案来内置到 Windows“蓝牙和其他设备”对话框中来控制蓝牙设备。使用 Windows.Devices.Enumeration API 我可以配对和取消配对设备。但是,音频蓝牙设备也具有连接功能(请参见下面的屏幕截图)。我想知道当我在应用程序中按下“连接”按钮时,如何执行与此 UI 对话框相同的操作。
目前没有API可以用来解决我的问题。 Github 上有一个关于这个主题的非常好的 讨论 , 它还包含可用解决方案的描述以及它们为什么不起作用。
answer。然而,要使其发挥作用,需要考虑以下因素:
$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."