Пример #1
0
        /// <summary>
        /// Disconnect currently active connections and/or stop listening to tap events.
        /// </summary>
        public void Stop()
        {
            PeerFinder.Stop();

            switch (_status)
            {
            case ConnectionStatusValue.Searching:
            case ConnectionStatusValue.Connecting:
            case ConnectionStatusValue.Listening:
            {
                PeerFinder.TriggeredConnectionStateChanged -= TriggeredConnectionStateChanged;
                PeerFinder.ConnectionRequested             -= ConnectionRequested;
            }
            break;

            case ConnectionStatusValue.Connected:
            {
                if (_socket != null)
                {
                    _socket.Dispose();
                    _socket = null;
                }
            }
            break;
            }

            _status = ConnectionStatusValue.Idle;
        }
Пример #2
0
        /// <summary>
        /// Connect to detected remote device.
        ///
        /// Connecting action will be invoked when a socket connection is being created.
        /// Connected action will be invoked when a connection with another device has been established.
        /// ConnectivityProblem action will be invoked if connection to another device cannot be made.
        /// ConnectionInterrupted action will be invoked if connection to another device breaks.
        /// MessageReceived action will be invoked when a message from another device has been received.
        /// <param name="peer">Peer to connect to</param>
        /// </summary>
        public async void Connect(PeerInformation peer)
        {
            _status = ConnectionStatusValue.Connecting;

            try
            {
                if (Connecting != null)
                {
                    Connecting();
                }

                _socket = await PeerFinder.ConnectAsync(peer);

                if (_socket != null)
                {
                    PeerFinder.TriggeredConnectionStateChanged -= TriggeredConnectionStateChanged;
                    PeerFinder.ConnectionRequested             -= ConnectionRequested;

                    _status = ConnectionStatusValue.Connected;

                    _writer = new DataWriter(_socket.OutputStream);
                    _writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    _writer.ByteOrder       = Windows.Storage.Streams.ByteOrder.LittleEndian;

                    _reader = new DataReader(_socket.InputStream);
                    _reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    _reader.ByteOrder       = Windows.Storage.Streams.ByteOrder.LittleEndian;

                    ListenAsync();

                    SendNameAsync(NFCTalk.DataContext.Singleton.Settings.Name);

                    if (Connected != null)
                    {
                        Connected();
                    }
                }
                else if (ConnectivityProblem != null)
                {
                    ConnectivityProblem();
                }
            }
            catch (Exception ex)
            {
                if (ConnectivityProblem != null)
                {
                    ConnectivityProblem();
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Disconnect currently active connections and continue listening for new connections.
        /// </summary>
        public void Disconnect()
        {
            if (_status == ConnectionStatusValue.Connected)
            {
                if (_socket != null)
                {
                    _socket.Dispose();
                    _socket = null;
                }

                PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged;
                PeerFinder.ConnectionRequested             += ConnectionRequested;

                _status = ConnectionStatusValue.Searching;
            }
        }
Пример #4
0
        /// <summary>
        /// Start listening to tap events and attempt to connect if such occurs.
        ///
        /// Connecting action will be invoked when a tap event happens.
        /// Connected action will be invoked when a connection with another device has been established.
        /// ConnectivityProblem action will be invoked if connection to another device cannot be made.
        /// ConnectionInterrupted action will be invoked if connection to another device breaks.
        /// MessageReceived action will be invoked when a message from another device has been received.
        /// </summary>
        public void Start()
        {
            if (_status == ConnectionStatusValue.Idle)
            {
                _status = ConnectionStatusValue.Searching;

                PeerName = "";

                PeerFinder.DisplayName = NFCTalk.DataContext.Singleton.Settings.Name;
                PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged;
                PeerFinder.ConnectionRequested             += ConnectionRequested;

                PeerFinder.Start();
            }
            else
            {
                throw new Exception("Bad state, please stop first");
            }
        }
Пример #5
0
        /// <summary>
        /// Start listening to tap events and attempt to connect if such occurs.
        ///
        /// Connecting action will be invoked when a tap event happens.
        /// Connected action will be invoked when a connection with another device has been established.
        /// ConnectivityProblem action will be invoked if connection to another device cannot be made.
        /// ConnectionInterrupted action will be invoked if connection to another device breaks.
        /// MessageReceived action will be invoked when a message from another device has been received.
        /// </summary>
        public void Start()
        {
            if (_status == ConnectionStatusValue.Idle)
            {
                _status = ConnectionStatusValue.Searching;

                PeerName = "";

                PeerFinder.DisplayName = NFCTalk.DataContext.Singleton.Settings.Name;
                PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged;
                PeerFinder.ConnectionRequested             += ConnectionRequested;

                PeerFinder.Start();
            }
            else if (_status != ConnectionStatusValue.Connected)
            {
                //throw new Exception(string.Concat("Bad state, please stop first - _status >>> ", _status));
                this.Stop();
            }
        }
Пример #6
0
        /// <summary>
        /// Event handler to be executed when PeerFinder's ConnectionRequested fires.
        /// </summary>
        private async void ConnectionRequested(object sender, ConnectionRequestedEventArgs e)
        {
            try
            {
                if (Connecting != null)
                {
                    Connecting();
                }

                _socket = await PeerFinder.ConnectAsync(e.PeerInformation);

                PeerFinder.TriggeredConnectionStateChanged -= TriggeredConnectionStateChanged;
                PeerFinder.ConnectionRequested -= ConnectionRequested;

                _writer = new DataWriter(_socket.OutputStream);
                _writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                _writer.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;

                _reader = new DataReader(_socket.InputStream);
                _reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                _reader.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;

                _status = ConnectionStatusValue.Connected;

                ListenAsync();

                SendNameAsync(NFCTalk.DataContext.Singleton.Settings.Name);

                if (Connected != null)
                {
                    Connected();
                }
            }
            catch (Exception ex)
            {
                if (ConnectivityProblem != null)
                {
                    ConnectivityProblem();
                }
            }
        }
Пример #7
0
        /// <summary>
        /// Event handler to be executed when PeerFinder's TriggeredConnectionStateChanged fires.
        /// </summary>
        private void TriggeredConnectionStateChanged(object sender, TriggeredConnectionStateChangedEventArgs e)
        {
            switch (e.State)
            {
                case TriggeredConnectState.PeerFound: System.Diagnostics.Debug.WriteLine("PeerFound");
                    {
                        if (Connecting != null)
                        {
                            Connecting();
                        }
                    }
                    break;

                case TriggeredConnectState.Connecting: System.Diagnostics.Debug.WriteLine("Connecting");
                    {
                        _status = ConnectionStatusValue.Connecting;
                    }
                    break;

                case TriggeredConnectState.Listening: System.Diagnostics.Debug.WriteLine("Listening");
                    {
                        _status = ConnectionStatusValue.Listening;
                    }
                    break;

                case TriggeredConnectState.Completed: System.Diagnostics.Debug.WriteLine("Completed");
                    {
                        PeerFinder.TriggeredConnectionStateChanged -= TriggeredConnectionStateChanged;
                        PeerFinder.ConnectionRequested -= ConnectionRequested;
                        
                        _socket = e.Socket;

                        _writer = new DataWriter(_socket.OutputStream);
                        _writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                        _writer.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;

                        _reader = new DataReader(_socket.InputStream);
                        _reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                        _reader.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian;

                        _status = ConnectionStatusValue.Connected;
                        
                        ListenAsync();

                        SendNameAsync(NFCTalk.DataContext.Singleton.Settings.Name);

                        if (Connected != null)
                        {
                            Connected();
                        }
                    }
                    break;

                case TriggeredConnectState.Canceled: System.Diagnostics.Debug.WriteLine("Canceled");
                    {
                        if (ConnectivityProblem != null)
                        {
                            ConnectivityProblem();
                        }
                    }
                    break;

                case TriggeredConnectState.Failed: System.Diagnostics.Debug.WriteLine("Failed");
                    {
                        if (ConnectivityProblem != null)
                        {
                            ConnectivityProblem();
                        }
                    }
                    break;
            }
        }
Пример #8
0
        /// <summary>
        /// Disconnect currently active connections and continue listening for new connections.
        /// </summary>
        public void Disconnect()
        {
            if (_status == ConnectionStatusValue.Connected)
            {
                if (_socket != null)
                {
                    _socket.Dispose();
                    _socket = null;
                }

                PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged;
                PeerFinder.ConnectionRequested += ConnectionRequested;

                _status = ConnectionStatusValue.Searching;
            }
        }
Пример #9
0
        /// <summary>
        /// Disconnect currently active connections and/or stop listening to tap events.
        /// </summary>
        public void Stop()
        {
            PeerFinder.Stop();

            switch (_status)
            {
                case ConnectionStatusValue.Searching:
                case ConnectionStatusValue.Connecting:
                case ConnectionStatusValue.Listening:
                    {
                        PeerFinder.TriggeredConnectionStateChanged -= TriggeredConnectionStateChanged;
                        PeerFinder.ConnectionRequested -= ConnectionRequested;
                    }
                    break;

                case ConnectionStatusValue.Connected:
                    {
                        if (_socket != null)
                        {
                            _socket.Dispose();
                            _socket = null;
                        }
                    }
                    break;
            }

            _status = ConnectionStatusValue.Idle;
        }
Пример #10
0
        /// <summary>
        /// Start listening to tap events and attempt to connect if such occurs.
        /// 
        /// Connecting action will be invoked when a tap event happens.
        /// Connected action will be invoked when a connection with another device has been established.
        /// ConnectivityProblem action will be invoked if connection to another device cannot be made.
        /// ConnectionInterrupted action will be invoked if connection to another device breaks.
        /// MessageReceived action will be invoked when a message from another device has been received.
        /// </summary>
        public void Start()
        {
            if (_status == ConnectionStatusValue.Idle)
            {
                _status = ConnectionStatusValue.Searching;

                PeerName = "";

                PeerFinder.DisplayName = NFCTalk.DataContext.Singleton.Settings.Name;
                PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged;
                PeerFinder.ConnectionRequested += ConnectionRequested;

                PeerFinder.Start();
            }
            else
            {
                throw new Exception("Bad state, please stop first");
            }
        }
Пример #11
0
        /// <summary>
        /// Event handler to be executed when PeerFinder's TriggeredConnectionStateChanged fires.
        /// </summary>
        private void TriggeredConnectionStateChanged(object sender, TriggeredConnectionStateChangedEventArgs e)
        {
            switch (e.State)
            {
            case TriggeredConnectState.PeerFound: System.Diagnostics.Debug.WriteLine("PeerFound");
                {
                    if (Connecting != null)
                    {
                        Connecting();
                    }
                }
                break;

            case TriggeredConnectState.Connecting: System.Diagnostics.Debug.WriteLine("Connecting");
                {
                    _status = ConnectionStatusValue.Connecting;
                }
                break;

            case TriggeredConnectState.Listening: System.Diagnostics.Debug.WriteLine("Listening");
                {
                    _status = ConnectionStatusValue.Listening;
                }
                break;

            case TriggeredConnectState.Completed: System.Diagnostics.Debug.WriteLine("Completed");
                {
                    PeerFinder.TriggeredConnectionStateChanged -= TriggeredConnectionStateChanged;
                    PeerFinder.ConnectionRequested             -= ConnectionRequested;

                    _socket = e.Socket;

                    _writer = new DataWriter(_socket.OutputStream);
                    _writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    _writer.ByteOrder       = Windows.Storage.Streams.ByteOrder.LittleEndian;

                    _reader = new DataReader(_socket.InputStream);
                    _reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    _reader.ByteOrder       = Windows.Storage.Streams.ByteOrder.LittleEndian;

                    _status = ConnectionStatusValue.Connected;

                    ListenAsync();

                    SendNameAsync(NFCTalk.DataContext.Singleton.Settings.Name);

                    if (Connected != null)
                    {
                        Connected();
                    }
                }
                break;

            case TriggeredConnectState.Canceled: System.Diagnostics.Debug.WriteLine("Canceled");
                {
                    if (ConnectivityProblem != null)
                    {
                        ConnectivityProblem();
                    }
                }
                break;

            case TriggeredConnectState.Failed: System.Diagnostics.Debug.WriteLine("Failed");
                {
                    if (ConnectivityProblem != null)
                    {
                        ConnectivityProblem();
                    }
                }
                break;
            }
        }
        /// <summary>
        /// Start listening to tap events and attempt to connect if such occurs.
        /// 
        /// Connecting action will be invoked when a tap event happens.
        /// Connected action will be invoked when a connection with another device has been established.
        /// ConnectivityProblem action will be invoked if connection to another device cannot be made.
        /// ConnectionInterrupted action will be invoked if connection to another device breaks.
        /// MessageReceived action will be invoked when a message from another device has been received.
        /// </summary>
        public void Start()
        {
            if (_status == ConnectionStatusValue.Idle)
            {
                _status = ConnectionStatusValue.Searching;

                PeerName = "";

                PeerFinder.DisplayName = NFCTalk.DataContext.Singleton.Settings.Name;
                PeerFinder.TriggeredConnectionStateChanged += TriggeredConnectionStateChanged;
                PeerFinder.ConnectionRequested += ConnectionRequested;

                PeerFinder.Start();
            }
            else if (_status != ConnectionStatusValue.Connected)
            {
                //throw new Exception(string.Concat("Bad state, please stop first - _status >>> ", _status));
                this.Stop();
            }
        }