private void SetConnectState(bool isConnected) { this.Dispatcher.Invoke(() => { this.writeControl.Connected = isConnected; if (isConnected) { this.connectedOff.Collapse(); this.connectedOn.Show(); this.btnConnect.Collapse(); this.btnDisconnect.Show(); DI.Wrapper.BLE_ConnectionStatusChanged += this.connectionStatusChanged; } else { this.connectedOn.Collapse(); this.connectedOff.Show(); this.btnDisconnect.Collapse(); this.btnConnect.Show(); this.treeServices.ItemsSource = null; this.currentDevice = null; this.currentCharacteristic = null; this.writeControl.Reset(); this.ucCmds.Reset(); DI.Wrapper.BLE_ConnectionStatusChanged -= this.connectionStatusChanged; DI.Wrapper.BLE_Disconnect(); // Reset title //this.lblCmdDataTypeContent.Content = BLE_DataType.Reserved.ToStr(); } this.ResizeOnNormal(); }); }
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)); } }
private void DeviceConnectResultHandler(object sender, BLEGetInfoStatus info) { this.Dispatcher.Invoke(() => { this.IsBusy = false; DI.Wrapper.BLE_DeviceConnectResult -= this.DeviceConnectResultHandler; this.ResizeOnNormal(); switch (info.Status) { case BLEOperationStatus.Failed: case BLEOperationStatus.UnhandledError: case BLEOperationStatus.UnknownError: case BLEOperationStatus.NotFound: case BLEOperationStatus.NoServices: case BLEOperationStatus.GetServicesFailed: App.ShowMsg(info.Message); break; case BLEOperationStatus.Success: this.SetConnectState(true); this.currentDevice = info.DeviceInfo; this.Title = string.Format("(BLE) {0}", this.currentDevice.Name); this.treeServices.ItemsSource = this.currentDevice.Services; break; } }); }
public BLE_ServicesDisplay(Window parent, BluetoothLEDeviceInfo info) { this.parent = parent; InitializeComponent(); this.SizeToContent = SizeToContent.WidthAndHeight; this.treeServices.ItemsSource = info.Services; }
public List <KeyValuePropertyDisplay> BLE_GetDeviceInfoForDisplay(BluetoothLEDeviceInfo info) { try { // TODO - language List <KeyValuePropertyDisplay> list = new List <KeyValuePropertyDisplay>(); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.Name), info.Name)); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.Id), info.Id)); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.AccessStatus), info.AccessStatus.ToString().CamelCaseToSpaces())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.Address), info.AddressAsULong.ToString())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.AddressType), info.AddressType.ToString().CamelCaseToSpaces())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.Default), info.IsDefault.ToString())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.Enabled), info.IsEnabled.ToString())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.Kind), info.DeviceKind.ToString().UnderlineToSpaces())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.PairingAllowed), info.CanPair.ToString())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.Paired), info.IsPaired.ToString())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.PairedWithSecureConnection), info.WasPairedUsingSecureConnection.ToString())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.Connected), info.IsConnected.ToString())); list.Add(new KeyValuePropertyDisplay(this.GetText(MsgCode.ProtectionLevel), info.ProtectionLevel.ToString().CamelCaseToSpaces())); list.Add(new KeyValuePropertyDisplay(string.Format("{0} (Bluetooth)", this.GetText(MsgCode.Kind)), info.TypeBluetooth.ToString().CamelCaseToSpaces())); list.Add(new KeyValuePropertyDisplay("Enclosure(Dock)", info.EnclosureLocation.InDock.ToString())); list.Add(new KeyValuePropertyDisplay("Enclosure(Lid)", info.EnclosureLocation.InLid.ToString())); list.Add(new KeyValuePropertyDisplay("Enclosure(Clockwise Rotation)", info.EnclosureLocation.ClockWiseRotationInDegrees.ToString())); list.Add(new KeyValuePropertyDisplay("Enclosure(Panel)", info.EnclosureLocation.Location.ToString())); return(list); } catch (Exception e) { this.log.Exception(9999, "", e); return(new List <KeyValuePropertyDisplay>()); } }
/// <summary>Fired when a device is discovered</summary> /// <param name="sender"></param> /// <param name="deviceInfo">Info on the discovered device</param> private void DevWatcher_Added(DeviceWatcher sender, DeviceInformation deviceInfo) { if (deviceInfo.Name.Length > 0) { WrapErr.ToErrReport(9999, () => { // TODO - find out what comes in with no name this.log.Info("DevWatcher_Added", () => string.Format("+++++ {0} : {1}", deviceInfo.Name, deviceInfo.Id)); this.DebugDumpDeviceInfo(deviceInfo); if (this.DeviceDiscovered != null) { BluetoothLEDeviceInfo dev = new BluetoothLEDeviceInfo(new BLE_WinPropertyKeys()) { Name = deviceInfo.Name, Id = deviceInfo.Id, IsDefault = deviceInfo.IsDefault, CanPair = deviceInfo?.Pairing.CanPair ?? false, IsPaired = deviceInfo?.Pairing.IsPaired ?? false, IsConnectable = deviceInfo.IsConnectable(), IsConnected = deviceInfo.IsConnected(), ServiceProperties = deviceInfo.CreatePropertiesDictionary(), // This would be the DeviceInformation object. Not required // but handy for updates in Windows OSSpecificObj = deviceInfo, }; this.DeviceDiscovered(sender, dev); } }); } }
public DeviceInfo_BLE(Window parent, BluetoothLEDeviceInfo info) { this.parent = parent; this.info = info; InitializeComponent(); this.PopulateFields(); this.SizeToContent = SizeToContent.WidthAndHeight; }
public void BLE_GetInfo(BluetoothLEDeviceInfo device) { ErrReport report; WrapErr.ToErrReport(out report, 200059, "Failure on BluetoothLEDeviceInfo", () => { this.bleBluetooth.GetInfo(device); }); }
public void BLE_ConnectAsync(BluetoothLEDeviceInfo device) { ErrReport report; WrapErr.ToErrReport(out report, 200058, "Failure on BLE_ConnectAsync", () => { this.bleBluetooth.Connect(device); }); }
private void Wrapper_BLE_DeviceInfoGatheredOnGetInfo(object sender, BluetoothLEDeviceInfo info) { this.Dispatcher.Invoke(() => { this.wrapper.BLE_DeviceInfoGathered -= this.Wrapper_BLE_DeviceInfoGatheredOnGetInfo; this.gridWait.Collapse(); DeviceInfo_BLE.ShowBox(this, info); }); }
public void GetInfo(BluetoothLEDeviceInfo deviceInfo) { this.DeviceInfoAssembled?.Invoke( this, new BLEGetInfoStatus(new BluetoothLEDeviceInfo() { Name = "NOT IMPLEMENTED" }, BLEOperationStatus.NotFound)); }
private void BLE_DeviceDiscoveredHandler(object sender, BluetoothLEDeviceInfo e) { ErrReport report; WrapErr.ToErrReport(out report, 200050, "Failure on BLE_DeviceDiscoveredHandler", () => { this.BLE_DeviceDiscovered?.Invoke(this, e); }); this.RaiseIfException(report); }
public void GetInfo(BluetoothLEDeviceInfo deviceDataModel) { Task.Run(async() => { try { await this.HarvestDeviceInfo(deviceDataModel); } catch (Exception e) { this.log.Exception(9999, "On Task Run GetInfo", e); } }); }
/// <summary>Build the GATT service data model</summary> /// <param name="service">The OS GATT service object</param> /// <param name="deviceDataModel">The portable GATT session data model</param> /// <returns>The async task</returns> public async Task BuildServiceDataModel(GattDeviceService service, BluetoothLEDeviceInfo deviceDataModel) { this.log.Info("BuildServiceDataModel", () => string.Format("Gatt Service:{0} Uid:{1}", BLE_DisplayHelpers.GetServiceName(service), service.Uuid.ToString())); // New service data model to add to device info BLE_ServiceDataModel serviceDataModel = new BLE_ServiceDataModel() { Characteristics = new Dictionary <string, BLE_CharacteristicDataModel>(), AttributeHandle = service.AttributeHandle, DeviceId = deviceDataModel.Id, DisplayName = BLE_DisplayHelpers.GetServiceName(service), Uuid = service.Uuid, SharingMode = (BLE_SharingMode)service.SharingMode, }; if (service.DeviceAccessInformation != null) { serviceDataModel.DeviceAccess = (BLE_DeviceAccessStatus)service.DeviceAccessInformation.CurrentStatus; } this.BuildSessionDataModel(service.Session, serviceDataModel.Session); // TODO //service.ParentServices // Get the characteristics for the service GattCharacteristicsResult characteristics = await service.GetCharacteristicsAsync(); if (characteristics.Status == GattCommunicationStatus.Success) { if (characteristics.Characteristics != null) { if (characteristics.Characteristics.Count > 0) { foreach (GattCharacteristic ch in characteristics.Characteristics) { await this.BuildCharacteristicDataModel(ch, serviceDataModel); } } else { this.log.Info("ConnectToDevice", () => string.Format("No characteristics")); } } } else { this.log.Error(9999, "HarvestDeviceInfo", () => string.Format("Failed to get Characteristics result {0}", characteristics.Status.ToString())); } // Add the service data model to the device info data model deviceDataModel.Services.Add(serviceDataModel.Uuid.ToString(), serviceDataModel); }
public void GetInfo(BluetoothLEDeviceInfo deviceDataModel) { Task.Run(async() => { try { // Not using the get info anymore so make sure the device is nulled this.Disconnect(); await this.HarvestDeviceInfo(deviceDataModel); } catch (Exception e) { this.log.Exception(9999, "On Task Run GetInfo", e); } }); }
public DeviceInfo_BLESerial(Window parent, BluetoothLEDeviceInfo info) { this.parent = parent; InitializeComponent(); this.SizeToContent = SizeToContent.WidthAndHeight; this.borderInput.MouseLeftButtonDown += this.BorderInput_MouseLeftButtonDown; this.borderOutput.MouseLeftButtonDown += this.BorderOutput_MouseLeftButtonDown; this.widthManager = new ButtonGroupSizeSyncManager(this.btnExit, this.btnSave); this.widthManager.PrepForChange(); this.info = info; // TODO - check if any previous selections this.treeServices.ItemsSource = this.info.Services; }
public void Connect(BluetoothLEDeviceInfo deviceInfo) { // TODO - need to have a copy of the BluetoothLEDeviceInfo saved also which subscribes to the BLE OS Device // info and passes those events up to the UI this.Disconnect(); Task.Run(async() => { try { await this.ConnectToDevice(deviceInfo); } catch (Exception e) { this.log.Exception(9999, "On Task Run ConnectToDevice", e); } }); }
private void selectionChanged(object sender, SelectionChangedEventArgs e) { BluetoothLEDeviceInfo device = this.listBox_BLE.SelectedItem as BluetoothLEDeviceInfo; lock (this.listBox_BLE) { if (device != null) { this.SelectedBLE = device; this.DisconnectEvents(); DI.Wrapper.BLE_CancelDiscover(); this.Close(); } } }
/// <summary>Update any previous information from discovery or other connections</summary> /// <param name="device">The BLE device returned</param> /// <param name="deviceDataModel">The portable data model to receive values</param> public void UpdateDeviceOnConnect(BluetoothLEDevice device, BluetoothLEDeviceInfo deviceDataModel) { if (device.DeviceInformation != null) { DeviceInformation di = device.DeviceInformation; deviceDataModel.IsConnectable = di.IsConnectable(); deviceDataModel.IsDefault = di.IsDefault; deviceDataModel.IsEnabled = di.IsEnabled; deviceDataModel.Kind = (BLE_DeviceInfoKind)di.Kind; if (di.Pairing != null) { deviceDataModel.CanPair = di.Pairing.CanPair; deviceDataModel.IsPaired = di.Pairing.IsPaired; deviceDataModel.ProtectionLevel = (BLE_ProtectionLevel)di.Pairing.ProtectionLevel; } if (di.EnclosureLocation != null) { deviceDataModel.EnclosureLocation.InDock = di.EnclosureLocation.InDock; deviceDataModel.EnclosureLocation.InLid = di.EnclosureLocation.InLid; deviceDataModel.EnclosureLocation.Location = (BLE_DevicePanelLocation)di.EnclosureLocation.Panel; deviceDataModel.EnclosureLocation.ClockWiseRotationInDegrees = di.EnclosureLocation.RotationAngleInDegreesClockwise; } } if (device.BluetoothDeviceId != null) { if (device.BluetoothDeviceId.IsClassicDevice) { deviceDataModel.TypeBluetooth = BluetoothType.Classic; } else if (device.BluetoothDeviceId.IsLowEnergyDevice) { deviceDataModel.TypeBluetooth = BluetoothType.LowEnergy; } } if (device.DeviceAccessInformation != null) { deviceDataModel.AccessStatus = (BLE_DeviceAccessStatus)device.DeviceAccessInformation.CurrentStatus; } deviceDataModel.WasPairedUsingSecureConnection = device.WasSecureConnectionUsedForPairing; deviceDataModel.AddressType = (BLE_AddressType)device.BluetoothAddressType; deviceDataModel.IsConnected = device.ConnectionStatus == BluetoothConnectionStatus.Connected; deviceDataModel.AddressAsULong = device.BluetoothAddress; // TODO //device.Appearance }
private async Task ConnectToDeviceAsync(BluetoothLEDeviceInfo deviceInfo) { try { this.log.Info("ConnectToDeviceAsync", () => string.Format("{0} ID:{1}", deviceInfo.Name, deviceInfo.Id)); BLEGetInfoStatus result = await this.GetBLEDeviceInfo(deviceInfo); this.ConnectBTLEDeviceEvents(result.Status); this.RaiseConnectAttemptResult(result); } catch (Exception e) { this.log.Exception(9999, "BLE Connect Exception", e); this.RaiseConnectAttemptResult(BLEOperationStatus.UnknownError); } }
public List <NetPropertyDataModelDisplay> BLE_GetServiceProperties(BluetoothLEDeviceInfo info) { try { List <NetPropertyDataModelDisplay> list = new List <NetPropertyDataModelDisplay>(); foreach (var sp in info.ServiceProperties) { list.Add(new NetPropertyDataModelDisplay(sp.Value)); } return(list); } catch (Exception e) { this.log.Exception(9999, "", e); return(new List <NetPropertyDataModelDisplay>()); } }
public void Translate(BluetoothLEDeviceInfo device) { try { if (device != null) { foreach (var s in device.Services) { this.Translate(s); } } } catch (Exception e) { this.log.Exception(9000, "Translate(device)", "", e); } }
/// <summary>Event handler for Bluetooth LE device discovery. Adds one at a time</summary> /// <param name="sender">The sender of event</param> /// <param name="info">The information for discovered device</param> private void BLE_DeviceDiscoveredHandler(object sender, BluetoothLEDeviceInfo info) { this.Dispatcher.Invoke(() => { WrapErr.ToErrReport(9999, "Failure on BLE Device Discovered", () => { lock (this.listBox_BLE) { this.log.Info("BLE_DeviceDiscoveredHandler", () => string.Format("Adding '{0}' '{1}'", info.Name, info.Id)); this.RemoveIfFound(info.Id, false, true); this.log.Info("BLE_DeviceDiscoveredHandler", () => string.Format("Adding DONE")); this.listBox_BLE.Add(this.infoList_BLE, info); //this.btnLEConnect.Show(); this.btnInfoLE.Show(); //this.btnConfigureBLE.Show(); } }); }); }
private void deviceDiscovered(object sender, BluetoothLEDeviceInfo device) { Log.Info("BLESelect", "deviceDiscovered", () => string.Format("Found {0}", device.Name)); this.Dispatcher.Invoke(() => { WrapErr.ToErrReport(9999, "Failure on BLE Device Discovered", () => { lock (this.listBox_BLE) { this.listBox_BLE.SelectionChanged -= this.selectionChanged; this.log.Info("deviceDiscovered", () => string.Format("Adding '{0}' '{1}'", device.Name, device.Id)); this.RemoveIfFound(device.Id); this.log.Info("BLE_DeviceDiscoveredHandler", () => string.Format("Adding DONE")); this.listBox_BLE.Add(this.devices, device); this.listBox_BLE.SelectionChanged += this.selectionChanged; } }); }); }
private void BLE_DeviceUpdatedHandler(object sender, NetPropertiesUpdateDataModel args) { this.Dispatcher.Invoke(() => { WrapErr.ToErrReport(9999, "Failure on BLE Device Updated", () => { this.log.Info("", () => string.Format("Updating '{0}'", args.Id)); // Disconnect the list from control before changing. Maybe change to Observable collection this.listBox_BLE.ItemsSource = null; BluetoothLEDeviceInfo item = this.infoList_BLE.Find((x) => x.Id == args.Id); if (item != null) { // This will raise events on change. How to deal with that. Think only if it is the current on display item.Update(args.ServiceProperties); } this.listBox_BLE.ItemsSource = this.infoList_BLE; }); }); }
private void Wrapper_BLE_DeviceInfoGatheredForConfig(object sender, BluetoothLEDeviceInfo info) { this.Dispatcher.Invoke(() => { WrapErr.ToErrReport(9999, "Failure on BLE Device info gathered Complete", () => { this.gridWait.Collapse(); this.wrapper.BLE_DeviceInfoGathered -= this.Wrapper_BLE_DeviceInfoGatheredForConfig; if (info != null) { DeviceInfo_BLESerial bleInfo = new DeviceInfo_BLESerial(this, info); bleInfo.ShowDialog(); } else { MsgBoxSimple.ShowBox(DI.Wrapper.GetText(MsgCode.Error), DI.Wrapper.GetText(MsgCode.LoadFailed)); } }); }); }
private async Task <BLEGetInfoStatus> GetBLEDeviceInfo(BluetoothLEDeviceInfo deviceDataModel) { this.log.InfoEntry("GetBLEDeviceInfo"); BLEGetInfoStatus result = await this.GetDevice(deviceDataModel); if (result.Status != BLEOperationStatus.Success) { return(result); } try { deviceDataModel.Services.Clear(); GattDeviceServicesResult services = await this.currentDevice.GetGattServicesAsync(BluetoothCacheMode.Cached); if (services.Status != GattCommunicationStatus.Success) { return(this.BuildConnectFailure(BLEOperationStatus.GetServicesFailed, services.Status.ToString())); } if (services.Services == null) { return(this.BuildConnectFailure(BLEOperationStatus.GetServicesFailed, "Null Services")); } if (services.Services.Count == 0) { return(this.BuildConnectFailure(BLEOperationStatus.NoServices, "No services exposed")); } result.Status = BLEOperationStatus.Success; result.DeviceInfo = deviceDataModel; foreach (GattDeviceService service in services.Services) { // TODO make sure status is set in functions await this.BuildServiceDataModel(service, result); } return(result); } catch (Exception e) { this.log.Exception(9999, "HarvestDeviceInfo", "Failure", e); return(this.BuildConnectFailure(BLEOperationStatus.GetServicesFailed, "Exception on getting services")); } }
private async Task <BLEGetInfoStatus> GetDevice(BluetoothLEDeviceInfo deviceDataModel) { this.log.Info("GetBLEDevice", () => string.Format("Attempting to get device for {0}: FromIdAsync({1})", deviceDataModel.Name, deviceDataModel.Id)); try { // https://github.com/microsoft/Windows-universal-samples/blob/master/Samples/BluetoothLE/cs/Scenario2_Client.xaml.cs this.log.Info("HarvestDeviceInfo", () => string.Format("--------------------------------------------------------------------")); this.log.Info("HarvestDeviceInfo", () => string.Format(" Param Device Info ID {0}", deviceDataModel.Id)); this.currentDevice = await BluetoothLEDevice.FromIdAsync(deviceDataModel.Id); deviceDataModel.InfoAttempted = true; this.UpdateDeviceInfo(this.currentDevice, deviceDataModel); return(new BLEGetInfoStatus(BLEOperationStatus.Success)); } catch (Exception e) { this.log.Exception(9999, "On harvest device info", e); return(this.BuildConnectFailure(BLEOperationStatus.UnhandledError, "Exception connecting to device")); } }
private void btnInfoLE_Click(object sender, RoutedEventArgs e) { if (this.listBox_BLE.SelectedItem != null) { BluetoothLEDeviceInfo ble = this.listBox_BLE.SelectedItem as BluetoothLEDeviceInfo; if (ble.Services.Count == 0 && !ble.InfoAttempted) { this.wrapper.BLE_DeviceInfoGathered -= Wrapper_BLE_DeviceInfoGatheredOnGetInfo; this.wrapper.BLE_DeviceInfoGathered += Wrapper_BLE_DeviceInfoGatheredOnGetInfo; this.gridWait.Show(); this.wrapper.BLE_GetInfo(ble); } else { DeviceInfo_BLE.ShowBox(this, ble); } //App.ShowMsg("BPIPO"); //this.wrapper.BLE_GetDbgInfoStringDump(this.listBox_BLE.SelectedItem, App.ShowMsgTitle); } }
public static void ShowBox(Window parent, BluetoothLEDeviceInfo info) { DeviceInfo_BLE win = new DeviceInfo_BLE(parent, info); win.ShowDialog(); }