Ejemplo n.º 1
0
        /// <summary>
        /// Unregister from App events and DeviceWatcher events because this page will be unloaded.
        /// </summary>
        /// <param name="eventArgs"></param>
        //protected override void OnNavigatedFrom(NavigationEventArgs eventArgs)
        //{
        //    StopDeviceWatchers();
        //    StopHandlingAppEvents();

        //    // We no longer care about the device being connected
        //    EventHandlerForDevice.Current.OnDeviceConnected = null;
        //    EventHandlerForDevice.Current.OnDeviceClose = null;
        //}

        public async void ConnectToDevice_Click()
        {
            var             selection = ConnectDevicesSelectedItems;
            DeviceListEntry entry     = null;

            if (selection != null)
            {
                var obj = selection;
                entry = (DeviceListEntry)obj;

                if (entry != null)
                {
                    // 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 = this.OnDeviceConnected;
                    EventHandlerForDevice.Current.OnDeviceClose     = this.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.

                    await Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(
                        CoreDispatcherPriority.Normal,
                        async() =>
                    {
                        Boolean openSuccess = await EventHandlerForDevice.Current.OpenDeviceAsync(entry.DeviceInformation, entry.DeviceSelector);
                        UpdateConnectDisconnectButtonsAndList(!openSuccess);
                    });

                    // Disable connect button if we connected to the device
                }
            }
        }
Ejemplo n.º 2
0
 /// <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 Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
         CoreDispatcherPriority.Normal,
         new DispatchedHandler(() =>
     {
         // We were connected to the device that was unplugged, so change the "Disconnect from device" button
         // to "Do not reconnect to device"
         if (ButtonDisconnectFromDeviceIsEnabled && EventHandlerForDevice.Current.IsEnabledAutoReconnect)
         {
             ButtonDisconnectFromDeviceContent = ButtonNameDisableReconnectToDevice;
         }
     }));
 }
Ejemplo n.º 3
0
        /// <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);

                ButtonDisconnectFromDeviceContent = ButtonNameDisconnectFromDevice;
            }

            if (EventHandlerForDevice.Current.Device.PortName != "")
            {
                SerialMain.NotifyUser("Connected to - " +
                                      EventHandlerForDevice.Current.Device.PortName +
                                      " - " +
                                      EventHandlerForDevice.Current.DeviceInformation.Id, NotifyType.StatusMessage);
            }
            else
            {
                SerialMain.NotifyUser("Connected to - " +
                                      EventHandlerForDevice.Current.DeviceInformation.Id, NotifyType.StatusMessage);
            }
        }
Ejemplo n.º 4
0
 /// <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();
 }