示例#1
0
        public static async Task <bool> Connect(Page parentPage)
        {
            var waitHandle  = new EventWaitHandle(false, EventResetMode.AutoReset);
            var connectPage = new ConnectDevicePage();

            connectPage.Disappearing += (sender, e) => { waitHandle.Set(); };
            await parentPage.Navigation.PushModalAsync(connectPage);

            await Task.Run(() => waitHandle.WaitOne());

            return(connected);
        }
        protected override async void OnAppearing()
        {
            base.OnAppearing();
            if (!_reEntry)
            {
                _reEntry = true;
                var device = SoterDeviceFactoryBle.Instance.CurrentDevice;
                if ((device == null) || (!device.Connected))
                {
                    if (!await ConnectDevicePage.Connect(this))
                    {
                        await Navigation.PopModalAsync();

                        return;
                    }
                }
                try
                {
                    device             = SoterDeviceFactoryBle.Instance.CurrentDevice;
                    deviceName.Text    = device.Name;
                    PinPage.ParentPage = this;
                    device.DeviceButtonRequestCallback += Device_DeviceButtonRequestCallback;
                    try
                    {
                        while (_processStep < _selectedProcess.Count)
                        {
                            var currentState = _selectedProcess[_processStep];
                            switch (currentState)
                            {
                            case CommState.WipeDevice:
                                messageLabel.Text = AppResources.ConfirmWipeMessage;
                                await SoterDeviceFactoryBle.Instance.CurrentDevice.WipeDeviceAsync();

                                _state = CommState.ResetDevice;
                                break;

                            case CommState.ResetDevice:
                                messageLabel.Text = AppResources.InitializeDeviceMessage;
                                await SoterDeviceFactoryBle.Instance.CurrentDevice.ResetDeviceAsync(_deviceLabel);

                                _state = CommState.UpdateCoinTable;
                                break;

                            case CommState.UpdateCoinTable:
                                messageLabel.Text = AppResources.LoadingMessage;
                                await device.InitializeAsync();

                                Repository.SaveCurrentDeviceToDb(device);
                                await Repository.LoadCoinTableFromDeviceAsync(device);

                                break;

                            case CommState.SignBitcoinTx:
                                await device.InitializeAsync();

                                _bitcoinTX.SerializedTx = await device.SignTransactionAsync(_bitcoinTX);

                                break;
                            }
                            _processStep++;
                        }
                    }
                    finally
                    {
                        PinPage.ParentPage = null;
                        device.DeviceButtonRequestCallback -= Device_DeviceButtonRequestCallback;
                        device.Disconnect();
                        await Navigation.PopModalAsync();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex.ToString());
                }
                _waitHandle.Set();
            }
        }