如何在Windows 11上用.NET Core连接已配对的蓝牙耳机?
解决Windows 11下触发蓝牙耳机音乐/聊天连接的问题
你需要通过蓝牙设备API建立基础连接,再针对性连接音频服务(A2DP用于音乐,HFP用于聊天),以下是具体实现方案:
核心实现步骤
- 从
DeviceInformation获取对应蓝牙耳机的BluetoothDevice实例 - 调用设备连接API建立基础蓝牙连接
- 枚举并连接音频相关的RFCOMM服务
- (可选)将耳机设置为默认音频设备
完整代码示例
替换你代码中的注释部分,添加以下逻辑:
using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.Rfcomm; using Windows.Devices.Enumeration; // 如果需要设置默认音频设备,需引用CoreAudio相关库或使用P/Invoke // ... 你的现有代码 ... if ("<myHeadphonesBluetoothId>" == di.Id) { // 获取蓝牙设备实例 BluetoothDevice bluetoothDevice = await BluetoothDevice.FromIdAsync(di.Id); if (bluetoothDevice == null) { Debug.WriteLine("无法获取目标蓝牙设备实例"); continue; } // 建立基础蓝牙连接 BluetoothError connectResult = await bluetoothDevice.ConnectAsync(); if (connectResult != BluetoothError.Success) { Debug.WriteLine($"基础连接失败: {connectResult}"); continue; } // 枚举并连接音频服务(A2DP音乐 + HFP聊天) var rfcommServicesResult = await bluetoothDevice.GetRfcommServicesAsync(BluetoothCacheMode.Uncached); if (rfcommServicesResult.Status != BluetoothError.Success) { Debug.WriteLine($"获取RFCOMM服务失败: {rfcommServicesResult.Status}"); continue; } foreach (RfcommDeviceService service in rfcommServicesResult.Services) { // 匹配A2DP音频接收器和HFP音频网关服务 if (service.ServiceUuid == RfcommServiceId.AudioSink || service.ServiceUuid == RfcommServiceId.HandsfreeAudioGateway) { GattCommunicationStatus openStatus = await service.OpenAsync(); if (openStatus == GattCommunicationStatus.Success) { Debug.WriteLine($"成功连接音频服务: {service.ServiceUuid}"); } else { Debug.WriteLine($"连接音频服务失败: {service.ServiceUuid}, 状态: {openStatus}"); } } } // 可选:设置为默认音频播放/录制设备 await SetDefaultAudioDevice(bluetoothDevice.Name, DeviceClass.AudioRender); await SetDefaultAudioDevice(bluetoothDevice.Name, DeviceClass.AudioCapture); } // 辅助方法:设置默认音频设备(需引用CoreAudioApi) private async Task SetDefaultAudioDevice(string deviceName, DeviceClass deviceClass) { var audioDevices = await DeviceInformation.FindAllAsync(deviceClass); foreach (var device in audioDevices) { if (device.Name.Contains(deviceName)) { // 使用CoreAudio API设置默认设备 var enumerator = new MMDeviceEnumerator(); var mmDevice = enumerator.GetDevice(device.Id); EDataFlow flow = deviceClass == DeviceClass.AudioRender ? EDataFlow.eRender : EDataFlow.eCapture; enumerator.SetDefaultAudioEndpoint(flow, ERole.eMultimedia, mmDevice); Debug.WriteLine($"已设置默认{deviceClass}设备: {device.Name}"); break; } } }
关键注意事项
- 权限配置:在
Package.appxmanifest中添加蓝牙权限:<Capabilities> <DeviceCapability Name="bluetooth" /> <DeviceCapability Name="bluetooth.rfcomm"> <Device Id="any"> <Function Type="serviceId:0000110b-0000-1000-8000-00805f9b34fb" /> <!-- A2DP服务 --> <Function Type="serviceId:0000111e-0000-1000-8000-00805f9b34fb" /> <!-- HFP服务 --> </Device> </DeviceCapability> </Capabilities> - 设备访问权限:首次运行时需要请求设备访问权限,可提前调用:
DeviceAccessStatus accessStatus = await DeviceInformation.RequestAccessAsync(di.Id); if (accessStatus != DeviceAccessStatus.Allowed) { Debug.WriteLine("设备访问权限被拒绝"); return; } - CoreAudio依赖:如果需要设置默认音频设备,需引用
NAudio库或使用Windows原生CoreAudio API的P/Invoke。
内容的提问来源于stack exchange,提问作者greven
相关产品推荐
相关产品推荐

