public void Pair(string name) { BluetoothDevice unbonded = this.UnBondedDevices.FirstOrDefault(device => device.Name == name); // Status is failed by default // TODO - check if needed another for BLE BTPairOperationStatus status = new BTPairOperationStatus() { Name = name, }; try { if (unbonded != null) { if (unbonded.CreateBond()) { status.IsSuccessful = true; status.PairStatus = BT_PairingStatus.Paired; } else { status.PairStatus = BT_PairingStatus.AuthenticationFailure; } } else { status.PairStatus = BT_PairingStatus.NoParingObject; } } catch (Exception e) { this.log.Exception(9999, "PairgAsync", "", e); status.PairStatus = BT_PairingStatus.Failed; } this.BT_PairStatus?.Invoke(this, status); }
private void BTClassic_PairStatus(object sender, BTPairOperationStatus e) { ErrReport report; WrapErr.ToErrReport(out report, 20015, "Failure on BTClassic_PairStatus", () => { this.BT_PairStatus?.Invoke(sender, e); }); this.RaiseIfException(report); }
private void pairStatusHandler(object sender, BTPairOperationStatus e) { this.Dispatcher.Invoke(() => { Log.InfoEntry("BTSettings", "pairStatusHandler"); this.gridWait.Collapse(); DI.Wrapper.BT_PairStatus -= this.pairStatusHandler; if (!e.IsSuccessful) { App.ShowMsg(e.PairStatus.ToString()); } }); }
private void BT_PairStatusHandler(object sender, BTPairOperationStatus e) { this.log.InfoEntry("BT_PairStatusHandler"); this.Dispatcher.Invoke(() => { this.gridWait.Collapse(); if (e.IsSuccessful) { this.BT_RemoveEntry(e.Name); this.SetBTCheckUncheckButtons(); } else { this.ShowMsgBox(this.wrapper.GetText(MsgCode.Error), e.PairStatus.ToString()); } }); }
private void BT_PairStatusHandler(object sender, BTPairOperationStatus e) { Device.BeginInvokeOnMainThread(() => { if (e.IsSuccessful) { this.lstDevices.ItemsSource = null; var d = this.devices.FirstOrDefault(x => x.Name == e.Name); if (d != null) { this.devices.Remove(d); } this.lstDevices.ItemsSource = this.devices; } else { this.OnErr(e.PairStatus.ToString()); } }); }
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, })); } }