private async void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            await DeviceListPage.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                lock (this)
                {
                    // Debug.WriteLine(String.Format("Updated {0}{1}", deviceInfoUpdate.Id, ""));

                    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
                    if (sender == deviceWatcher)
                    {
                        BLEDeviceInfo bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id);
                        if (bleDeviceDisplay != null)
                        {
                            // Device is already being displayed - update UX.
                            BLEDevices.Remove(bleDeviceDisplay);
                            NotifyStatusMessage?.Invoke(bleDeviceDisplay.Name + " Removed.");
                            return;
                        }

                        DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id);
                        if (deviceInfo != null)
                        {
                            BLEUnknownDevices.Remove(deviceInfo);
                            NotifyStatusMessage?.Invoke(deviceInfo.Id + " Removed.");
                        }
                    }
                }
            });
        }
        private async void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate)
        {
            await DeviceListPage.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                lock (this)
                {
                    // Debug.WriteLine(String.Format("Updated {0}{1}", deviceInfoUpdate.Id, ""));

                    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
                    if (sender == deviceWatcher)
                    {
                        BLEDeviceInfo bleDeviceDisplay = FindBluetoothLEDeviceDisplay(deviceInfoUpdate.Id);
                        if (bleDeviceDisplay != null)
                        {
                            // Device is already being displayed - update UX.
                            bleDeviceDisplay.Update(deviceInfoUpdate);
                            return;
                        }

                        DeviceInformation deviceInfo = FindUnknownDevices(deviceInfoUpdate.Id);
                        if (deviceInfo != null)
                        {
                            // If device has been updated with a friendly name it's no longer unknown.
                            if (deviceInfo.Name != String.Empty)
                            {
                                NotifyStatusMessage?.Invoke("device found:" + deviceInfo.Name);
                                deviceInfo.Update(deviceInfoUpdate);
                                BLEDevices.Add(new BLEDeviceInfo(deviceInfo));
                                BLEUnknownDevices.Remove(deviceInfo);
                            }
                        }
                    }
                }
            });
        }
        public void StartDeviceSearch()
        {
            StopDeviceSearch();

            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.Updated += DeviceWatcher_Updated;
            deviceWatcher.Removed += DeviceWatcher_Removed;
            deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
            deviceWatcher.Stopped += DeviceWatcher_Stopped;
            BLEDevices.Clear();
            deviceWatcher.Start();
            NotifyStatusMessage?.Invoke("Device Search Initiated.");
        }
        private async void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation devinfo)
        {
            await DeviceListPage.dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                lock (this)
                {
                    if (!string.IsNullOrEmpty(devinfo.Name))
                    {
                        NotifyStatusMessage?.Invoke("device found:" + devinfo.Name);
                        BLEDevices.Add(new BLEDeviceInfo(devinfo));
                        // Debug.WriteLine("Name: {0} Id {1}", devinfo.Name, devinfo.Id);
                    }

                    else
                    {
                        // Add it to a list in case the name gets updated later.
                        BLEUnknownDevices.Add(devinfo);
                    }
                }
            });
        }