如何通过Windows PowerShell连接已配对的蓝牙音频设备(如AirPods)
问题根因
你之前使用的Enable-PnpDevice命令作用是启用被系统禁用的硬件设备,并非触发已配对蓝牙设备的连接流程,所以操作无效果。你操作音频端点设备时出现的0x80041001报错,是因为蓝牙连接未建立时,对应的音频端点设备处于不可操作的离线状态,系统不允许直接启用该设备。
可行解决方案
方案1:使用第三方命令行工具实现(配置简单,稳定性高)
- 下载BluetoothCommandLineTools工具,解压后将工具所在文件夹路径添加到系统环境变量PATH中
- 执行命令
btcom -l列出所有已配对蓝牙设备,记录你的AirPods Pro对应的MAC地址 - 蓝牙连接命令(替换为你自己的设备MAC地址):
btcom -c "AA:BB:CC:DD:EE:FF" -s {0000110B-0000-1000-8000-00805F9B34FB}
- 连接成功后切换默认音频输出设备可以用nircmd工具,命令如下:
nircmd setdefaultsounddevice "Headphones (AirPods Pro Stereo)" 1
- 将上述两行命令存为.bat批处理文件,双击即可一键完成连接+切换音频输出的全流程,也可以放到开机启动文件夹实现开机自动连接。
方案2:纯PowerShell调用系统API实现(无需额外下载工具)
直接调用Windows原生WinRT蓝牙API发起连接,脚本如下(替换脚本中的MAC地址为你自己的AirPods MAC地址):
Add-Type -AssemblyName System.Runtime.WindowsRuntime $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0] Function Await($WinRtTask, $ResultType) { $asTask = $asTaskGeneric.MakeGenericMethod($ResultType) $netTask = $asTask.Invoke($null, @($WinRtTask)) $netTask.Wait() $netTask.Result } Function AwaitAction($WinRtAction) { $asTask = [System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and !$_.IsGenericMethod } $netTask = $asTask.Invoke($null, @($WinRtAction)) $netTask.Wait() } # 替换为你的AirPods MAC地址 $macAddress = "AA:BB:CC:DD:EE:FF" $macBytes = [byte[]]($macAddress.Split(':') | ForEach-Object { [Convert]::ToByte($_, 16) }) [Array]::Reverse($macBytes) $btAddress = [System.BitConverter]::ToUInt64($macBytes, 0) $btDevice = Await ([Windows.Devices.Bluetooth.BluetoothDevice::FromBluetoothAddressAsync($btAddress)]) ([Windows.Devices.Bluetooth.BluetoothDevice]) Await ($btDevice.GetRfcommServicesAsync()) ([Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceServicesResult]) # 切换默认音频输出 $AudioDeviceType = New-Object -ComObject CoreAudioApi.MMDeviceEnumerator $defaultDevice = $AudioDeviceType.EnumerateAudioEndPoints(0,1) | Where-Object {$_.FriendlyName -like "*AirPods Pro Stereo*"} $defaultDevice.SetAsDefault()
脚本保存为.ps1格式,运行时需以管理员身份执行PowerShell。
内容的提问来源于stack exchange,提问作者Peter
相关产品推荐
相关产品推荐

