public int WriteMessageEventReceived(byte[] writeBuf)
 {
     if(writeBuf[0] == Constants.IdMessageRestart)
     {
         lock (_locker)
         {
             if(_state == Network.StartState.OPPONENT_READY)
             {
                 // Set result and finish this Activity
                 SetResult(Android.App.Result.Ok, null);
                 Finish();
             }
             else if(_state == Network.StartState.NONE)
             {
                 _state = Network.StartState.WAITING_FOR_OPPONENT;
             }
         }
     }
     return 0;
 }
        public int StateNoneEventReceived()
        {
            #if DEBUG
            Log.Debug(Tag, "State = none");
            #endif

            // To avoid infinite loop because this function can throw a StateNoneEvent
            Network.Instance.StateNoneEvent -= StateNoneEventReceived;

            _isConnectionInitiator = false;
            _state = Network.StartState.NONE;

            // if the connection fail we remove the pop-up and restart the bluetooth
            DialogActivity.CloseAll();

            Network.Instance.CommunicationWay.Start();

            if(Network.Instance.CommunicationWay.State == BluetoothManager.StateEnum.None)
            {
                // if the bluetooth is still disabled, we need to stop
                Finish();
            }

            Network.Instance.StateNoneEvent += StateNoneEventReceived;

            return 0;
        }
        public int StartGameMessageReceived(byte[] message)
        {
            // close the pop-up in all the case
            DialogActivity.CloseAll();

            // We have recieve a demand to start the game
            // We verify that the two player have the same version of the application
            if(message[1] == Constants.NumVersion)
            {
                // The 2 players have the same version, we can launch the game if we are ready
                if(_state == Network.StartState.WAITING_FOR_OPPONENT)
                {
                    User.Instance.AddFriend(Network.Instance.CommunicationWay._deviceAddress, _opponentName);
                    MenuActivity.startTwoPlayerGame(this, _opponentName);
                }
                else
                {
                    // if we are not ready, display a pop-up asking if we want to play with this person
                    _state = Network.StartState.OPPONENT_READY;

                    // Display a pop-up asking if we want to play now that we are connected and the opponent is ready
                    Intent dialog = DialogActivity.CreateYesNoDialog(this, Resources.GetString(Resource.String.game_request_title),
                        String.Format(Resources.GetString(Resource.String.game_request), _opponentName),
                        delegate{SendStartGameMessage();}, delegate{CancelConnection();});
                    StartActivity(dialog);
                }
            }
            else
            {
                Intent dialog = UtilsDialog.CreateBluetoothDialogNoCancel(this, Resource.String.wrong_version);
                StartActivity(dialog);
                Network.Instance.CommunicationWay.Start(); // We restart the connection
            }
            return 0;
        }
        public void SendStartGameMessage()
        {
            byte[] message = {Constants.IdMessageStart, Constants.NumVersion};
            // We notify the opponent that we are ready
            Network.Instance.CommunicationWay.Write(message);

            if(_state != Network.StartState.OPPONENT_READY)
            {
                _state = Network.StartState.WAITING_FOR_OPPONENT;
                displayWaitingDialog(Resource.String.waiting_for_opponent);
            }
        }
        public void CancelConnection()
        {
            #if DEBUG
            Log.Debug(Tag, "CancelConnection()");
            #endif

            _state = Network.StartState.NONE;
            _isConnectionInitiator = false;

            DialogActivity.CloseAll();

            Network.Instance.CommunicationWay.Start();
        }
 private int OnReceiveNewGame()
 {
     lock (_stateLocker)
     {
         if(_restartState != Network.StartState.WAITING_FOR_OPPONENT)
         {
             _restartState = Network.StartState.OPPONENT_READY;
         }
         else
         {
             SetResult(Result.FirstUser);
             Finish();
         }
     }
     return 0;
 }
        public int WriteMessageEventReceived(byte[] writeBuf)
        {
            if(writeBuf[0] == Constants.IdMessageResume)
            {
                _gameTimer.AutoReset = true;
                _gameTimer.Interval = getTimerLapse();
                _gameTimer.Start();

                _originPause = StopOrigin.None;
            }
            else if(writeBuf[0] == Constants.IdMessageNewGame)
            {
                lock (_stateLocker)
                {
                    if(_restartState == Network.StartState.OPPONENT_READY)
                    {
                        SetResult(Result.FirstUser);
                        Finish();
                    }
                    else if(_restartState == Network.StartState.NONE)
                    {
                        _restartState = Network.StartState.WAITING_FOR_OPPONENT;
                    }
                }
            }
            return 0;
        }
 private int OnRestartReceived()
 {
     lock (_locker)
     {
         if(_state != Network.StartState.WAITING_FOR_OPPONENT)
         {
             _state = Network.StartState.OPPONENT_READY;
         }
         else
         {
             // Set result and finish this Activity
             SetResult(Android.App.Result.Ok, null);
             Finish();
         }
     }
     return 0;
 }
        private int OnFail()
        {
            #if DEBUG
            Log.Debug(Tag, "Fail");
            #endif

            if(_connectingOccured)
            {
                // Reset result and asking if we retry or finish this Activity
                SetResult(Android.App.Result.Canceled, null);

                if(!isDialogDisplayed)
                {
                    isDialogDisplayed = true;
                    Intent intent = DialogActivity.CreateYesNoDialog(this, Resource.String.retry_connection, -1,
                        delegate{isDialogDisplayed = false; restartBluetooth();}, delegate{isDialogDisplayed = false; Finish();});
                    StartActivity(intent);
                }
                _connectingOccured = false;
            }
            _state = Network.StartState.NONE;

            return 0;
        }