private async void ButtonConnect_Click(object sender, RoutedEventArgs e)
        {
            var selection = ConnectDevices.SelectedItems;

            if (selection.Count <= 0)
            {
                return;
            }
            var obj   = selection[0];
            var entry = (DeviceListEntry)obj;

            if (entry == null)
            {
                return;
            }
            // Create an EventHandlerForDevice to watch for the device we are connecting to
            EventHandlerForDevice.CreateNewEventHandlerForDevice();

            // Get notified when the device was successfully connected to or about to be closed
            EventHandlerForDevice.Current.OnDeviceConnected = OnDeviceConnected;
            EventHandlerForDevice.Current.OnDeviceClose     = OnDeviceClosing;

            // It is important that the FromIdAsync call is made on the UI thread because the consent prompt, when present,
            // can only be displayed on the UI thread. Since this method is invoked by the UI, we are already in the UI thread.
            var openSuccess =
                await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector);

            // Disable connect button if we connected to the device
            UpdateConnectDisconnectButtonsAndList(!openSuccess);
            await MainPage.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                MainPage.Current.GoToPage(1);
            });
        }
        /// <summary>
        /// If all the devices have been enumerated, select the device in the list we connected to. Otherwise let the EnumerationComplete event
        /// from the device watcher handle the device selection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="deviceInformation"></param>
        private void OnDeviceConnected(EventHandlerForDevice sender, DeviceInformation deviceInformation)
        {
            // Find and select our connected device
            if (_isAllDevicesEnumerated)
            {
                SelectDeviceInList(EventHandlerForDevice.Current.DeviceInformation.Id);

                ButtonDisconnect.IsEnabled = true;
            }

            if (EventHandlerForDevice.Current.Device.PortName != "")
            {
                MainPage.Current.NotifyUser("Connected to - " +
                                            EventHandlerForDevice.Current.Device.PortName +
                                            " - " +
                                            EventHandlerForDevice.Current.DeviceInformation.Id, NotifyType.StatusMessage);
            }
            else
            {
                MainPage.Current.NotifyUser("Connected to - " +
                                            EventHandlerForDevice.Current.DeviceInformation.Id, NotifyType.StatusMessage);
            }
            // ReSharper disable once UnusedVariable
            var asyncAction = MainPage.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => MainPage.Current.GoToPage(1));
        }
 /// <summary>
 /// The device was closed. If we will autoreconnect to the device, reflect that in the UI
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="deviceInformation"></param>
 private async void OnDeviceClosing(EventHandlerForDevice sender, DeviceInformation deviceInformation)
 {
     await MainPage.Current.Dispatcher.RunAsync(
         CoreDispatcherPriority.Normal,
         () =>
     {
         // We were connected to the device that was unplugged, so change the "Disconnect from device" button
         // to "Do not reconnect to device"
         if (ButtonDisconnect.IsEnabled)
         {
             ButtonDisconnect.IsEnabled = false;
         }
     });
 }
 /// <summary>
 /// Creates a new instance of EventHandlerForDevice, enables auto reconnect, and uses it as the Current instance.
 /// </summary>
 public static void CreateNewEventHandlerForDevice()
 {
     _eventHandlerForDevice = new EventHandlerForDevice();
 }