private void StartWatcher()
        {
            startWatcherButton.IsEnabled = false;
            resultCollection.Clear();
            DeviceWatcher deviceWatcher;

            // Get the device selector chosen by the UI then add additional constraints for devices that
            // can be paired or are already paired.
            DeviceSelectorInfo deviceSelectorInfo = (DeviceSelectorInfo)selectorComboBox.SelectedItem;
            string             selector           = "(" + deviceSelectorInfo.Selector + ")" + " AND (System.Devices.Aep.CanPair:=System.StructuredQueryType.Boolean#True OR System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True)";

            if (deviceSelectorInfo.Kind == DeviceInformationKind.Unknown)
            {
                // Kind will be determined by the selector
                deviceWatcher = DeviceInformation.CreateWatcher(
                    selector,
                    null // don't request additional properties for this sample
                    );
            }
            else
            {
                // Kind is specified in the selector info
                deviceWatcher = DeviceInformation.CreateWatcher(
                    selector,
                    null, // don't request additional properties for this sample
                    deviceSelectorInfo.Kind);
            }

            rootPage.NotifyUser("Starting Watcher...", NotifyType.StatusMessage);
            deviceWatcherHelper.StartWatcher(deviceWatcher);
            stopWatcherButton.IsEnabled = true;
        }
        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);
        }
        private void StartWatcher()
        {
            startWatcherButton.IsEnabled = false;
            resultCollection.Clear();

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

            DeviceWatcher deviceWatcher;

            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);
            }

            rootPage.NotifyUser("Starting Watcher...", NotifyType.StatusMessage);
            deviceWatcherHelper.StartWatcher(deviceWatcher);
            stopWatcherButton.IsEnabled = true;
        }
Esempio n. 4
0
        private async void ShowDevicePicker(bool pickSingle)
        {
            showDevicePickerButton.IsEnabled = false;
            resultCollection.Clear();

            var devicePicker = new DevicePicker();

            // 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
                devicePicker.Filter.SupportedDeviceClasses.Add(deviceSelectorInfo.DeviceClassSelector);
            }
            else
            {
                // Use AQS string selector from dynamic call to a device api's GetDeviceSelector call
                devicePicker.Filter.SupportedDeviceSelectors.Add(deviceSelectorInfo.Selector);
            }

            rootPage.NotifyUser("Showing Device Picker", NotifyType.StatusMessage);

            // Calculate the position to show the picker (right below the buttons)
            GeneralTransform ge    = pickSingleDeviceButton.TransformToVisual(null);
            Point            point = ge.TransformPoint(new Point());
            Rect             rect  = new Rect(point, new Point(point.X + pickSingleDeviceButton.ActualWidth, point.Y + pickSingleDeviceButton.ActualHeight));

            if (pickSingle)
            {
                DeviceInformation di = await devicePicker.PickSingleDeviceAsync(rect);

                if (null != di)
                {
                    resultCollection.Add(new DeviceInformationDisplay(di));
                }
                showDevicePickerButton.IsEnabled = true;
            }
            else
            {
                devicePicker.DevicePickerDismissed += new TypedEventHandler <DevicePicker, object>(
                    async(picker, obj) =>
                {
                    // Must access UI elements from the UI thread.
                    await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                    {
                        showDevicePickerButton.IsEnabled = true;
                        rootPage.NotifyUser("Hiding Device Picker", NotifyType.StatusMessage);
                    });
                });

                devicePicker.DeviceSelected += new TypedEventHandler <DevicePicker, DeviceSelectedEventArgs>(
                    async(picker, args) =>
                {
                    // Since we have the collection databound to a UI element, we need to update the collection on the UI thread.
                    await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                    {
                        resultCollection.Clear();
                        resultCollection.Add(new DeviceInformationDisplay(args.SelectedDevice));
                    });
                });

                // Show the picker
                devicePicker.Show(rect);
            }
        }