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; } }); }
private void BleDeviceInfoAssembledHandler(object sender, BLEGetInfoStatus info) { ErrReport report; WrapErr.ToErrReport(out report, 200056, "Failure on BLEGetInfoStatus", () => { info.Message = this.Translate(info.Status); this.BLE_DeviceInfoGathered?.Invoke(this, info); }); this.RaiseIfException(report); }
private void RaiseConnectAttemptResult(BLEGetInfoStatus status) { Task.Run(() => { try { this.DeviceConnectResult?.Invoke(this, status); } catch (Exception e) { this.log.Exception(9999, "RaiseConnectAttemptResult", "", e); } }); }
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); } }
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 HarvestDeviceInfo(BluetoothLEDeviceInfo deviceDataModel) { this.log.Info("HarvestDeviceInfo", () => string.Format("Harvesting from {0}: FromIdAsync({1})", deviceDataModel.Name, deviceDataModel.Id)); try { BLEGetInfoStatus result = await this.GetBLEDeviceInfo(deviceDataModel); this.DeviceInfoAssembled?.Invoke(this, result); } catch (Exception e) { this.log.Exception(9999, "On raise event", e); try { this.DeviceInfoAssembled?.Invoke(this, new BLEGetInfoStatus(deviceDataModel, BLEOperationStatus.UnknownError)); } catch (Exception e2) { this.log.Exception(9999, "On raise event unknown error", e2); } } #region Factored out into common GetBLEDeviceInfo //BluetoothLEDevice device = null; //BLEGetInfoStatus result = new BLEGetInfoStatus() { // DeviceInfo = deviceDataModel, //}; //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)); // device = await BluetoothLEDevice.FromIdAsync(deviceDataModel.Id); // deviceDataModel.InfoAttempted = true; // this.UpdateDeviceOnConnect(device, deviceDataModel); // // Clear services and get a new set for the passed in device info // deviceDataModel.Services.Clear(); // try { // // Attempts pair and causes catastropic failure if not supported // // This will happen with Arduinos // GattDeviceServicesResult services = await device.GetGattServicesAsync(); // if (services.Status == GattCommunicationStatus.Success) { // if (services.Services != null) { // if (services.Services.Count > 0) { // foreach (GattDeviceService service in services.Services) { // await this.BuildServiceDataModel(service, deviceDataModel); // } // result.Status = BLEOperationStatus.Success; // } // else { // result.Status = BLEOperationStatus.NoServices; // this.log.Info("HarvestDeviceInfo", "No services exposed"); // } // } // else { // result.Status = BLEOperationStatus.GetServicesFailed; // this.log.Error(9999, "Null services"); // } // } // else { // result.Status = BLEOperationStatus.GetServicesFailed; // this.log.Error(9999, "HarvestDeviceInfo", () => string.Format(" Get Services Failed {0}", services.Status.ToString())); // } // } // catch(Exception e) { // result.Status = BLEOperationStatus.GetServicesFailed; // this.log.Exception(9999, "HarvestDeviceInfo", "Failure", e); // } //} //catch (Exception e) { // this.log.Exception(9999, "On harvest device info", e); // // TODO - raise event with null device // result.Status = BLEOperationStatus.UnhandledError; //} //finally { // try { // if (device != null) { // device.Dispose(); // device = null; // } // } // catch (Exception ex) { // this.log.Exception(9999, "On fail to disconnect harvesting device data", ex); // } //} //try { // // Raise event // this.DeviceInfoAssembled?.Invoke(this, result); //} //catch(Exception e) { // this.log.Exception(9999, "On raise event", e); //} #endregion }
/// <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, BLEGetInfoStatus status) { try { 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 List <BLE_CharacteristicDataModel>(), AttributeHandle = service.AttributeHandle, DeviceId = status.DeviceInfo.Id, ServiceTypeEnum = BLE_ParseHelpers.GetServiceTypeEnum(service.Uuid), 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.currentServices.Add(service); 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) { try { await this.BuildCharacteristicDataModel(ch, serviceDataModel); } catch (Exception e1) { this.log.Exception(9999, "HarvestDeviceInfo", e1); } } } 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 status.DeviceInfo.Services.Add(serviceDataModel); } catch (Exception e) { this.log.Exception(9999, "BuildServiceDataModel", "", e); } }