蓝牙UWP应用连接时出现E_ACCESSDENIED访问拒绝错误的原因
问题描述
尝试在Windows PC上与移动设备建立RFCOMM连接,进而建立HFP服务级连接以实现拨打电话功能,但遇到权限拒绝错误。
错误信息
从ID Bluetooth#Bluetooth00:45:e2:a8:f6:90-48:83:b4:17:24:c2获取蓝牙设备失败:访问被拒绝。(异常来自HRESULT: 0x80070005 (E_ACCESSDENIED))
已配置权限
已添加bluetooth和bluetooth.rfcomm权限
代码实现
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using Windows.Devices.Bluetooth; using Windows.Devices.Bluetooth.Rfcomm; using Windows.Devices.Enumeration; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.Networking.Sockets; using Windows.Security.Authorization.AppCapabilityAccess; using Windows.Storage.Streams; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Windows.UI.Popups; using Windows.Security.Authorization.AppCapabilityAccess; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace hfpuwp { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { public static readonly Guid HFP_Id = new Guid("0000111f-0000-1000-8000-00805f9b34fb"); public MainPage() { this.InitializeComponent(); } private async void Button_Click(object sender, RoutedEventArgs e) { BluetoothDevice BlutooDevice; RfcommDeviceService BTService; StreamSocket BTSocket; DataWriter _writer; DataReader _reader; DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelectorFromPairingState(true)); String xz = ""; for (int i = 0; i < devices.Count; i++) { xz += devices[i].Name+" "; //Console.WriteLine(i + "#: " + devices[i].Name + " " + devices[i].Id); if (devices[i].Name == "OPPO F11 Pro") { BTDevice.Text = devices[i].Name + " " + devices[i].Id; try { BlutooDevice = await BluetoothDevice.FromIdAsync(devices[i].Id); var result = await BlutooDevice.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(HFP_Id)); if (result.Services.Count > 0) { var accessStatus = await BlutooDevice.RequestAccessAsync(); BTService = result.Services[0]; BTSocket = new StreamSocket(); if (result.Services.Count > 0 && accessStatus == DeviceAccessStatus.Allowed) { await BTSocket.ConnectAsync(BTService.ConnectionHostName, BTService.ConnectionServiceName, SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication); } //_writer = new DataWriter(BTSocket.OutputStream); //_reader = new DataReader(BTSocket.InputStream); } BTDevice.Text = "Connection Sucessful"; } catch (Exception ex) { Debug.WriteLine($"Failed to get Bluetooth device from ID {devices[i].Id}: {ex.Message}"); continue; } } } } } }
内容的提问来源于stack exchange,提问作者Aryaveer Chaudhary
相关产品推荐
相关产品推荐

