Exemple #1
0
        private void StartWatcher()
        {
            DeviceWatcherEventKind[] triggerEventKinds = { DeviceWatcherEventKind.Add, DeviceWatcherEventKind.Remove /*, DeviceWatcherEventKind.Update */ };
            //IEnumerable<DeviceWatcherEventKind> triggerEventKinds = DeviceWatcherEventKind.Add;
            DeviceWatcher deviceWatcher = null;

            string selector = SmartCardReader.GetDeviceSelector();

            selector += " AND System.Devices.DeviceInstanceId:~~\"Ledger\"";

            deviceWatcher = DeviceInformation.CreateWatcher(selector, null);
            DeviceWatcherTrigger deviceWatcherTrigger = deviceWatcher.GetBackgroundTrigger(triggerEventKinds);

            RegisterTask(deviceWatcherTrigger);
        }
Exemple #2
0
        private async void ToggleSwitch_Toggled(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            ToggleSwitch toggleSwitch = sender as ToggleSwitch;

            if (toggleSwitch != null)
            {
                var TaskName = "background.Task";
                if (toggleSwitch.IsOn == true)
                {
                    var taskRegistered = false;
                    foreach (var task in BackgroundTaskRegistration.AllTasks)
                    {
                        if (task.Value.Name == TaskName)
                        {
                            taskRegistered = true;
                            break;
                        }
                    }
                    if (taskRegistered != true)
                    {
                        var builder = new BackgroundTaskBuilder();
                        builder.Name           = TaskName;
                        builder.TaskEntryPoint = TaskName;
                        DeviceWatcher            deviceWatcher     = DeviceInformation.CreateWatcher();
                        DeviceWatcherEventKind[] triggerEventKinds = { DeviceWatcherEventKind.Add };
                        IBackgroundTrigger       trigger           = deviceWatcher.GetBackgroundTrigger(triggerEventKinds);
                        builder.SetTrigger(trigger);
                        await BackgroundExecutionManager.RequestAccessAsync();

                        BackgroundTaskRegistration task = builder.Register();
                        task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);
                    }
                    else
                    {
                        foreach (var task in BackgroundTaskRegistration.AllTasks)
                        {
                            if (task.Value.Name == TaskName)
                            {
                                task.Value.Unregister(true);
                                break;
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        private void StartWatcher()
        {
            DeviceWatcherEventKind[] triggerEventKinds = { DeviceWatcherEventKind.Add, DeviceWatcherEventKind.Remove, DeviceWatcherEventKind.Update };
            DeviceWatcher            deviceWatcher     = null;

            startWatcherButton.IsEnabled = false;

            // First get the device selector chosen by the UI.
            DeviceSelectorInfo deviceSelectorInfo = (DeviceSelectorInfo)selectorComboBox.SelectedItem;

            if (null == deviceSelectorInfo.Selector)
            {
                // If the a pre-canned device class selector was chosen, call the DeviceClass overload
                deviceWatcher = DeviceInformation.CreateWatcher(deviceSelectorInfo.DeviceClassSelector);
            }
            else if (deviceSelectorInfo.Kind == DeviceInformationKind.Unknown)
            {
                // Use AQS string selector from dynamic call to a device api's GetDeviceSelector call
                // Kind will be determined by the selector
                deviceWatcher = DeviceInformation.CreateWatcher(
                    deviceSelectorInfo.Selector,
                    null // don't request additional properties for this sample
                    );
            }
            else
            {
                // Kind is specified in the selector info
                deviceWatcher = DeviceInformation.CreateWatcher(
                    deviceSelectorInfo.Selector,
                    null, // don't request additional properties for this sample
                    deviceSelectorInfo.Kind);
            }

            // Get the background trigger for this watcher
            DeviceWatcherTrigger deviceWatcherTrigger = deviceWatcher.GetBackgroundTrigger(triggerEventKinds);

            rootPage.NotifyUser("Starting Watcher...", NotifyType.StatusMessage);

            // Register this trigger for our background task
            RegisterBackgroundTask(deviceWatcherTrigger);

            stopWatcherButton.IsEnabled = true;
            rootPage.NotifyUser("Watcher started...", NotifyType.StatusMessage);
        }