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; } } } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)