//https://stackoverflow.com/questions/45191412/deviceinformation-pairasync-not-working-in-wpf private async Task DoUnPairing(BTDeviceInfo info) { try { using (BluetoothDevice device = await BluetoothDevice.FromIdAsync(info.Address)) { this.log.Info("DoUnPairing", () => string.Format("'{0}'", info.Name)); DeviceUnpairingResult result = await device.DeviceInformation.Pairing.UnpairAsync(); this.log.Info("DoUnPairing", () => string.Format("'{0}' Unpair status {1}", info.Name, result.Status.ToString())); this.BT_UnPairStatus?.Invoke(this, new BTUnPairOperationStatus() { Name = info.Name, UnpairStatus = result.Status.ConvertStatus(), IsSuccessful = result.Status.IsSuccessful(), }); } } catch (Exception e) { WrapErr.SafeAction(() => { this.BT_UnPairStatus?.Invoke(this, new BTUnPairOperationStatus() { Name = info.Name, UnpairStatus = BT_UnpairingStatus.Failed, IsSuccessful = false, }); }); } }
private void RaiseDeviceDiscovered(BluetoothDevice device) { if (this.deviceType == BluetoothDeviceType.Classic) { BTDeviceInfo info = new BTDeviceInfo() { IsPaired = device.BondState == Bond.Bonded, Name = device.Name, DeviceClassName = device.Class.Name, Address = device.Address, // TODO - any others as needed }; this.log.Info("RaiseDeviceDiscovered", () => string.Format( "{0} - {1} - {2}", info.Name, info.DeviceClassName, device.Address)); this.DiscoveredBTDevice?.Invoke(this, info); } else if (this.deviceType == BluetoothDeviceType.Le) { BluetoothLEDeviceInfo info = new BluetoothLEDeviceInfo() { // TODO - initialize }; this.DiscoveredBLEDevice?.Invoke(this, info); } else { this.log.Error(9999, "", () => string.Format("Unhandled device type:{0}", this.deviceType)); } }
public bool Connect(BTDeviceInfo device) { try { this.log.InfoEntry("Connect"); this.Disconnect(); this.currentDevice = new BluetoothClient(); BluetoothAddress address; if (!BluetoothAddress.TryParse(device.Address, out address)) { this.log.Error(9999, () => string.Format("Failed to parse out the address {0} on device {1}", device.Address, device.Name)); return(false); } this.currentDevice.Connect(address, BluetoothService.SerialPort); this.log.Info("Connect", () => string.Format("IsConnected:{0}", this.currentDevice.Connected)); return(this.currentDevice.Connected); } catch (Exception e) { this.log.Exception(8888, "", e); this.log.Info("Connect", "Exiting FALSE"); this.Disconnect(); return(false); } }
// It adds distinctively devices public void UpdateAdapter(BTDeviceInfo dataItem) { if (devices != null && dataItem != null && !devices.Any(device => dataItem.MacAddress != null && dataItem.MacAddress.Equals(device.MacAddress))) { devices.Add(dataItem); } }
/// <summary>Run asynchronous connection where ConnectionCompleted is raised on completion</summary> /// <param name="deviceDataModel">The data model with information on the device</param> public void ConnectAsync(BTDeviceInfo deviceDataModel) { Task.Run(async() => { try { this.log.InfoEntry("ConnectAsync"); this.msgPump.Disconnect(); await this.GetExtraInfo(deviceDataModel, false, false); this.log.Info("ConnectAsync", () => string.Format( "Host:{0} Service:{1}", deviceDataModel.RemoteHostName, deviceDataModel.RemoteServiceName)); this.msgPump.ConnectAsync(new SocketMsgPumpConnectData() { MaxReadBufferSize = READ_BUFF_MAX_SIZE, ProtectionLevel = SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication, RemoteHostName = deviceDataModel.RemoteHostName, ServiceName = deviceDataModel.RemoteServiceName, }); } catch (Exception e) { this.log.Exception(9999, "Connect Asyn Error", e); this.ConnectionCompleted?.Invoke(this, false); } }); }
public void ConnectAsync(BTDeviceInfo device) { try { this.Disconnect(); this.currentDevice = new BluetoothClient(); this.log.Info("ConnectAsync", () => string.Format("Starting connection of:{0} {1}", device.Name, device.Address)); IAsyncResult result = this.currentDevice.BeginConnect( BluetoothAddress.Parse(device.Address), BluetoothService.SerialPort, this.ConnectedCallback, this.currentDevice); this.log.Info("ConnectAsyn", () => string.Format("IsComplete:{0} IsCompletedSynchronously:{1}", result.IsCompleted, result.CompletedSynchronously)); } catch (Exception e) { this.log.Exception(7777, "", e); this.Disconnect(); this.ConnectionCompleted?.Invoke(this, false); } }
private async void OnRun(BTDeviceInfo info) { if (info != null) { // Seem to only be able to pass strings as params await Shell.Current.GoToAsync($"{nameof(BluetoothRunPage)}?BTDevice={JsonConvert.SerializeObject(info)}"); } }
private void deviceDiscovered(object sender, BTDeviceInfo device) { this.log.Info("deviceDiscovered", () => string.Format("Found {0}", device.Name)); this.Dispatcher.Invoke(() => { WrapErr.ToErrReport(9999, () => { this.lbBluetooth.Add(this.items, device); }); }); }
private void BTClassic_DeviceInfoGathered(object sender, BTDeviceInfo e) { ErrReport report; WrapErr.ToErrReport(out report, 20014, "Failure on BTClassic_DeviceInfoGathered", () => { this.BT_DeviceInfoGathered?.Invoke(this, e); }); this.RaiseIfException(report); }
private void deviceInfoGathered(object sender, BTDeviceInfo e) { this.Dispatcher.Invoke(() => { this.ui.IsBusy = false; DI.Wrapper.BT_DeviceInfoGathered -= deviceInfoGathered; DeviceInfo_BT win = new DeviceInfo_BT(this, e); win.ShowDialog(); }); }
private void DoDiscovery() { this.Title = "Bluetooth"; this.selectedDevice = BTSelect.ShowBox(this, true); if (this.selectedDevice != null) { this.Title = this.selectedDevice.Name; } }
private void BTClassic_DiscoveredDeviceHandler(object sender, BTDeviceInfo e) { ErrReport report; WrapErr.ToErrReport(out report, 20013, "Failure on BTClassic_DiscoveredDeviceHandler", () => { this.BT_DeviceDiscovered?.Invoke(this, e); }); this.RaiseIfException(report); }
public void BTClassicUnPairAsync(BTDeviceInfo device) { ErrReport report; WrapErr.ToErrReport(out report, 20006, "Failure on BTClassicUnPairAsync", () => { this.classicBluetooth.UnPairAsync(device); }); this.RaiseIfException(report); }
/// <summary>Discover devices</summary> /// <param name="paired">If discovery limited to paired or non paired devices</param> private async void DoDiscovery(bool paired) { try { DeviceInformationCollection devices = await DeviceInformation.FindAllAsync( BluetoothDevice.GetDeviceSelectorFromPairingState(paired)); foreach (DeviceInformation info in devices) { try { this.log.Info("DoDiscovery", () => string.Format("Found device {0}", info.Name)); BTDeviceInfo deviceInfo = new BTDeviceInfo() { Name = info.Name, Connected = false, Address = info.Id, }; using (BluetoothDevice device = await BluetoothDevice.FromIdAsync(info.Id)) { deviceInfo.Connected = device.ConnectionStatus == BluetoothConnectionStatus.Connected; deviceInfo.CanPair = this.GetBoolProperty(device.DeviceInformation.Properties, KEY_CAN_PAIR, false); deviceInfo.IsPaired = this.GetBoolProperty(device.DeviceInformation.Properties, KEY_IS_PAIRED, false); // Container Id also //device.DeviceAccessInformation.CurrentStatus == DeviceAccessStatus. // Allowed, denied by user, by system, unspecified //device.DeviceInformation.EnclosureLocation.; // Dock, lid, panel, etc //device.DeviceInformation.IsDefault; //device.DeviceInformation.IsEnabled; //device.DeviceInformation.Kind == //AssociationEndpoint, Device, etc //device.DeviceInformation.Properties if (device.ClassOfDevice != null) { deviceInfo.DeviceClassInt = (uint)device.ClassOfDevice.MajorClass; deviceInfo.DeviceClassName = string.Format("{0}:{1}", device.ClassOfDevice.MajorClass.ToString(), device.ClassOfDevice.MinorClass.ToString()); //device.ClassOfDevice.ServiceCapabilities == BluetoothServiceCapabilities.ObjectTransferService, etc } // Serial port service name // Bluetooth#Bluetooth10:08:b1:8a:b0:02-20:16:04:07:61:01#RFCOMM:00000000:{00001101-0000-1000-8000-00805f9b34fb} // TODO - determine if all the info in device is disposed by the device.Dispose } // end of using (device) this.DeviceDiscovered?.Invoke(this, deviceInfo); } catch (Exception ex2) { this.log.Exception(9999, "", ex2); } } this.DiscoveryComplete?.Invoke(this, true); } catch (Exception e) { this.log.Exception(9999, "", e); this.DiscoveryComplete?.Invoke(this, false); } }
private void buttonConnectEvent(object sender, EventArgs e) { BTDeviceInfo device = SelectBT.ShowBox(this); if (device != null) { DI.W.BTConnectAsync(device); } }
private void selectionChanged(object sender, SelectionChangedEventArgs e) { BTDeviceInfo info = this.lbBluetooth.SelectedItem as BTDeviceInfo; if (info != null) { this.SelectedBT = info; this.Close(); } }
/// <summary>Complete by connecting and filling in the device information</summary> /// <param name="device"></param> public void GetDeviceInfoAsync(BTDeviceInfo deviceDataModel) { Task.Run(async () => { try { await this.GetExtraInfo(deviceDataModel, false, true); } catch (Exception e) { this.log.Exception(9999, "", e); // TODO - raise error event } }); }
private void DeviceDiscoveredHandler(object sender, BTDeviceInfo e) { Device.BeginInvokeOnMainThread(() => { this.log.Info("DeviceDiscoveredHandler", () => string.Format("Adding device:{0}", e.Name)); this.lstDevices.ItemSelected -= LstDevices_ItemSelectedHandler; this.activity.IsRunning = false; this.lstDevices.ItemsSource = null; this.devices.Add(e); this.lstDevices.ItemsSource = this.devices; this.lstDevices.ItemSelected += LstDevices_ItemSelectedHandler; }); }
private void BT_RemoveEntry(string name) { // TODO - just using the same list box. Will need more logic BTDeviceInfo item = this.infoList_BT.FirstOrDefault(x => x.Name == name); if (item != null) { this.listBox_BT.ItemsSource = null; this.infoList_BT.Remove(item); this.listBox_BT.ItemsSource = this.infoList_BT; } }
public void PairgAsync(BTDeviceInfo info) { Task.Run(async() => { try { await this.DoPairing(info); } catch (Exception e) { this.log.Exception(9999, "PairgAsync", "", e); } }); return; }
public DeviceInfo_BT(Window parent, BTDeviceInfo info) { this.parent = parent; this.info = info; InitializeComponent(); this.Init(); // Call before rendering which will trigger initial resize events this.widthManager = new ButtonGroupSizeSyncManager(this.btnExit, this.btnProperties); this.widthManager.PrepForChange(); this.SizeToContent = SizeToContent.WidthAndHeight; }
private void btnSelect_Clicked(object sender, EventArgs e) { BTDeviceInfo info = (this.lstDevices.SelectedItem as BTDeviceInfo); if (info != null) { this.viewModel.RunCommand.Execute(this.lstDevices.SelectedItem as BTDeviceInfo); } else { App.ShowError(this, App.GetText(MsgCode.NothingSelected)); } }
private void btnPair_Clicked(object sender, EventArgs e) { BTDeviceInfo info = this.lstDevices.SelectedItem as BTDeviceInfo; if (info != null) { App.Wrapper.BTClassicPairAsync(info); } else { this.OnErr(MsgCode.NothingSelected); } }
void SdkListener.BTConnected(BTDeviceInfo iDeviceInfo) { Debug.Write("MainController.BTConnected"); this.Dispatcher.Invoke(() => { if (!object.ReferenceEquals(this.listWindow, null)) { this.listWindow.Hide(); this.listWindow.Close(); } }); }
public void BTClassicConnectAsync(BTDeviceInfo device) { this.log.InfoEntry("BTClassicConnectAsync"); ErrReport report; WrapErr.ToErrReport(out report, 20003, "Failure on BTClassicConnectAsync", () => { this.classicBluetooth.ConnectAsync(device); }); if (report.Code != 0) { WrapErr.SafeAction(() => BT_DiscoveryComplete?.Invoke(this, false)); } }
private BTRadioInfo BuildRadioDataModel(BluetoothDeviceInfo device) { try { this.log.InfoEntry("BuildRadioDataModel"); BTDeviceInfo info = new BTDeviceInfo() { Name = device.DeviceName, Address = device.DeviceAddress.ToString(), }; // Need to connect to each device to get radio info RadioVersions devRadio = null; if (this.Connect(info)) { devRadio = device.GetVersions(); } else { this.log.Error(9991, "NOT CONNECTED TO READ RADIO INFO"); } if (devRadio == null) { this.log.Info("", "NULL Radio info"); return(new BTRadioInfo()); } string tmp = devRadio.LmpSupportedFeatures.ToString(); string[] pieces = tmp.Split(','); List <string> features = new List <string>(pieces.Length); for (int i = 0; i < pieces.Length; i++) { features.Add(pieces[i].Replace("_", "").CamelCaseToSpaces()); } return(new BTRadioInfo() { Manufacturer = devRadio.Manufacturer.ToString().CamelCaseToSpaces(), LinkManagerProtocol = string.Format("{0} ({1})", devRadio.LmpVersion.LmpVerToString(), devRadio.LmpSubversion), Features = features, }); } catch (Exception e) { this.log.Exception(9999, "", e); return(new BTRadioInfo()); } finally { // TODO - move this to the getInfo function as it takes a 1/2 second per device disconnect this.Disconnect(); } }
private void connectionCompleted(object sender, bool ok) { this.Dispatcher.Invoke(() => { this.ui.IsBusy = false; if (ok) { this.ui.SetConnected(); } else { this.selectedDevice = null; App.ShowMsg(DI.Wrapper.GetText(MsgCode.ConnectionFailure)); } }); }
private void btnUnPair_Clicked(object sender, EventArgs e) { BTDeviceInfo device = this.lstDevices.SelectedItem as BTDeviceInfo; if (device != null) { App.ShowYesNo(this, device.Name, MsgCode.Unpair, () => { App.Wrapper.BTClassicUnPairAsync(device); }); } else { this.OnErr(MsgCode.NothingSelected); } }
// TODO - ADD THIS TO INTERFACE AND MAKE IT ASYNC LIKE THE BLE IMPLEMENTATION /// <summary>Complete by connecting and filling in the device information</summary> /// <param name="device"></param> public void GetDeviceInfo(BTDeviceInfo deviceDataModel) { #if USE_BT_WRAPPER //this.btWrapper.DiscoverPairedDevicesAsync(); #else Task.Run(async() => { try { await this.HarvestInfo(deviceDataModel); } catch (Exception e) { this.log.Exception(9999, "", e); } }); #endif }
// TODO - change names. Properties for both BT and BLE public List <NetPropertyDataModelDisplay> BT_GetProperties(BTDeviceInfo info) { try { List <NetPropertyDataModelDisplay> list = new List <NetPropertyDataModelDisplay>(); foreach (var sp in info.Properties) { list.Add(new NetPropertyDataModelDisplay(sp.Value, this.Translate)); } return(list); } catch (Exception e) { this.log.Exception(9999, "", e); return(new List <NetPropertyDataModelDisplay>()); } }