Click here to Skip to main content
15,850,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code i am trying to setup a rfcomm connection with a mobile device and after that i wanna setup hfp service level connection so that i can call frommy windows pc and it gives Failed to get Bluetooth device from ID Bluetooth#Bluetooth00:45:e2:a8:f6:90-48:83:b4:17:24:c2: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) this error i have already added the bluetooth and bluetooth.rfcomm capability

What I have tried:

C#
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;
                    }
                }

            }
        }

    }

}
Posted
Updated 8-May-23 3:03am
v3
Comments
Pete O'Hanlon 9-May-23 13:23pm    
Do you know where in this code that it triggers the denied access operation? You have a number of lines of bluetooth code in there; knowing which one throws the exception would help diagnose the problem.
Aryaveer Chaudhary 10-May-23 1:01am    
in .net console app this code was working fine but in UWP app this line
await BTSocket.ConnectAsync(BTService.ConnectionHostName, BTService.ConnectionServiceName,
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
this gives access is denied instead of making rfcomm connection i also tried this https://learn.microsoft.com/en-us/windows/uwp/devices-sensors/send-or-receive-files-with-rfcomm from microsoft documentation but this was also not working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900