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(); // 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); } // Hook up handlers for the watcher events before starting the watcher handlerAdded = new TypedEventHandler <DeviceWatcher, DeviceInformation>(async(watcher, deviceInfo) => { // 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.Add(new DeviceInformationDisplay(deviceInfo)); rootPage.NotifyUser( String.Format("{0} devices found.", ResultCollection.Count), NotifyType.StatusMessage); }); }); deviceWatcher.Added += handlerAdded; handlerUpdated = new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>(async(watcher, deviceInfoUpdate) => { // 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, () => { // Find the corresponding updated DeviceInformation in the collection and pass the update object // to the Update method of the existing DeviceInformation. This automatically updates the object // for us. foreach (DeviceInformationDisplay deviceInfoDisp in ResultCollection) { if (deviceInfoDisp.Id == deviceInfoUpdate.Id) { deviceInfoDisp.Update(deviceInfoUpdate); // If the item being updated is currently "selected", then update the pairing buttons DeviceInformationDisplay selectedDeviceInfoDisp = (DeviceInformationDisplay)resultsListView.SelectedItem; if (deviceInfoDisp == selectedDeviceInfoDisp) { UpdatePairingButtons(); } break; } } }); }); deviceWatcher.Updated += handlerUpdated; handlerRemoved = new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>(async(watcher, deviceInfoUpdate) => { // 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, () => { // Find the corresponding DeviceInformation in the collection and remove it foreach (DeviceInformationDisplay deviceInfoDisp in ResultCollection) { if (deviceInfoDisp.Id == deviceInfoUpdate.Id) { ResultCollection.Remove(deviceInfoDisp); break; } } rootPage.NotifyUser( String.Format("{0} devices found.", ResultCollection.Count), NotifyType.StatusMessage); }); }); deviceWatcher.Removed += handlerRemoved; handlerEnumCompleted = new TypedEventHandler <DeviceWatcher, Object>(async(watcher, obj) => { await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { rootPage.NotifyUser( String.Format("{0} devices found. Enumeration completed. Watching for updates...", ResultCollection.Count), NotifyType.StatusMessage); }); }); deviceWatcher.EnumerationCompleted += handlerEnumCompleted; handlerStopped = new TypedEventHandler <DeviceWatcher, Object>(async(watcher, obj) => { await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { rootPage.NotifyUser( String.Format("{0} devices found. Watcher {1}.", ResultCollection.Count, DeviceWatcherStatus.Aborted == watcher.Status ? "aborted" : "stopped"), NotifyType.StatusMessage); }); }); deviceWatcher.Stopped += handlerStopped; rootPage.NotifyUser("Starting Watcher...", NotifyType.StatusMessage); deviceWatcher.Start(); stopWatcherButton.IsEnabled = true; }
private async void ShowDevicePicker(bool pickSingle) { showDevicePickerButton.IsEnabled = false; ResultCollection.Clear(); 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) => { // 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, () => { 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); } }
private void StartWatcher() { startWatcherButton.IsEnabled = false; ResultCollection.Clear(); // 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); } // Hook up handlers for the watcher events before starting the watcher handlerAdded = new TypedEventHandler <DeviceWatcher, DeviceInformation>(async(watcher, deviceInfo) => { // 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.Add(new DeviceInformationDisplay(deviceInfo)); rootPage.NotifyUser( String.Format("{0} devices found.", ResultCollection.Count), NotifyType.StatusMessage); }); }); deviceWatcher.Added += handlerAdded; handlerUpdated = new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>(async(watcher, deviceInfoUpdate) => { // 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, () => { // Find the corresponding updated DeviceInformation in the collection and pass the update object // to the Update method of the existing DeviceInformation. This automatically updates the object // for us. foreach (DeviceInformationDisplay deviceInfoDisp in ResultCollection) { if (deviceInfoDisp.Id == deviceInfoUpdate.Id) { deviceInfoDisp.Update(deviceInfoUpdate); break; } } }); }); deviceWatcher.Updated += handlerUpdated; handlerRemoved = new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>(async(watcher, deviceInfoUpdate) => { // 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, () => { // Find the corresponding DeviceInformation in the collection and remove it foreach (DeviceInformationDisplay deviceInfoDisp in ResultCollection) { if (deviceInfoDisp.Id == deviceInfoUpdate.Id) { ResultCollection.Remove(deviceInfoDisp); break; } } rootPage.NotifyUser( String.Format("{0} devices found.", ResultCollection.Count), NotifyType.StatusMessage); }); }); deviceWatcher.Removed += handlerRemoved; handlerEnumCompleted = new TypedEventHandler <DeviceWatcher, Object>(async(watcher, obj) => { await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { rootPage.NotifyUser( String.Format("{0} devices found. Enumeration completed. Watching for updates...", ResultCollection.Count), NotifyType.StatusMessage); }); }); deviceWatcher.EnumerationCompleted += handlerEnumCompleted; handlerStopped = new TypedEventHandler <DeviceWatcher, Object>(async(watcher, obj) => { await rootPage.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { rootPage.NotifyUser( String.Format("{0} devices found. Watcher {1}.", ResultCollection.Count, DeviceWatcherStatus.Aborted == watcher.Status ? "aborted" : "stopped"), NotifyType.StatusMessage); }); }); deviceWatcher.Stopped += handlerStopped; rootPage.NotifyUser("Starting Watcher...", NotifyType.StatusMessage); deviceWatcher.Start(); stopWatcherButton.IsEnabled = true; }