コード例 #1
0
        private async void Connect()
        {
            try
            {
                using (UserDialogs.Instance.Loading("Connecting..."))
                {
                    _transporter = ResonanceTransporter.Builder
                                   .Create()
                                   .WithAdapter(new BluetoothAdapter(SelectedDevice))
                                   .WithJsonTranscoding()
                                   .Build();

                    _transporter.StateChanged += (x, e) =>
                    {
                        InvokeUI(async() =>
                        {
                            SendCommand.ChangeCanExecute();

                            if (e.PreviousState == ResonanceComponentState.Connected && e.NewState == ResonanceComponentState.Failed)
                            {
                                await Navigation.PopAsync();
                                UserDialogs.Instance.Toast($"Connection lost.\n{_transporter.FailedStateException}");
                            }
                        });
                    };

                    _transporter.RegisterRequestHandler <ChatMessageRequest, ChatMessageResponse>(OnChatRequest);

                    await _transporter.ConnectAsync();

                    SelectedDevice.Refresh();
                    Logs.Clear();
                }

                await Navigation.PushAsync(new ChatPage(), true);
            }
            catch (Exception ex)
            {
                UserDialogs.Instance.Toast($"Error connecting to the selected device.\n{ex.Message}");
            }
        }