Exemplo n.º 1
0
        /// <summary>
        /// Tries to connect device.
        /// </summary>
        /// <returns>The to connect device.</returns>
        /// <param name="deviceViewModel">Device view model.</param>
        /// <param name="showPrompt">If set to <c>true</c> show prompt.</param>
        private async Task <bool> TryToConnectDevice(int index = -1, bool showPrompt = true)
        {
            try
            {
                CancellationTokenSource tokenSource = new CancellationTokenSource();

                var config = new ProgressDialogConfig()
                {
                    Title           = $"Trying to establish connection with '{DeviceName}'",
                    CancelText      = "Cancel",
                    IsDeterministic = false,
                    MaskType        = MaskType.None,
                    OnCancel        = tokenSource.Cancel
                };

                bool didConnect = false;
                using (var progress = _userDialogs.Progress(config))
                {
                    progress.Show();
                    if (index > -1)
                    {
                        didConnect = await _bluetoothService.ConnectDevice(SensorDevice, tokenSource, index);
                    }
                    else
                    {
                        didConnect = await _bluetoothService.ConnectDevice(SensorDevice, tokenSource, -1);
                    }
                }

                tokenSource.Dispose();

                if (!didConnect)
                {
                    return(false);
                }
                _userDialogs.Toast($"\tConnected to {SensorDevice.Name}");

                RaisePropertyChanged("ConnectOrDisposeActionString");
                RaisePropertyChanged("ImageSource");
                return(true);
            }
            catch (Exception ex)
            {
                _userDialogs.Alert(ex.Message, "Connection error");
                _log.Trace(ex.Message);
                return(false);
            }
            finally
            {
                _userDialogs.HideLoading();
                Update();
            }
        }