示例#1
0
        public async Task Connect()
        {
            _agent = await Agent.GetAgent("/org/cybernetic87/gassist").ConfigureAwait(false);

            _agent.PeerStatusChanged += PeerStatusChanged;
            var peers = await _agent.FindPeers().ConfigureAwait(false);

            if (peers.Any())
            {
                _peer       = peers.First();
                _connection = _peer.Connection;
                _connection.DataReceived  -= Connection_DataReceived;
                _connection.DataReceived  += Connection_DataReceived;
                _connection.StatusChanged -= Connection_StatusChanged;
                _connection.StatusChanged += Connection_StatusChanged;
                try
                {
                    await _connection.Open().ConfigureAwait(false);
                }
                catch (Exception)
                {
                    TextPopUp.Text = "Phone app is not configured. Configure the phone app.";
                    TextPopUp.Show();
                    await Task.Delay(2000).ConfigureAwait(false);
                    await Connect().ConfigureAwait(false);
                }
                if (_reconnectTimer.Enabled)
                {
                    _reconnectTimer.Stop();
                }
            }
            else
            {
                if (BluetoothAdapter.IsBluetoothEnabled)
                {
                    if (BluetoothAdapter
                        .GetBondedDevices()
                        .First(x => x.Class.MajorDeviceClassType == BluetoothMajorDeviceClassType.Phone)
                        .IsConnected)
                    {
                        TextPopUp.Text = "Companion app is not installed, check your phone.";
                        TextPopUp.Show();
                        LaunchApp();
                    }
                    else
                    {
                        TextPopUp.Text = "Unable to connect to phone. Please check if Bluetooth is turned on both devices.";
                        TextPopUp.Show();
                    }
                }
                else
                {
                    PromptBluetooth();
                }
            }
        }