private async Task<int> unpair_ganglion_async () { if (ganglion_info == null) { return (int)CustomExitCodes.GANGLION_NOT_FOUND_ERROR; } if (ganglion_device == null) { return (int)CustomExitCodes.GANGLION_IS_NOT_OPEN_ERROR; } DeviceUnpairingResult result = null; DeviceInformationPairing pairing_information = ganglion_device.DeviceInformation.Pairing; try { if (pairing_information.IsPaired) { result = await ganglion_device.DeviceInformation.Pairing.UnpairAsync ().AsTask ().TimeoutAfter (timeout); } else { return (int)CustomExitCodes.GANGLION_IS_NOT_PAIRED_ERROR; } } catch (TimeoutException e) { return (int)CustomExitCodes.TIMEOUT_ERROR; } catch (Exception e) { return (int)CustomExitCodes.GENERAL_ERROR; } return (int)CustomExitCodes.STATUS_OK; }
static async void PairBluetooth(string param) { DevicePairingResult result = null; DeviceInformationPairing pairingInformation = _selectedDevice.DeviceInformation.Pairing; await _selectedDevice.DeviceInformation.Pairing.UnpairAsync(); if (pairingInformation.CanPair) { result = await _selectedDevice.DeviceInformation.Pairing.PairAsync(pairingInformation.ProtectionLevel); } }
async void RegisterForInboundPairingRequests() { // Make the system discoverable for Bluetooth await MakeDiscoverable(); // If the attempt to make the system discoverable failed then likely there is no Bluetooth device present // so leave the diagnositic message put out by the call to MakeDiscoverable() if (App.IsBluetoothDiscoverable) { string formatString; string confirmationMessage; // Get state of ceremony checkboxes DevicePairingKinds ceremoniesSelected = GetSelectedCeremonies(); int iCurrentSelectedCeremonies = (int)ceremoniesSelected; // Find out if we changed the ceremonies we orginally registered with - if we have registered before these will be saved var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; Object supportedPairingKinds = localSettings.Values["supportedPairingKinds"]; int iSavedSelectedCeremonies = -1; // Deliberate impossible value if (supportedPairingKinds != null) { iSavedSelectedCeremonies = (int)supportedPairingKinds; } ResourceLoader loader = ResourceLoader.GetForCurrentView(); if (!DeviceInformationPairing.TryRegisterForAllInboundPairingRequests(ceremoniesSelected)) { confirmationMessage = loader.GetString("BluetoothInboundRegistrationFailed/Text"); } else { // Save off the ceremonies we registered with localSettings.Values["supportedPairingKinds"] = iCurrentSelectedCeremonies; formatString = loader.GetString("BluetoothInboundRegistrationSucceeded/Text"); confirmationMessage = formatString + ceremoniesSelected.ToString(); } // Clear the current collection BluetoothDevices.Clear(); // Start the watcher StartWatcher(); // Display a message confirmationMessage += loader.GetString("BluetoothOn/Text"); DisplayMessagePanelAsync(confirmationMessage, MessageType.InformationalMessage); } }
private async void RegisterForInboundPairingRequests() { // Make the system discoverable for Bluetooth await MakeDiscoverable(); // If the attempt to make the system discoverable failed then likely there is no Bluetooth device present // so leave the diagnositic message put uot by the call to MakeDiscoverable() if (App.IsBluetoothDiscoverable) { string formatString; string confirmationMessage; // Get state of ceremony checkboxes DevicePairingKinds ceremoniesSelected = GetSelectedCeremonies(); int iCurrentSelectedCeremonies = (int)ceremoniesSelected; // Find out if we changed the ceremonies we orginally registered with - if we have registered before these will be saved var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; Object supportedPairingKinds = localSettings.Values["supportedPairingKinds"]; int iSavedSelectedCeremonies = -1; // Deliberate impossible value if (supportedPairingKinds != null) { iSavedSelectedCeremonies = (int)supportedPairingKinds; } if (!DeviceInformationPairing.TryRegisterForAllInboundPairingRequests(ceremoniesSelected)) { confirmationMessage = BluetoothDeviceInformationDisplay.GetResourceString("BluetoothInboundRegistrationFailed"); } else { // Save off the ceremonies we registered with localSettings.Values["supportedPairingKinds"] = iCurrentSelectedCeremonies; formatString = BluetoothDeviceInformationDisplay.GetResourceString("BluetoothInboundRegistrationSucceededFormat"); confirmationMessage = string.Format(formatString, ceremoniesSelected.ToString()); } DisplayMessagePanel(confirmationMessage, MessageType.InformationalMessage); // Enable the watcher switch BluetoothWatcherToggle.IsEnabled = true; } else { // Disable the watcher switch BluetoothWatcherToggle.IsEnabled = false; } }
/// <summary> /// Register the device for inbound pairing requests /// </summary> private async void RegisterForInboundPairingRequests() { // Make the system discoverable for Bluetooth. await MakeDiscoverable(); string confirmationMessage = string.Empty; // If the attempt to make the system discoverable failed then likely there is no Bluetooth device present, // so leave the diagnostic message put up by the call to MakeDiscoverable(). if (_isBluetoothDiscoverable) { // Get state of ceremony checkboxes DevicePairingKinds ceremoniesSelected = GetSelectedCeremonies(); int iCurrentSelectedCeremonies = (int)ceremoniesSelected; int iSavedSelectedCeremonies = -1; // Deliberate impossible value // Find out if we changed the ceremonies we originally registered with. // If we have registered before, these will be saved. object supportedPairingKinds = LocalSettings.Values["supportedPairingKinds"]; if (supportedPairingKinds != null) { iSavedSelectedCeremonies = (int)supportedPairingKinds; } if (!DeviceInformationPairing.TryRegisterForAllInboundPairingRequests(ceremoniesSelected)) { confirmationMessage = string.Format(BluetoothInboundRegistrationFailed, ceremoniesSelected.ToString()); } else { // Save off the ceremonies we registered with LocalSettings.Values["supportedPairingKinds"] = iCurrentSelectedCeremonies; confirmationMessage = string.Format(BluetoothInboundRegistrationSucceededFormat, ceremoniesSelected.ToString()); } BluetoothWatcherEnabled = true; } else { BluetoothWatcherEnabled = false; } await DisplayMessagePanel(confirmationMessage, MessageType.InformationalMessage); }
public async Task <bool> RequestPairDeviceAsync(DeviceInformationPairing pairing) { WiFiDirectConnectionParameters connectionParams = new WiFiDirectConnectionParameters(); short?groupOwnerIntent = Utils.GetSelectedItemTag <short?>(cmbGOIntent); if (groupOwnerIntent.HasValue) { connectionParams.GroupOwnerIntent = groupOwnerIntent.Value; } DevicePairingKinds devicePairingKinds = DevicePairingKinds.None; if (_supportedConfigMethods.Count > 0) { // If specific configuration methods were added, then use them. foreach (var configMethod in _supportedConfigMethods) { connectionParams.PreferenceOrderedConfigurationMethods.Add(configMethod); devicePairingKinds |= WiFiDirectConnectionParameters.GetDevicePairingKinds(configMethod); } } else { // If specific configuration methods were not added, then we'll use these pairing kinds. devicePairingKinds = DevicePairingKinds.ConfirmOnly | DevicePairingKinds.DisplayPin | DevicePairingKinds.ProvidePin; } connectionParams.PreferredPairingProcedure = Utils.GetSelectedItemTag <WiFiDirectPairingProcedure>(cmbPreferredPairingProcedure); DeviceInformationCustomPairing customPairing = pairing.Custom; customPairing.PairingRequested += OnPairingRequested; DevicePairingResult result = await customPairing.PairAsync(devicePairingKinds, DevicePairingProtectionLevel.Default, connectionParams); if (result.Status != DevicePairingResultStatus.Paired) { //rootPage.NotifyUser($"PairAsync failed, Status: {result.Status}", NotifyType.ErrorMessage); return(false); } return(true); }
public async Task <bool> RequestPairDeviceAsync(DeviceInformationPairing pairing) { WiFiDirectConnectionParameters connectionParams = new WiFiDirectConnectionParameters(); short?groupOwnerIntent = 1; if (groupOwnerIntent.HasValue) { connectionParams.GroupOwnerIntent = groupOwnerIntent.Value; } DevicePairingKinds devicePairingKinds = DevicePairingKinds.None; //if (_supportedConfigMethods.Count > 0) //{ // // If specific configuration methods were added, then use them. // foreach (var configMethod in _supportedConfigMethods) // { // connectionParams.PreferenceOrderedConfigurationMethods.Add(configMethod); // devicePairingKinds |= WiFiDirectConnectionParameters.GetDevicePairingKinds(configMethod); // } //} //else { // If specific configuration methods were not added, then we'll use these pairing kinds. devicePairingKinds = DevicePairingKinds.ConfirmOnly;// | DevicePairingKinds.DisplayPin | DevicePairingKinds.ProvidePin; } connectionParams.PreferredPairingProcedure = WiFiDirectPairingProcedure.GroupOwnerNegotiation; DeviceInformationCustomPairing customPairing = pairing.Custom; customPairing.PairingRequested += OnPairingRequested; DevicePairingResult result = await customPairing.PairAsync(devicePairingKinds, DevicePairingProtectionLevel.Default, connectionParams); if (result.Status != DevicePairingResultStatus.Paired) { StatusBlock.Text = $"PairAsync failed, Status: {result.Status}"; return(false); } return(true); }
public async Task<bool> RequestPairDeviceAsync(DeviceInformationPairing pairing) { WiFiDirectConnectionParameters connectionParams = new WiFiDirectConnectionParameters(); short? groupOwnerIntent = Utils.GetSelectedItemTag<short?>(cmbGOIntent); if (groupOwnerIntent.HasValue) { connectionParams.GroupOwnerIntent = groupOwnerIntent.Value; } DevicePairingKinds devicePairingKinds = DevicePairingKinds.None; if (_supportedConfigMethods.Count > 0) { // If specific configuration methods were added, then use them. foreach (var configMethod in _supportedConfigMethods) { connectionParams.PreferenceOrderedConfigurationMethods.Add(configMethod); devicePairingKinds |= WiFiDirectConnectionParameters.GetDevicePairingKinds(configMethod); } } else { // If specific configuration methods were not added, then we'll use these pairing kinds. devicePairingKinds = DevicePairingKinds.ConfirmOnly | DevicePairingKinds.DisplayPin | DevicePairingKinds.ProvidePin; } connectionParams.PreferredPairingProcedure = Utils.GetSelectedItemTag<WiFiDirectPairingProcedure>(cmbPreferredPairingProcedure); DeviceInformationCustomPairing customPairing = pairing.Custom; customPairing.PairingRequested += OnPairingRequested; DevicePairingResult result = await customPairing.PairAsync(devicePairingKinds, DevicePairingProtectionLevel.Default, connectionParams); if (result.Status != DevicePairingResultStatus.Paired) { rootPage.NotifyUser($"PairAsync failed, Status: {result.Status}", NotifyType.ErrorMessage); return false; } return true; }
// -->> 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; }
private async Task DoPairing(BTDeviceInfo info) { try { // Code in example does not work but has the calls. This one forces a prompt // https://stackoverflow.com/questions/53010320/uwp-bluetooth-pairing-without-prompt this.log.Info("DoPairing", () => string.Format("'{0}'", info.Name)); using (BluetoothDevice device = await BluetoothDevice.FromIdAsync(info.Address)) { DeviceInformationPairing pairing = device.DeviceInformation.Pairing; BTPairOperationStatus data = new BTPairOperationStatus() { Name = info.Name, }; // TODO find if encryption required etc //DevicePairingProtectionLevel pl = p.ProtectionLevel; //pl. if (pairing == null) { data.PairStatus = BT_PairingStatus.NoParingObject; this.BT_PairStatus?.Invoke(this, data); } else if (!pairing.CanPair) { data.PairStatus = BT_PairingStatus.NotSupported; this.BT_PairStatus?.Invoke(this, data); } else { if (pairing.IsPaired) { data.IsSuccessful = true; data.PairStatus = BT_PairingStatus.AlreadyPaired; this.BT_PairStatus?.Invoke(this, data); } else { DeviceInformationCustomPairing cpi = pairing.Custom; cpi.PairingRequested += this.OnPairRequestedAsyncCallback; // TODO - need to figure out if requesting PIN or just confirm DevicePairingResult result = await cpi.PairAsync(DevicePairingKinds.ProvidePin); cpi.PairingRequested -= this.OnPairRequestedAsyncCallback; this.log.Info("DoPairing", () => string.Format("'{0}' Pair status {1}", info.Name, result.Status.ToString())); data.IsSuccessful = result.Status.IsSuccessful(); data.PairStatus = result.Status.ConvertStatus(); this.BT_PairStatus?.Invoke(this, data); } } } } catch (Exception e) { this.log.Exception(9999, "DoPairing", "", e); WrapErr.SafeAction(() => this.BT_PairStatus?.Invoke( this, new BTPairOperationStatus() { IsSuccessful = false, })); } }