Reputation: 3
I am trying to set-up an rfcomm connection with a mobile device and after that I want to set-up hfp service level connection so that I can call from my 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
This is my code:
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;
}
}
}
}
}
}
Upvotes: 0
Views: 161