locked
BLE modelled on sample is not working RRS feed

  • Question

  • I am modelling an app on the BLE UWP sample. The app fails to connect. It can pair but doesn't connect. When running the sample app it asks permission before connecting the the device; is that a proble with my program?

     BluetoothLEDevice.FromIdAsync(NordicID) is not working 

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    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.Devices.Bluetooth;
    using Windows.Devices.Bluetooth.GenericAttributeProfile;
    using System.Collections.ObjectModel;
    using System.Threading.Tasks;
    using Windows.Devices.Enumeration;
    using Windows.UI.Core;
    using System.Diagnostics;


    // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

    namespace ThisApp
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            private ObservableCollection<BluetoothLEDeviceDisplay> KnownDevices = new ObservableCollection<BluetoothLEDeviceDisplay>();
            private BluetoothLEDeviceDisplay NordicDevice;
            private string message;
            private DeviceWatcher deviceWatcher;
            private bool connectable = false;
            private string NordicID = "BluetoothLE#BluetoothLEc0:33:5e:1a:20:fa-ee:0c:af:fa:******";

            private BluetoothLEDevice bluetoothLeDevice = null;
            private bool connected = false;

            public MainPage()
            {
                this.InitializeComponent();
                Initialize();

            }
            private async System.Threading.Tasks.Task Initialize()
            {
                string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.Bluetooth.Le.IsConnectable" };

                // BT_Code: Example showing paired and non-paired in a single query.
                string aqsAllBluetoothLEDevices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";

                deviceWatcher =
                        DeviceInformation.CreateWatcher(
                            aqsAllBluetoothLEDevices,
                            requestedProperties,
                            DeviceInformationKind.AssociationEndpoint);

                deviceWatcher.Added += DeviceWatcher_Added;
                deviceWatcher.Start();
            }
            private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation deviceInfo)
            {
                // We must update the collection on the UI thread because the collection is databound to a UI element.
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    lock (this)
                    {
                        Debug.WriteLine(String.Format("Added {0}{1}", deviceInfo.Id, deviceInfo.Name));

                        // Protect against race condition if the task runs after the app stopped the deviceWatcher.
                        if (sender == deviceWatcher)
                        {
                            // Make sure device isn't already present in the list.
                            if (FindBluetoothLEDeviceDisplay(deviceInfo.Id) == null)
                            {
                                if (deviceInfo.Name != string.Empty)
                                {
                                    // If device has a friendly name display it immediately.
                                    //KnownDevices.Add(new BluetoothLEDeviceDisplay(deviceInfo));
                                    if (deviceInfo.Id == NordicID)
                                    {
                                        NordicDevice = new BluetoothLEDeviceDisplay(deviceInfo);
                                        ConnectToNordic();

                                        deviceWatcher.Stop();
                                        deviceWatcher = null;
                                    }

                                }
                            }

                        }
                    }
                });
            }
            private async Task ConnectToNordic()
            {
                DevicePairingResult result = await NordicDevice.DeviceInformation.Pairing.PairAsync();
                if (connectable = result.Status == DevicePairingResultStatus.Paired || result.Status == DevicePairingResultStatus.AlreadyPaired)
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                    {
                        bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(NordicID);
                    });

                    if (bluetoothLeDevice != null)
                        connected = true;
                }
            }
            private BluetoothLEDeviceDisplay FindBluetoothLEDeviceDisplay(string id)
            {
                foreach (BluetoothLEDeviceDisplay bleDeviceDisplay in KnownDevices)
                {
                    if (bleDeviceDisplay.Id == id)
                    {
                        return bleDeviceDisplay;
                    }
                }
                return null;
            }

    Sunday, March 18, 2018 5:22 PM

Answers

  • Hi andy229,

    >> BluetoothLEDevice.FromIdAsync(NordicID) is not working 

    Is there any message for it? Does it crash?

    May I know the OS version and the target version of your app?

    Another is that please check if you have enabled the Bluetooth Capability in the manifest file.

    Best regards

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by andy229 Monday, March 19, 2018 11:31 AM
    Monday, March 19, 2018 7:19 AM