/// <summary> /// User wants to unpair from the selected device /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void UnpairButton_Click(object sender, RoutedEventArgs e) { // Use the unpair button on the bluetoothDeviceListView.SelectedItem to get the data context BluetoothDeviceInformationDisplay deviceInfoDisp = ((Button)sender).DataContext as BluetoothDeviceInformationDisplay; string formatString; string confirmationMessage; Button unpairButton = sender as Button; // Disable the unpair button until we are done unpairButton.IsEnabled = false; DeviceUnpairingResult unpairingResult = await deviceInfoDisp.DeviceInformation.Pairing.UnpairAsync(); ResourceLoader loader = ResourceLoader.GetForCurrentView(); if (unpairingResult.Status == DeviceUnpairingResultStatus.Unpaired) { // Device is unpaired formatString = loader.GetString("BluetoothUnpairingSuccess/Text"); confirmationMessage = formatString + deviceInfoDisp.Name + " (" + deviceInfoDisp.Id + ")"; } else { formatString = loader.GetString("BluetoothUnpairingFailure/Text"); confirmationMessage = formatString + deviceInfoDisp.Name + " (" + deviceInfoDisp.Id + ")"; // unpairingResult.Status.ToString() } // Display the result of the pairing attempt ViewModel.DisplayMessagePanelAsync(confirmationMessage, SettingBluetoothViewModel.MessageType.InformationalMessage); // If the watcher toggle is on, clear any devices in the list and stop and restart the watcher to ensure state is reflected in list if (SwitchBluetooth.IsChecked.Value) { ViewModel.BluetoothDevices.Clear(); ViewModel.StopWatcher(); ViewModel.StartWatcher(); } else { // If the watcher is off this is an inbound request so just clear the list ViewModel.BluetoothDevices.Clear(); } // Re-enable the unpair button unpairButton.IsEnabled = true; }
/// <summary> /// User wants to use custom pairing with the selected ceremony types and Default protection level /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void PairButton_Click(object sender, RoutedEventArgs e) { // Use the pair button on the bluetoothDeviceListView.SelectedItem to get the data context BluetoothDeviceInformationDisplay deviceInfoDisp = ((Button)sender).DataContext as BluetoothDeviceInformationDisplay; ResourceLoader loader = ResourceLoader.GetForCurrentView(); string formatString = loader.GetString("BluetoothAttemptingToPair/Text"); string confirmationMessage = formatString + deviceInfoDisp.Name + " (" + deviceInfoDisp.Id + ")"; ViewModel.DisplayMessagePanelAsync(confirmationMessage, SettingBluetoothViewModel.MessageType.InformationalMessage); // Save the pair button Button pairButton = sender as Button; ViewModel.InProgressPairButton = pairButton; // Save the flyout and set to null so it doesn't pop up unless we want it ViewModel.SavedPairButtonFlyout = pairButton.Flyout; ViewModel.InProgressPairButton.Flyout = null; // Disable the pair button until we are done pairButton.IsEnabled = false; // Get ceremony type and protection level selections DevicePairingKinds ceremoniesSelected = ViewModel.GetSelectedCeremonies(); // Get protection level DevicePairingProtectionLevel protectionLevel = DevicePairingProtectionLevel.Default; // Specify custom pairing with all ceremony types and protection level EncryptionAndAuthentication DeviceInformationCustomPairing customPairing = deviceInfoDisp.DeviceInformation.Pairing.Custom; customPairing.PairingRequested += ViewModel.PairingRequestedHandler; DevicePairingResult result = await customPairing.PairAsync(ceremoniesSelected, protectionLevel); if (result.Status == DevicePairingResultStatus.Paired) { formatString = loader.GetString("BluetoothPairingSuccess/Text"); confirmationMessage = formatString + deviceInfoDisp.Name + " (" + deviceInfoDisp.Id + ")"; } else { formatString = loader.GetString("BluetoothPairingFailure/Text"); confirmationMessage = formatString + deviceInfoDisp.Name + " (" + deviceInfoDisp.Id + ")"; // result.Status.ToString() } // Display the result of the pairing attempt ViewModel.DisplayMessagePanelAsync(confirmationMessage, SettingBluetoothViewModel.MessageType.InformationalMessage); // If the watcher toggle is on, clear any devices in the list and stop and restart the watcher to ensure state is reflected in list if (SwitchBluetooth.IsChecked.Value) { ViewModel.BluetoothDevices.Clear(); ViewModel.StopWatcher(); ViewModel.StartWatcher(); } else { // If the watcher is off this is an inbound request so just clear the list ViewModel.BluetoothDevices.Clear(); } // Re-enable the pair button ViewModel.InProgressPairButton = null; pairButton.IsEnabled = true; }