Example #1
0
        /// <summary>
        /// If a HidDevice object has been instantiated (a handle to the device is opened), we must close it before the app
        /// goes into suspension because the API automatically closes it for us if we don't. When resuming, the API will
        /// not reopen the device automatically, so we need to explicitly open the device in the app (Scenario1_DeviceConnect).
        ///
        /// Since we have to reopen the device ourselves when the app resumes, it is good practice to explicitly call the close
        /// in the app as well (For every open there is a close).
        ///
        /// We must stop the DeviceWatchers because device watchers will continue to raise events even if
        /// the app is in suspension, which is not desired. We resume the device watcher once the app resumes again.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="eventArgs"></param>
        private void OnAppSuspension(Object sender, SuspendingEventArgs args)
        {
            if (WatchersStarted)
            {
                watchersSuspended = true;
                StopDeviceWatchers();
            }
            else
            {
                watchersSuspended = false;
            }

            DeviceConnect.CloseDevice();
        }
Example #2
0
        /// <summary>
        /// When resume into the application, we should reopen a handle to the Hid device again. Please see the comment in
        /// OnAppSuspension() for more details why we are doing this.
        ///
        /// See OnAppSuspension for why we are starting the device watchers again
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arg"></param>
        private void OnAppResume(Object sender, Object args)
        {
            if (watchersSuspended)
            {
                watchersSuspended = false;
                StartDeviceWatchers();
            }

            // Go to Scenario1_ConnectDevice to see the code for OpenDevice
            if (DeviceList.Current.PreviousDeviceId != null)
            {
                DeviceConnect.OpenDevice(DeviceList.Current.PreviousDeviceId);
            }
        }
Example #3
0
        /// <summary>
        /// When a device is removed, we need to check if it's the device we're using. If it is, we need to close the device
        /// so that all pending operations are canceled properly.
        ///
        /// We will also remove the device from the UI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="deviceInformationUpdate"></param>
        private async void OnDeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate deviceInformationUpdate)
        {
            if ((DeviceList.Current.IsDeviceConnected) &&
                (deviceInformationUpdate.Id == DeviceList.Current.CurrentDeviceEntry.Id))
            {
                await rootPage.Dispatcher.RunAsync(
                    CoreDispatcherPriority.Normal,
                    new DispatchedHandler(() =>
                {
                    rootPage.NotifyUser(
                        deviceInformationUpdate.Id + " was removed. Don't worry, we are closing the HidDevice",
                        NotifyType.StatusMessage);

                    DeviceList.Current.RemoveDeviceFromList(deviceInformationUpdate.Id);

                    DeviceConnect.CloseDevice();
                }));
            }
        }
Example #4
0
        /// <summary>
        /// Stops all device watchers.
        /// </summary>
        public void StopDeviceWatchers()
        {
            rootPage.NotifyUser("Stopping device watchers", NotifyType.StatusMessage);

            // Stop all device watchers
            foreach (DeviceWatcher deviceWatcher in deviceWatchers)
            {
                if ((deviceWatcher.Status == DeviceWatcherStatus.Started) ||
                    (deviceWatcher.Status == DeviceWatcherStatus.EnumerationCompleted))
                {
                    deviceWatcher.Stop();
                }
            }

            // Close the device so we don't have a device connected when the list of devices is empty
            DeviceConnect.CloseDevice();

            // Clear the list of devices so we don't have potentially disconnected devices around
            DeviceList.Current.ClearDeviceEntries();

            watchersStarted = false;
        }