private async void HandleSelectedDevice(DeviceListItemViewModel device) { if (await ConnectDeviceAsync(device)) { openDevice(device.Device.Id); } }
private async Task <bool> ConnectDeviceAsync(DeviceListItemViewModel device, bool showPrompt = true) { if (device.IsConnected) { return(true); } if (showPrompt && !await _userDialogs.ConfirmAsync($"Connect to device '{device.Name}'?")) { return(false); } try { _userDialogs.ShowLoading("Connecting ..."); await Adapter.ConnectToDeviceAync(device.Device); _userDialogs.InfoToast($"Connected to {device.Device.Name}."); PreviousGuid = device.Device.Id; return(true); } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Connection error"); Mvx.Trace(ex.Message); return(false); } finally { _userDialogs.HideLoading(); device.Update(); } }
private async void DisconnectDevice(DeviceListItemViewModel device) { if (BleMvxApplication._reader.Status != CSLibrary.HighLevelInterface.READERSTATE.DISCONNECT) { BleMvxApplication._reader.DisconnectAsync(); } try { if (!device.IsConnected) { return; } _userDialogs.ShowLoading($"Disconnecting {device.Name}..."); await Adapter.DisconnectDeviceAsync(device.Device); } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Disconnect error"); } finally { device.Update(); _userDialogs.HideLoading(); } }
private async Task <bool> ConnectDeviceAsync(DeviceListItemViewModel device, bool showPrompt = true) { if (showPrompt && !await _userDialogs.ConfirmAsync($"Connect to device '{device.Name}'?")) { return(false); } try { CancellationTokenSource tokenSource = new CancellationTokenSource(); ConnectParameters connectParameters = new ConnectParameters(); await Adapter.ConnectToDeviceAsync(device.Device, connectParameters, tokenSource.Token); _userDialogs.ShowSuccess($"Initializing Reader, Please Wait.", 8000); PreviousGuid = device.Device.Id; return(true); } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Connection error"); Mvx.Trace(ex.Message); return(false); } finally { //_userDialogs.HideLoading(); device.Update(); } }
private async void HandleSelectedDevice(DeviceListItemViewModel devices) { try { if (await ConnectDeviceAsync(devices)) { // Connect to CS108 //var Services = Adapter.ConnectedDevices.FirstOrDefault(d => d.Id.ToString().Equals(device.Device.Id.ToString())); var device = Adapter.ConnectedDevices.FirstOrDefault(d => d.Id.Equals(devices.Device.Id)); if (device == null) { return; } Connect(device); Close(this); } } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Disconnect error"); } }
private async void ShowServices(DeviceListItemViewModel device) { if (await ConnectDeviceAsync(device, false)) { ShowViewModel <ServiceListViewModel>(new MvxBundle(new Dictionary <string, string> { { DeviceIdKey, device.Device.Id.ToString() } })); } /* * try * { * using (item.Device) * { * _userDialogs.ShowLoading($"Connecting to {item.Name} ..."); * await Adapter.ConnectToDeviceAsync(item.Device); * item.Update(); * _userDialogs.ShowSuccess($"Connected {item.Device.Name}"); * * _userDialogs.HideLoading(); * * } * } * catch (Exception ex) * { * _userDialogs.Alert(ex.Message, "Failed to connect."); * } * finally * { * _userDialogs.HideLoading(); * device.Update(); * } * */ }
private async void ConnectAndDisposeDevice(DeviceListItemViewModel item) { try { using (item.Device) { //_userDialogs.ShowLoading($"Connecting to {item.Name} ..."); await Adapter.ConnectToDeviceAsync(item.Device); item.Update(); //_userDialogs.ShowSuccess($"Connected {item.Device.Name}"); //_userDialogs.HideLoading(); //for (var i = 5; i >= 1; i--) //{ // _userDialogs.ShowLoading($"Disconnect in {i}s..."); // await Task.Delay(1000); // _userDialogs.HideLoading(); //} } } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Failed to connect and dispose."); } finally { _userDialogs.HideLoading(); } }
private async void HandleSelectedDevice(DeviceListItemViewModel device) { if (await ConnectDeviceAsync(device)) { Close(this); MessagingCenter.Send <BaseViewModel>(this, "Reload"); } }
private async void HandleSelectedDevice(DeviceListItemViewModel device) { if (await ConnectDeviceAsync(device)) { ShowViewModel <ServiceListViewModel>(new MvxBundle(new Dictionary <string, string> { { DeviceIdKey, device.Device.Id.ToString() } })); } }
private void HandleSelectedDevice(DeviceListItemViewModel device) { var config = new ActionSheetConfig(); if (device.IsConnected) { config.Add("Details", () => { ShowViewModel <ServiceListViewModel>(new MvxBundle(new Dictionary <string, string> { { DeviceIdKey, device.Device.Id.ToString() } })); }); config.Add("Update RSSI", async() => { try { _userDialogs.ShowLoading(); await device.Device.UpdateRssiAsync(); device.RaisePropertyChanged(nameof(device.Rssi)); _userDialogs.HideLoading(); _userDialogs.Toast($"RSSI updated {device.Rssi}", TimeSpan.FromSeconds(1000)); } catch (Exception ex) { _userDialogs.HideLoading(); await _userDialogs.AlertAsync($"Failed to update rssi. Exception: {ex.Message}"); } }); config.Destructive = new ActionSheetOption("Disconnect", () => DisconnectCommand.Execute(device)); } else { config.Add("Connect", async() => { if (await ConnectDeviceAsync(device)) { var navigation = Mvx.Resolve <IMvxNavigationService>(); await navigation.Navigate <ServiceListViewModel, MvxBundle>(new MvxBundle(new Dictionary <string, string> { { DeviceIdKey, device.Device.Id.ToString() } })); } }); config.Add("Connect & Dispose", () => ConnectDisposeCommand.Execute(device)); } config.Add("Copy GUID", () => CopyGuidCommand.Execute(device)); config.Cancel = new ActionSheetOption("Cancel"); config.SetTitle("Device Options"); _userDialogs.ActionSheet(config); }
private async Task <bool> ConnectDeviceAsync(DeviceListItemViewModel device, bool showPrompt = true) { //if (device.IsConnected) //{ // return true; //} if (showPrompt && !await _userDialogs.ConfirmAsync($"Connect to device '{device.Name}'?")) { return(false); } try { CancellationTokenSource tokenSource = new CancellationTokenSource(); var config = new ProgressDialogConfig() { Title = $"Connecting to '{device.Id}'", CancelText = "Cancel", IsDeterministic = false, OnCancel = tokenSource.Cancel }; using (var progress = _userDialogs.Progress(config)) { progress.Show(); await Adapter.ConnectToDeviceAsync(device.Device, new ConnectParameters(forceBleTransport : false), tokenSource.Token); } _userDialogs.ShowSuccess($"Connected to {device.Device.Name}."); PreviousGuid = device.Device.Id; return(true); } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Connection error"); Mvx.Trace(ex.Message); return(false); } finally { _userDialogs.HideLoading(); device.Update(); } }
private async void ConnectToPreviousDeviceAsync() { IDevice device; try { CancellationTokenSource tokenSource = new CancellationTokenSource(); ConnectParameters connectParameters = new ConnectParameters(); var config = new ProgressDialogConfig() { Title = $"Searching for '{PreviousGuid}'", CancelText = "Cancel", IsDeterministic = false, OnCancel = tokenSource.Cancel }; using (var progress = _userDialogs.Progress(config)) { progress.Show(); device = await Adapter.ConnectToKnownDeviceAsync(PreviousGuid, connectParameters, tokenSource.Token); } //_userDialogs.ShowSuccess($"Connected to {device.Name}."); var deviceItem = Devices.FirstOrDefault(d => d.Device.Id == device.Id); if (deviceItem == null) { deviceItem = new DeviceListItemViewModel(device); Devices.Add(deviceItem); //await Task.Delay(9000); } else { deviceItem.Update(device); } } catch (Exception ex) { _userDialogs.ShowError(ex.Message, 5000); return; } }
private async void ConnectAndDisposeDevice(DeviceListItemViewModel item) { try { using (item.Device) { _userDialogs.ShowLoading($"Connecting to {item.Name} ..."); await Adapter.ConnectToDeviceAsync(item.Device); // TODO make this configurable var resultMTU = await item.Device.RequestMtuAsync(60); System.Diagnostics.Debug.WriteLine($"Requested MTU. Result is {resultMTU}"); // TODO make this configurable var resultInterval = item.Device.UpdateConnectionInterval(ConnectionInterval.High); System.Diagnostics.Debug.WriteLine($"Set Connection Interval. Result is {resultInterval}"); item.Update(); //_userDialogs.ShowSuccess($"Connected {item.Device.Name}"); _userDialogs.Toast($"Connected {item.Device.Name}"); _userDialogs.HideLoading(); for (var i = 5; i >= 1; i--) { _userDialogs.ShowLoading($"Disconnect in {i}s..."); await Task.Delay(1000); _userDialogs.HideLoading(); } } } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Failed to connect and dispose."); } finally { _userDialogs.HideLoading(); } }
private async void ConnectToPreviousDeviceAsync() { IDevice device; try { CancellationTokenSource tokenSource = new CancellationTokenSource(); var config = new ProgressDialogConfig() { Title = $"Searching for '{PreviousGuid}'", CancelText = "Cancel", IsDeterministic = false, OnCancel = tokenSource.Cancel }; using (var progress = _userDialogs.Progress(config)) { progress.Show(); device = await Adapter.ConnectToKnownDeviceAsync(PreviousGuid, new ConnectParameters(autoConnect : UseAutoConnect, forceBleTransport : false), tokenSource.Token); } await _userDialogs.AlertAsync($"Connected to {device.Name}."); var deviceItem = Devices.FirstOrDefault(d => d.Device.Id == device.Id); if (deviceItem == null) { deviceItem = new DeviceListItemViewModel(device); Devices.Add(deviceItem); } else { deviceItem.Update(device); } } catch (Exception ex) { _userDialogs.ErrorToast(string.Empty, ex.Message, TimeSpan.FromSeconds(5000)); return; } }
private async void DisconnectDevice(DeviceListItemViewModel device) { try { device.IsMaster = false; device.IsSlave = false; if (!device.IsConnected) { return; } _userDialogs.ShowLoading($"Disconnecting {device.Name}..."); await Adapter.DisconnectDeviceAsync(device.Device); } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Disconnect error"); } finally { device.Update(); _userDialogs.HideLoading(); } }
private async void DisconnectDevice(DeviceListItemViewModel device) { try { if (!device.IsConnected) { return; } await UVIZIO.disconnectDevice(device, _userDialogs); } catch (Exception ex) { _userDialogs.Alert("Unable to Disconnect", "Disconnect error"); } finally { device.Update(); _userDialogs.HideLoading(); } }
private void HandleSelectedDevice(DeviceListItemViewModel device, int type) { //type = 1 if slave, type = 2 if master. var config = new ActionSheetConfig(); if (device.IsConnected) { config.Destructive = new ActionSheetOption("Disconnect", () => DisconnectCommand.Execute(device)); } else { config.Add("Connect", async() => { if (await ConnectDeviceAsync(device)) { switch (type) { case 1: device.IsSlave = true; GraphViewModel.SlaveDeviceId = device.Device.Id; var ServiceSlave = await device.Device.GetServiceAsync(Guid.Parse("0000180d-0000-1000-8000-00805f9b34fb")); var CharacteristicSlave = await ServiceSlave.GetCharacteristicAsync(Guid.Parse("00002a37-0000-1000-8000-00805f9b34fb")); await CharacteristicSlave.StartUpdatesAsync(); break; case 2: device.IsMaster = true; GraphViewModel.MasterDeviceId = device.Device.Id; var ServiceMaster = await device.Device.GetServiceAsync(Guid.Parse("0000180d-0000-1000-8000-00805f9b34fb")); var CharacteristicMaster = await ServiceMaster.GetCharacteristicAsync(Guid.Parse("00002a37-0000-1000-8000-00805f9b34fb")); await CharacteristicMaster.StartUpdatesAsync(); break; } } }); } config.Cancel = new ActionSheetOption("Cancel"); config.SetTitle("Device Options"); _userDialogs.ActionSheet(config); }
private async Task <bool> ConnectDeviceAsync(DeviceListItemViewModel device, bool showPrompt = true) { try { CancellationTokenSource tokenSource = new CancellationTokenSource(); var config = new ProgressDialogConfig() { Title = $"Connecting to '{device.Name}'", CancelText = "Cancel", IsDeterministic = false, OnCancel = tokenSource.Cancel }; using (var progress = _userDialogs.Progress(config)) { progress.Show(); await Adapter.ConnectToDeviceAsync(device.Device, tokenSource.Token); } _userDialogs.ShowSuccess($"Connected to {device.Device.Name}."); PreviousGuid = device.Device.Id; PreviousName = device.Name; return(true); } catch (Exception ex) { _userDialogs.Alert("Could not connect to " + device.Name, "Connection error"); Mvx.Trace(ex.Message); return(false); } finally { _userDialogs.HideLoading(); device.Update(); } }
private void HandleSelectedDevice(DeviceListItemViewModel device) { var config = new ActionSheetConfig(); if (device.IsConnected) { config.Add("Update RSSI", async() => { try { _userDialogs.ShowLoading(); await device.Device.UpdateRssiAsync(); device.RaisePropertyChanged(nameof(device.Rssi)); _userDialogs.HideLoading(); _userDialogs.ShowSuccess($"RSSI updated {device.Rssi}", 1000); } catch (Exception ex) { _userDialogs.HideLoading(); _userDialogs.ShowError($"Failed to update rssi. Exception: {ex.Message}"); } }); config.Destructive = new ActionSheetOption("Disconnect", () => DisconnectCommand.Execute(device)); } else { config.Add("Connect", async() => { if (await ConnectDeviceAsync(device)) { ShowViewModel <ServiceListViewModel>(new MvxBundle(new Dictionary <string, string> { { DeviceIdKey, device.Device.Id.ToString() } })); } }); //config.Add("Connect & Dispose", () => ConnectDisposeCommand.Execute(device)); config.Add("Save Advertised Data", () => { if (device.Device.AdvertisementRecords == null || device.Device.AdvertisementRecords.Count == 0) { _userDialogs.Alert("No Data Found"); return; } var advModel = new AdvertisementData() { Id = Guid.NewGuid(), DeviceId = device.Id.ToString(), Name = device.Name, Data = device.Device.AdvertisementRecords[0].Data }; _advertisementDataRepository.InsertDevice(advModel); }); } config.Add("Copy ID", () => CopyGuidCommand.Execute(device)); config.Cancel = new ActionSheetOption("Cancel"); config.SetTitle("Device Options"); _userDialogs.ActionSheet(config); }