Beispiel #1
0
        /// <summary>
        /// Entry point of connection thread
        /// </summary>
        private async Task ConnectThreadRunAsync(PairedFoxDTO foxToConnect)
        {
            Exception lastException = null;

            using (IProgressDialog progress = UserDialogs.Instance.Progress("Connecting...", null, null, true, MaskType.Clear))
            {
                for (var attempt = 0; attempt < ConnectionAttemptsCount; attempt++)
                {
                    progress.PercentComplete = (int)Math.Round(100 * attempt / (double)ConnectionAttemptsCount);

                    try
                    {
                        await _bluetoothCommunicator.ConnectAsync(foxToConnect);

                        // We survived till here, so connection is estabilished
                        lastException = null;
                        break;
                    }
                    catch (Exception ex)
                    {
                        lastException = ex;
                    }
                }
            }

            if (lastException != null)
            {
                _onConnectionFailed(lastException);
                await _userNotifier.ShowErrorMessageAsync("Failed to connect!", $"Reason: { lastException.Message }");
            }
        }
Beispiel #2
0
        public async Task ConnectAsync(PairedFoxDTO foxToConnect)
        {
            _ = foxToConnect ?? throw new ArgumentNullException(nameof(foxToConnect));

            if (!_isDelegatesSetUp)
            {
                throw new InvalidOperationException("Delegates aren't set up!");
            }

            var connectThread = new Thread(async() => await ConnectThreadRunAsync(foxToConnect));

            connectThread.Start();
        }
        public async Task ConnectAsync(PairedFoxDTO fox)
        {
            if (_socket != null && _socket.IsConnected)
            {
                throw new InvalidOperationException("Already connected to fox!");
            }

            if (_onBTCommunicatorConnect == null)
            {
                throw new InvalidOperationException("OnBTCommunicatorConnect delegate is null!");
            }

            if (_onBTCommunicatorDisconnect == null)
            {
                throw new InvalidOperationException("OnBTCommunicationDisconnect delegate is null!");
            }

            if (_onBTCommunicatorNewByteRead == null)
            {
                throw new InvalidOperationException("OnBTCommunicatorNewByteRead delegate is null!");
            }

            var foxes = await _pairedFoxesEnumerator.EnumerateAsync();

            if (!foxes.Any(f => f.MAC.Equals(fox.MAC)))
            {
                throw new InvalidOperationException($"Fox { fox.DisplayName } is not found!");
            }

            // We are sure that adapted exists and enabled because of checks in _pairedFoxesEnumerator.EnumerateAsync()
            // Also we are sure that fox with given MAC exists too
            var device = BluetoothAdapter
                         .DefaultAdapter
                         .BondedDevices
                         .First(d => d.Address.Equals(fox.MAC));

            try
            {
                // What does this magic UUID mean?
                _socket = device.CreateInsecureRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
                if (_socket == null)
                {
                    throw new InvalidOperationException("Unable to get bluetooth socket!");
                }

                _socket.Connect();
                if (!_socket.IsConnected)
                {
                    throw new InvalidOperationException("Unable to connect to fox!");
                }

                // Starting reader thread
                _readerThread = new Thread(new ThreadStart(ReaderThreadRun));
                _readerThread.Start();

                _onBTCommunicatorConnect(fox);
            }
            catch (Exception)
            {
                if (_socket != null && _socket.IsConnected)
                {
                    _socket.Close();
                }

                throw;
            }
        }
Beispiel #4
0
 private void OnConnect(PairedFoxDTO connectedFox)
 {
     _onConnected(connectedFox);
 }