// Devices List View: Selection Changed Event //____________________________________________ private async void devicesListView_SelectionChanged(object sender, RoutedEventArgs e) { DeviceInformationDisplay deviceInfoDisp = devicesListView.SelectedItem as DeviceInformationDisplay; await Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { if (deviceInfoDisp != null) { pairingCommandBar.Background = (SolidColorBrush)Application.Current.Resources["CommandBarBackground"]; if (deviceInfoDisp.IsPaired) { pairingStatusTextBlock.Text = $"{deviceInfoDisp.Name} is already paired. You can connect to this device."; pairButton.Label = "Unpair"; pairButton.Icon = new SymbolIcon(Symbol.Clear); rootPage.selectedDeviceId = deviceInfoDisp.Id; rootPage.selectedDeviceName = deviceInfoDisp.Name; } else { pairingStatusTextBlock.Text = $"{deviceInfoDisp.Name} is not paired."; pairButton.Label = "Pair"; pairButton.Icon = new SymbolIcon(Symbol.Add); } } }); }
// Device Watcher Event: Device Updated //_____________________________________ private async void deviceWatcher_UpdatedAsync(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { // Collection must be update in the UI await devicesListView.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { // Protect against race condition if (sender == deviceWatcher) { DeviceInformationDisplay deviceInfoDisp = ResultCollection.Where(x => x.Id == deviceInfoUpdate.Id) as DeviceInformationDisplay; if (deviceInfoDisp != null) { deviceInfoDisp.Update(deviceInfoUpdate); } } }); }
// Device Watcher Event: Device Removed //_____________________________________ private async void deviceWatcher_RemovedAsync(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) { // We must update the collection in the UI await devicesListView.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { // Protect against race condition if (sender == deviceWatcher) { // Find the device in the collection and remove it DeviceInformationDisplay deviceInfoDisp = ResultCollection.Where(x => x.Id == deviceInfoUpdate.Id) as DeviceInformationDisplay; if (deviceInfoDisp != null) { ResultCollection.Remove(deviceInfoDisp); } } }); }
// -->> Device Pairing/Unpairing <<-- // Pair Button: Click Event //___________________________________ private async void pairButton_Click(object sender, RoutedEventArgs e) { pairButton.IsEnabled = false; devicesListView.IsEnabled = false; rootPage.ShowProgressRing(pairingProgressRing, true); pairingCommandBar.Background = (SolidColorBrush)Application.Current.Resources["CommandBarBackground"]; DeviceInformationDisplay deviceInfoSelected = devicesListView.SelectedItem as DeviceInformationDisplay; if (deviceInfoSelected != null) // Checks a device has been selected { bool paired = true; if (deviceInfoSelected.IsPaired != true) // Is device unpaired? { // Pair Selectede Device paired = false; // Selects all the available ceremonies for pairing DevicePairingKinds ceremoniesSelected = DevicePairingKinds.ConfirmOnly | DevicePairingKinds.DisplayPin | DevicePairingKinds.ProvidePin | DevicePairingKinds.ConfirmPinMatch; DevicePairingProtectionLevel protectionLevel = DevicePairingProtectionLevel.Default; // Specify a custom pairing with all ceremony types and EncryptionAndAuthentication protection DeviceInformationCustomPairing customPairing = deviceInfoSelected.DeviceInformation.Pairing.Custom; customPairing.PairingRequested += PairingRequested_EventHandler; // Requesting pairing ... pairingStatusTextBlock.Text = $"Pairing to {deviceInfoSelected.Name}"; // -->> Pairing device DevicePairingResult result = await customPairing.PairAsync(ceremoniesSelected, protectionLevel); customPairing.PairingRequested -= PairingRequested_EventHandler; switch (result.Status) { case DevicePairingResultStatus.Paired: // Pairing succeeded paired = true; break; case DevicePairingResultStatus.AccessDenied: // Permission denied pairingStatusTextBlock.Text = "Operation cancelled by the user"; break; default: // Failed pairingStatusTextBlock.Text = $"Pairing to {deviceInfoSelected.Name} failed"; pairingCommandBar.Background = new SolidColorBrush(Colors.Tomato); break; } if (paired) { // Device paired correctly StopWatcher(); StartWatcher(); pairingStatusTextBlock.Text = $"Successfully paired to {deviceInfoSelected.Name}."; pairingCommandBar.Background = new SolidColorBrush(Colors.LightGreen); pairButton.Icon = new SymbolIcon(Symbol.Clear); // Saving the Device ID and Name for future use rootPage.selectedDeviceId = deviceInfoSelected.Id; rootPage.selectedDeviceName = deviceInfoSelected.Name; } } else if (deviceInfoSelected.IsPaired) // Else, device is already paired { // Unpair device pairingStatusTextBlock.Text = $"Unpairing {deviceInfoSelected.Name}"; // -->> Unpairing device DeviceInformationPairing deviceUnpairing = deviceInfoSelected.DeviceInformation.Pairing; DeviceUnpairingResult result = await deviceUnpairing.UnpairAsync(); switch (result.Status) { case DeviceUnpairingResultStatus.Unpaired: // Succeeded StopWatcher(); StartWatcher(); pairingStatusTextBlock.Text = $"{deviceInfoSelected.Name} unpaired successfully"; pairingCommandBar.Background = new SolidColorBrush(Colors.LightGreen); pairButton.Content = "Pair/Unpair Device"; rootPage.selectedDeviceId = null; rootPage.selectedDeviceName = null; break; case DeviceUnpairingResultStatus.AccessDenied: // Permission denied pairingStatusTextBlock.Text = "Operation cancelled by the user"; break; default: // Failed pairingStatusTextBlock.Text = "Unpairing failed!"; pairingCommandBar.Background = new SolidColorBrush(Colors.Tomato); break; } } } rootPage.ShowProgressRing(pairingProgressRing, false); pairButton.IsEnabled = true; devicesListView.IsEnabled = true; }