Ejemplo n.º 1
0
        protected virtual void OnBluetoothAdapterEnabled()
        {
            if (_bluetoothNetworkManagerSettings.LogBluetoothEvents)
            {
                Debug.Log("Bluetooth Event - AdapterEnabled");
            }

            // Resuming desired action after enabling the adapter
            switch (_desiredMode)
            {
            case BluetoothMultiplayerMode.Server:
                StopAll();
                AndroidBluetoothMultiplayer.StartServer((ushort)_networkManager.networkPort);
                break;

            case BluetoothMultiplayerMode.Client:
                StopAll();
                // Open device picker dialog
                if (_customDeviceBrowser != null)
                {
                    _customDeviceBrowser.Open();
                }
                else
                {
                    AndroidBluetoothMultiplayer.ShowDeviceList();
                }
                break;
            }

            _desiredMode = BluetoothMultiplayerMode.None;
        }
Ejemplo n.º 2
0
        private void StartBluetoothClient(Action onReadyAction)
        {
#if !UNITY_EDITOR
            _clientAction = onReadyAction;

            // If Bluetooth is enabled, immediately open the device picker
            if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
            {
                StopAll();
                // Open device picker dialog
                if (_customDeviceBrowser != null)
                {
                    _customDeviceBrowser.Open();
                }
                else
                {
                    AndroidBluetoothMultiplayer.ShowDeviceList();
                }
            }
            else
            {
                // Otherwise, we have to enable Bluetooth first and wait for callback
                _desiredMode = BluetoothMultiplayerMode.Client;
                AndroidBluetoothMultiplayer.RequestEnableBluetooth();
            }
#else
            onReadyAction();
#endif
        }
        private void OnGUI() {
            float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();
            // If initialization was successfull, showing the buttons
            if (_initResult) {
                // If there is no current Bluetooth connectivity
                BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();
                if (currentMode == BluetoothMultiplayerMode.None) {
                    if (GUI.Button(new Rect(10, 10, 150, 50), "Create server")) {
                        // If Bluetooth is enabled, then we can do something right on
                        if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled()) {
                            AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                            Network.Disconnect(); // Just to be sure
                            AndroidBluetoothMultiplayer.StartServer(kPort);
                        } else {
                            // Otherwise we have to enable Bluetooth first and wait for callback
                            _desiredMode = BluetoothMultiplayerMode.Server;
                            AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                        }
                    }

                    if (GUI.Button(new Rect(170, 10, 150, 50), "Connect to server")) {
                        // If Bluetooth is enabled, then we can do something right on
                        if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled()) {
                            Network.Disconnect(); // Just to be sure
                            AndroidBluetoothMultiplayer.ShowDeviceList(); // Open device picker dialog
                        } else {
                            // Otherwise we have to enable Bluetooth first and wait for callback
                            _desiredMode = BluetoothMultiplayerMode.Client;
                            AndroidBluetoothMultiplayer.RequestEnableBluetooth();
                        }
                    }
                } else {
                    // Stop all networking
                    if (GUI.Button(new Rect(10, 10, 150, 50), currentMode  == BluetoothMultiplayerMode.Client ? "Disconnect" : "Stop server")) {
                        if (Network.peerType != NetworkPeerType.Disconnected)
                            Network.Disconnect();
                    }
                }
            } else {
                // Show a message if initialization failed for some reason
                GUI.contentColor = Color.black;
                GUI.Label(
                    new Rect(10, 10, Screen.width / scaleFactor - 10, 50), 
                    "Bluetooth not available. Are you running this on Bluetooth-capable " +
                    "Android device and AndroidManifest.xml is set up correctly?");
            }

            DrawBackButton(scaleFactor);
        }
        protected override void Update()
        {
            base.Update();

            // Spawn an effect where user has tapped
            if (NetworkClient.active && Input.GetMouseButtonDown(0))
            {
                Vector2 tapPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                // Send the message with the tap position to the server, so it can send it to other clients
                NetworkManager.singleton.client.Send(CreateTapMarkerMessage.kMessageType, new CreateTapMarkerMessage {
                    Position = tapPosition
                });

                // Local client can just instantiate the effect for immediate response, as this is a purely visual thing
                Instantiate(TapMarkerPrefab, tapPosition, Quaternion.identity);
            }

            // Refresh UI
            UIPanelGameObject.SetActive(AndroidBluetoothNetworkManagerHelper.IsInitialized);
            ErrorUIPanelGameObject.SetActive(!AndroidBluetoothNetworkManagerHelper.IsInitialized);

            if (!AndroidBluetoothNetworkManagerHelper.IsInitialized)
            {
                return;
            }

            BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();

            StartServerButtonGameObject.SetActive(currentMode == BluetoothMultiplayerMode.None);
            ConnectToServerButtonGameObject.SetActive(currentMode == BluetoothMultiplayerMode.None);
            DisconnectButtonGameObject.SetActive(currentMode != BluetoothMultiplayerMode.None);
            if (DisconnectButtonGameObject.activeInHierarchy)
            {
                DisconnectButtonGameObject.GetComponentInChildren <Text>().text = currentMode == BluetoothMultiplayerMode.Client ? "Disconnect" : "Stop server";
            }

            bool togglesInteractable = currentMode == BluetoothMultiplayerMode.None;
            bool togglesActive       = currentMode != BluetoothMultiplayerMode.Client;

            StressTestToggle.interactable = togglesInteractable;
            StressTestToggle.gameObject.SetActive(togglesActive);

            UseCustomDeviceBrowserUIToggle.interactable = togglesInteractable;
            UseCustomDeviceBrowserUIToggle.gameObject.SetActive(togglesActive);

            // Update values
            BluetoothMultiplayerDemoNetworkManager.StressTestMode = StressTestToggle.isOn;
        }
Ejemplo n.º 5
0
 public void ConnectToServer()
 {
     // If Bluetooth is enabled, then we can do something right on
     if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
     {
         Network.Disconnect();                         // Just to be sure
         AndroidBluetoothMultiplayer.ShowDeviceList(); // Open device picker dialog
     }
     else
     {
         // Otherwise we have to enable Bluetooth first and wait for callback
         _desiredMode = BluetoothMultiplayerMode.Client;
         AndroidBluetoothMultiplayer.RequestEnableBluetooth();
     }
 }
Ejemplo n.º 6
0
    void onBluetoothAdapterEnabled()
    {
        switch (desiredMode)
        {
        case BluetoothMultiplayerMode.Server:
            Network.Disconnect();
            BluetoothMultiplayerAndroid.InitializeServer((ushort)Common.Port);
            break;

        case BluetoothMultiplayerMode.Client:
            Network.Disconnect();
            BluetoothMultiplayerAndroid.ShowDeviceList();
            break;
        }
        desiredMode = BluetoothMultiplayerMode.None;
    }
Ejemplo n.º 7
0
//		private void OnGUI() {
//			float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();
//			// If initialization was successfull, showing the buttons
//			if (_initResult) {
//				// If there is no current Bluetooth connectivity
//				BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();
//				if (currentMode == BluetoothMultiplayerMode.None) {
//					if (GUI.Button(new Rect(10, 10, 150, 50), "Create server")) {
//						// If Bluetooth is enabled, then we can do something right on
//						if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled()) {
//							AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
//							Network.Disconnect(); // Just to be sure
//							AndroidBluetoothMultiplayer.StartServer(kPort);
//						} else {
//							// Otherwise we have to enable Bluetooth first and wait for callback
//							_desiredMode = BluetoothMultiplayerMode.Server;
//							AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
//						}
//					}
//
//					if (GUI.Button(new Rect(170, 10, 150, 50), "Connect to server")) {
//						// If Bluetooth is enabled, then we can do something right on
//						if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled()) {
//							Network.Disconnect(); // Just to be sure
//							AndroidBluetoothMultiplayer.ShowDeviceList(); // Open device picker dialog
//						} else {
//							// Otherwise we have to enable Bluetooth first and wait for callback
//							_desiredMode = BluetoothMultiplayerMode.Client;
//							AndroidBluetoothMultiplayer.RequestEnableBluetooth();
//						}
//					}
//				} else {
//					// Stop all networking
//					if (GUI.Button(new Rect(10, 10, 150, 50), currentMode  == BluetoothMultiplayerMode.Client ? "Disconnect" : "Stop server")) {
//						if (Network.peerType != NetworkPeerType.Disconnected)
//							Network.Disconnect();
//					}
//				}
//			} else {
//				// Show a message if initialization failed for some reason
//				GUI.contentColor = Color.black;
//				GUI.Label(
//					new Rect(10, 10, Screen.width / scaleFactor - 10, 50),
//					"Bluetooth not available. Are you running this on Bluetooth-capable " +
//					"Android device and AndroidManifest.xml is set up correctly?");
//			}
//		}

        public void CreateServer()
        {
            // If Bluetooth is enabled, then we can do something right on
            if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
            {
                AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                Network.Disconnect();                 // Just to be sure
                AndroidBluetoothMultiplayer.StartServer(kPort);
            }
            else
            {
                // Otherwise we have to enable Bluetooth first and wait for callback
                _desiredMode = BluetoothMultiplayerMode.Server;
                AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
            }
        }
Ejemplo n.º 8
0
        private void Update()
        {
            float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();

            // If initialization was successfull, showing the buttons
            if (_initResult)
            {
                // If there is no current Bluetooth connectivity
                BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();

                if (_readyToLoadGameScene)
                {
                    CreateServerButton.SetActive(false);
                    ConnectToServerButton.SetActive(false);
                    DisconnectOrStopServerButton.SetActive(false);
                    WaitingForOpponentToJoinLabel.SetActive(false);
                    ReadyToLoadGameScene.SetActive(true);
                }
                else
                {
                    if (currentMode == BluetoothMultiplayerMode.None)
                    {
                        CreateServerButton.SetActive(true);
                        ConnectToServerButton.SetActive(true);
                        DisconnectOrStopServerButton.SetActive(false);
                        WaitingForOpponentToJoinLabel.SetActive(false);
                        ReadyToLoadGameScene.SetActive(false);
                    }
                    else
                    {
                        CreateServerButton.SetActive(false);
                        ConnectToServerButton.SetActive(false);
                        DisconnectOrStopServerButton.SetActive(true);
                        ReadyToLoadGameScene.SetActive(false);

                        if (currentMode == BluetoothMultiplayerMode.Server)
                        {
                            WaitingForOpponentToJoinLabel.SetActive(true);
                        }
                        else
                        {
                            WaitingForOpponentToJoinLabel.SetActive(false);
                        }
                    }
                }
            }
        }
Ejemplo n.º 9
0
 //连接到服务器
 public void ConnectToServer()
 {
     if (initResult)
     {
         if (BluetoothMultiplayerAndroid.CurrentMode() == BluetoothMultiplayerMode.None)
         {
             if (BluetoothMultiplayerAndroid.IsBluetoothEnabled())
             {
                 Network.Disconnect();
                 BluetoothMultiplayerAndroid.ShowDeviceList();
             }
             else
             {
                 desiredMode = BluetoothMultiplayerMode.Client;
                 BluetoothMultiplayerAndroid.RequestBluetoothEnable();
             }
         }
     }
 }
Ejemplo n.º 10
0
 //创建服务器
 public void CreateServer()
 {
     if (initResult)
     {
         if (BluetoothMultiplayerAndroid.CurrentMode() == BluetoothMultiplayerMode.None)
         {
             if (BluetoothMultiplayerAndroid.IsBluetoothEnabled())
             {
                 Network.Disconnect();
                 BluetoothMultiplayerAndroid.InitializeServer((ushort)Common.Port);
             }
             else
             {
                 desiredMode = BluetoothMultiplayerMode.Server;
                 BluetoothMultiplayerAndroid.RequestBluetoothEnable();
             }
         }
     }
 }
	public void StartHostBluetooth(){
		Debug.Log ("StartHostBluetooth\n-------\n------------\n------------\n----------\n--------\n------------\n----------");
		
		if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled()) {
			Debug.Log ("Bluetooth is Enabled");
			
			AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
			Network.Disconnect(); // Just to be sure
			AndroidBluetoothMultiplayer.StartServer(kPort);
			
		} else {
			// Otherwise we have to enable Bluetooth first and wait for callback
			Debug.Log ("Bluetooth is Disabled");
			
			_desiredMode = BluetoothMultiplayerMode.Server;
			AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
		}

		
	}
        private void OnBluetoothAdapterEnabled()
        {
            Debug.Log("Event - AdapterEnabled");

            // Resuming desired action after enabling the adapter
            switch (_desiredMode)
            {
            case BluetoothMultiplayerMode.Server:
                Network.Disconnect();
                AndroidBluetoothMultiplayer.StartServer(kPort);
                break;

            case BluetoothMultiplayerMode.Client:
                Network.Disconnect();
                AndroidBluetoothMultiplayer.ShowDeviceList();
                break;
            }

            _desiredMode = BluetoothMultiplayerMode.None;
        }
    private void onBluetoothAdapterEnabled()
    {
        Debug.Log("onBluetoothAdapterEnabled()");

        // Resuming desired action after enabling the adapter
        switch (_desiredMode)
        {
        case BluetoothMultiplayerMode.Server:
            Network.Disconnect();
            BluetoothMultiplayerAndroid.InitializeServer(Port);
            break;

        case BluetoothMultiplayerMode.Client:
            Network.Disconnect();
            Debug.Log("BluetoothMultiplayerAndroid.ShowDeviceList()");
            BluetoothMultiplayerAndroid.ShowDeviceList();
            break;
        }

        _desiredMode = BluetoothMultiplayerMode.None;
    }
Ejemplo n.º 14
0
        protected virtual void StartBluetoothHost(Action onReadyAction)
        {
#if !UNITY_EDITOR
            _hostAction = onReadyAction;

            // If Bluetooth is enabled, immediately start the Bluetooth server
            if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
            {
                AndroidBluetoothMultiplayer.RequestEnableDiscoverability(_bluetoothNetworkManagerSettings.DefaultBluetoothDiscoverabilityInterval);
                StopAll(); // Just to be sure
                AndroidBluetoothMultiplayer.StartServer((ushort)_networkManager.networkPort);
            }
            else
            {
                // Otherwise, we have to enable Bluetooth first and wait for callback
                _desiredMode = BluetoothMultiplayerMode.Server;
                AndroidBluetoothMultiplayer.RequestEnableDiscoverability(_bluetoothNetworkManagerSettings.DefaultBluetoothDiscoverabilityInterval);
            }
#else
            onReadyAction();
#endif
        }
        protected override void Update()
        {
            base.Update();

            if (Input.GetKeyDown(KeyCode.Escape))
            {
                QuitApplication();
            }


            if (!AndroidBluetoothNetworkManagerHelper.IsInitialized)
            {
                return;
            }

            BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();

            disconnectButtonRef.gameObject.SetActive(currentMode != BluetoothMultiplayerMode.None);
            if (disconnectButtonRef.gameObject.activeInHierarchy)
            {
                disconnectButtonRef.gameObject.GetComponentInChildren <Text>().text = currentMode == BluetoothMultiplayerMode.Client ? "Disconnect" : "Stop server";
            }
        }
Ejemplo n.º 16
0
    // Don't forget to unregister the event delegates!
    public void Destroy()
    {
        _desiredMode = BluetoothMultiplayerMode.None;

        BluetoothMultiplayerAndroidManager.onBluetoothListeningStartedEvent       -= onBluetoothListeningStarted;
        BluetoothMultiplayerAndroidManager.onBluetoothListeningCanceledEvent      -= onBluetoothListeningCanceled;
        BluetoothMultiplayerAndroidManager.onBluetoothAdapterEnabledEvent         -= onBluetoothAdapterEnabled;
        BluetoothMultiplayerAndroidManager.onBluetoothAdapterEnableFailedEvent    -= onBluetoothAdapterEnableFailed;
        BluetoothMultiplayerAndroidManager.onBluetoothAdapterDisabledEvent        -= onBluetoothAdapterDisabled;
        BluetoothMultiplayerAndroidManager.onBluetoothConnectedToServerEvent      -= onBluetoothConnectedToServer;
        BluetoothMultiplayerAndroidManager.onBluetoothConnectToServerFailedEvent  -= onBluetoothConnectToServerFailed;
        BluetoothMultiplayerAndroidManager.onBluetoothDisconnectedFromServerEvent -= onBluetoothDisconnectedFromServer;
        BluetoothMultiplayerAndroidManager.onBluetoothClientConnectedEvent        -= onBluetoothClientConnected;
        BluetoothMultiplayerAndroidManager.onBluetoothClientDisconnectedEvent     -= onBluetoothClientDisconnected;
        BluetoothMultiplayerAndroidManager.onBluetoothDevicePickedEvent           -= onBluetoothDevicePicked;

        // Gracefully closing all Bluetooth connectivity and loading the menu
        try {
            BluetoothMultiplayerAndroid.StopDiscovery();
            BluetoothMultiplayerAndroid.Disconnect();
        } catch {
            //
        }
    }
 public void disconnectNetWork()
 {
     Network.Disconnect();
     desiredMode = BluetoothMultiplayerMode.None;
 }
        private void OnGUI()
        {
#if UNITY_ANDROID
            if (!showGUI)
            {
                return;
            }

            if (Application.platform == RuntimePlatform.Android)
            {
                int       xpos         = 10 + offsetX;
                int       ypos         = 50 + offsetY;
                const int spacing      = 60;
                const int spacingSmall = 24;
                const int buttonHeight = 55;

                bool noConnection = manager.client == null || manager.client.connection == null ||
                                    manager.client.connection.connectionId == -1;

                BluetoothMultiplayerMode bluetoothMultiplayerMode = AndroidBluetoothMultiplayer.GetCurrentMode();
                if (bluetoothMultiplayerMode == BluetoothMultiplayerMode.None && !manager.IsClientConnected() && !NetworkServer.active && manager.matchMaker == null)
                {
                    if (noConnection)
                    {
                        if (GUI.Button(new Rect(xpos, ypos, 200, buttonHeight), "Bluetooth Client(C)"))
                        {
                            managerHelper.StartClient();
                        }

                        ypos += spacing;

                        if (GUI.Button(new Rect(xpos, ypos, 200, buttonHeight), "Bluetooth Host(H)"))
                        {
                            managerHelper.StartHost();
                        }
                        ypos += spacing;

                        if (GUI.Button(new Rect(xpos, ypos, 200, buttonHeight), "Bluetooth Server Only(S)"))
                        {
                            managerHelper.StartServer();
                        }
                        ypos += spacing;
                    }
                    else
                    {
                        GUI.Label(new Rect(xpos, ypos, 200, buttonHeight), "Connecting to " + manager.networkAddress + ":" + manager.networkPort + "..");
                        ypos += spacingSmall;

                        if (GUI.Button(new Rect(xpos, ypos, 200, buttonHeight), "Cancel Connection Attempt"))
                        {
                            manager.StopClient();
                        }
                    }
                }
                else
                {
                    if (NetworkServer.active)
                    {
                        string serverMsg = "Server: port=" + manager.networkPort;
                        GUI.Label(new Rect(xpos, ypos, 300, 20), serverMsg);
                        ypos += spacingSmall;
                    }
                    if (manager.IsClientConnected())
                    {
                        GUI.Label(new Rect(xpos, ypos, 300, 20), "Client: address=" + manager.networkAddress + " port=" + manager.networkPort);
                        ypos += spacingSmall;
                    }
                }

                if (manager.IsClientConnected() && !ClientScene.ready)
                {
                    if (GUI.Button(new Rect(xpos, ypos, 200, buttonHeight), "Client Ready"))
                    {
                        ClientScene.Ready(manager.client.connection);

                        if (ClientScene.localPlayers.Count == 0)
                        {
                            ClientScene.AddPlayer(0);
                        }
                    }
                    ypos += spacing;
                }

                if (NetworkServer.active || manager.IsClientConnected())
                {
                    if (GUI.Button(new Rect(xpos, ypos, 200, buttonHeight), "Stop (X)"))
                    {
                        manager.StopHost();
                    }
                    ypos += spacing;
                }
                else if (bluetoothMultiplayerMode != BluetoothMultiplayerMode.None)
                {
                    if (bluetoothMultiplayerMode == BluetoothMultiplayerMode.Client)
                    {
                        GUI.Label(new Rect(xpos, ypos, 300, 20), "Connecting to Bluetooth server...");
                        ypos += spacingSmall;
                    }
                    else if (bluetoothMultiplayerMode == BluetoothMultiplayerMode.Server)
                    {
                        GUI.Label(new Rect(xpos, ypos, 300, 20), "Starting Bluetooth server...");
                        ypos += spacingSmall;
                    }
                    if (GUI.Button(new Rect(xpos, ypos, 200, buttonHeight), "Stop (X)"))
                    {
                        AndroidBluetoothMultiplayer.Stop();
                    }
                    ypos += spacing;
                }
            }
            else
            {
                BaseOnGUI();
            }
#else
            BaseOnGUI();
#endif
        }
Ejemplo n.º 19
0
 private void ClearState()
 {
     _desiredMode  = BluetoothMultiplayerMode.None;
     _clientAction = null;
     _hostAction   = null;
 }
Ejemplo n.º 20
0
	public void StartBluetoothHost(){
		if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled ()) {
			AndroidBluetoothMultiplayer.RequestEnableDiscoverability (120);
			//Network.Disconnect (); // Just to be sure
			AndroidBluetoothMultiplayer.StartServer (kPort);
		} else {
			// Otherwise we have to enable Bluetooth first and wait for callback
			_desiredMode = BluetoothMultiplayerMode.Server;
			AndroidBluetoothMultiplayer.RequestEnableDiscoverability (120);
		}
	}
Ejemplo n.º 21
0
    public void Init()
    {
        app = App.Instance;
        switch (app.controller.gameMode.mode)
        {
        case GameModeController.Mode.multiplayer_bluetooth_client:
            ServerClientMultiplayerWindow.SetActive(true);
            ServerClinetInfo.text = "Connecting to server...";
            break;

        case GameModeController.Mode.multiplayer_bluetooth_server:
            ServerClientMultiplayerWindow.SetActive(true);
            ServerClinetInfo.text = "Waiting for player...";
            break;
        }

        homeButton.onClick.AddListener(Home);
        // Setting the UUID. Must be unique for every application
        _initResult = AndroidBluetoothMultiplayer.Initialize("8ce255c0-200a-11e0-ac64-0800200c9a66");

        // Enabling verbose logging. See log cat!
        AndroidBluetoothMultiplayer.SetVerboseLog(true);

        // Registering the event delegates
        AndroidBluetoothMultiplayer.ListeningStarted            += OnBluetoothListeningStarted;
        AndroidBluetoothMultiplayer.ListeningStopped            += OnBluetoothListeningStopped;
        AndroidBluetoothMultiplayer.AdapterEnabled              += OnBluetoothAdapterEnabled;
        AndroidBluetoothMultiplayer.AdapterEnableFailed         += OnBluetoothAdapterEnableFailed;
        AndroidBluetoothMultiplayer.AdapterDisabled             += OnBluetoothAdapterDisabled;
        AndroidBluetoothMultiplayer.DiscoverabilityEnabled      += OnBluetoothDiscoverabilityEnabled;
        AndroidBluetoothMultiplayer.DiscoverabilityEnableFailed += OnBluetoothDiscoverabilityEnableFailed;
        AndroidBluetoothMultiplayer.ConnectedToServer           += OnBluetoothConnectedToServer;
        AndroidBluetoothMultiplayer.ConnectionToServerFailed    += OnBluetoothConnectionToServerFailed;
        AndroidBluetoothMultiplayer.DisconnectedFromServer      += OnBluetoothDisconnectedFromServer;
        AndroidBluetoothMultiplayer.ClientConnected             += OnBluetoothClientConnected;
        AndroidBluetoothMultiplayer.ClientDisconnected          += OnBluetoothClientDisconnected;
        AndroidBluetoothMultiplayer.DevicePicked += OnBluetoothDevicePicked;

        int modeIndex = SaveDataController.Instance.Data.mode;

        switch (modeIndex)
        {
        case 1:
            this.mode = Mode.single;
            break;

        case 2:
            this.mode = Mode.multiplayer_local;
            break;

        case 3:
            this.mode = Mode.multiplayer_bluetooth_server;
            break;

        case 4:
            this.mode = Mode.multiplayer_bluetooth_client;
            break;
        }

        float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();

        // If initialization was successfull, showing the buttons
        if (_initResult)
        {
            // If there is no current Bluetooth connectivity
            BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();
            if (currentMode == BluetoothMultiplayerMode.None)
            {
                if (mode == Mode.multiplayer_bluetooth_server)
                {
                    // If Bluetooth is enabled, then we can do something right on
                    if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
                    {
                        AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                        Network.Disconnect(); // Just to be sure
                        AndroidBluetoothMultiplayer.StartServer(kPort);
                    }
                    else
                    {
                        // Otherwise we have to enable Bluetooth first and wait for callback
                        _desiredMode = BluetoothMultiplayerMode.Server;
                        AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                    }
                    Debug.Log("Server");
                }

                if (mode == Mode.multiplayer_bluetooth_client)
                {
                    // If Bluetooth is enabled, then we can do something right on
                    if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
                    {
                        Network.Disconnect();                         // Just to be sure
                        AndroidBluetoothMultiplayer.ShowDeviceList(); // Open device picker dialog
                    }
                    else
                    {
                        // Otherwise we have to enable Bluetooth first and wait for callback
                        _desiredMode = BluetoothMultiplayerMode.Client;
                        AndroidBluetoothMultiplayer.RequestEnableBluetooth();
                    }
                    Debug.Log("Client");
                }
            }
            else
            {
                Debug.Log("Bluetooth activated before Init");
            }

            /*if (currentMode == BluetoothMultiplayerMode.None)
             * {
             *  OnBackToMenu();
             * }
             * else
             * {
             *  started = true;
             * }*/
            started = true;
            //SetupGame();
        }
        else
        {
            // Show a message if initialization failed for some reason
            Debug.Log("Bluetooth not available. Are you running this on Bluetooth-capable " +
                      "Android device and AndroidManifest.xml is set up correctly?");
        }
    }
Ejemplo n.º 22
0
        // Update is called once per frame
        private void OnGUI()
        {
            float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();

            // If initialization was successfull, showing the buttons
            if (_initResult)
            {
                // Show log
                float logY = 80f;
                if (_receivedTexture != null)
                {
                    logY += kTextureSize;
                }

                GUILayout.Space(logY);
                BluetoothExamplesTools.TouchScroll(ref _logPosition);
                _logPosition = GUILayout.BeginScrollView(
                    _logPosition,
                    GUILayout.MaxHeight(Screen.height / scaleFactor - logY),
                    GUILayout.MinWidth(Screen.width / scaleFactor - 10f / scaleFactor),
                    GUILayout.ExpandHeight(false));
                GUI.contentColor = Color.black;
                GUILayout.Label(_log, GUILayout.ExpandHeight(true), GUILayout.MaxWidth(Screen.width / scaleFactor));
                GUI.contentColor = Color.white;
                GUILayout.EndScrollView();

                // If there is no current Bluetooth connectivity
                BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();
                if (currentMode == BluetoothMultiplayerMode.None)
                {
                    if (GUI.Button(new Rect(10, 10, 150, 50), "Create server"))
                    {
                        // If Bluetooth is enabled, then we can do something right on
                        if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
                        {
                            AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                            Network.Disconnect(); // Just to be sure
                            AndroidBluetoothMultiplayer.StartServer(kPort);
                        }
                        else
                        {
                            // Otherwise we have to enable Bluetooth first and wait for callback
                            _desiredMode = BluetoothMultiplayerMode.Server;
                            AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                        }
                    }

                    if (GUI.Button(new Rect(170, 10, 150, 50), "Connect to server"))
                    {
                        // If Bluetooth is enabled, then we can do something right on
                        if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
                        {
                            Network.Disconnect();                         // Just to be sure
                            AndroidBluetoothMultiplayer.ShowDeviceList(); // Open device picker dialog
                        }
                        else
                        {
                            // Otherwise we have to enable Bluetooth first and wait for callback
                            _desiredMode = BluetoothMultiplayerMode.Client;
                            AndroidBluetoothMultiplayer.RequestEnableBluetooth();
                        }
                    }
                }
                else
                {
                    // Stop all networking
                    if (GUI.Button(new Rect(10, 10, 150, 50), currentMode == BluetoothMultiplayerMode.Client ? "Disconnect" : "Stop server"))
                    {
                        if (Network.peerType != NetworkPeerType.Disconnected)
                        {
                            Network.Disconnect();
                        }
                    }

                    // Display file transfer UI
                    if (Network.peerType != NetworkPeerType.Disconnected && (Network.isClient || Network.connections.Length > 0) && _fileTransfer != null)
                    {
                        GUI.enabled = _fileTransfer.TransferState == FileTransfer.FileTransferState.None;
                        if (GUI.Button(new Rect(Screen.width / scaleFactor - 160, 10, 150, 50), "Send file"))
                        {
                            Color32[] image = GenerateTexture(kTextureSize);

                            byte[] colorBytes = Color32ArrayToByteArray(image);
                            _fileTransfer.TransmitFile(colorBytes);
                        }
                        GUI.enabled = true;

                        // Display file transfer status
                        string status = null;
                        switch (_fileTransfer.TransferState)
                        {
                        case FileTransfer.FileTransferState.Receiving:
                            status =
                                string.Format("Receiving: {0:F1}% ({1} out of {2} bytes)",
                                              _transferSize / (float)_transferTotalSize * 100f,
                                              _transferSize,
                                              _transferTotalSize);
                            break;

                        case FileTransfer.FileTransferState.Transmitting:
                            status =
                                string.Format("Transmitting: {0:F1}% ({1} out of {2} bytes)",
                                              _transferSize / (float)_transferTotalSize * 100f,
                                              _transferSize,
                                              _transferTotalSize);
                            break;

                        case FileTransfer.FileTransferState.None:
                            status = "Idle.";
                            break;
                        }

                        GUI.contentColor = Color.black;
                        GUI.Label(new Rect(10, 65, Screen.width, 20), status);

                        GUI.contentColor = Color.white;
                        if (_receivedTexture != null)
                        {
                            GUI.Label(new Rect(10, 80, kTextureSize, kTextureSize), _receivedTexture);
                        }
                    }
                }
            }
            else
            {
                // Show a message if initialization failed for some reason
                GUI.contentColor = Color.black;
                GUI.Label(
                    new Rect(10, 10, Screen.width / scaleFactor - 10, 50),
                    "Bluetooth not available. Are you running this on Bluetooth-capable " +
                    "Android device and AndroidManifest.xml is set up correctly?");
            }

            DrawBackButton(scaleFactor);
        }
        private void OnBluetoothAdapterEnabled() {
            Debug.Log("Event - AdapterEnabled");

            // Resuming desired action after enabling the adapter
            switch (_desiredMode) {
                case BluetoothMultiplayerMode.Server:
                    Network.Disconnect();
                    AndroidBluetoothMultiplayer.StartServer(kPort);
                    break;
                case BluetoothMultiplayerMode.Client:
                    Network.Disconnect();
                    AndroidBluetoothMultiplayer.ShowDeviceList();
                    break;
            }

            _desiredMode = BluetoothMultiplayerMode.None;
        }
    private void onBluetoothAdapterEnabled()
    {
        switch (desiredMode)
        {
            case BluetoothMultiplayerMode.Server:
                Network.Disconnect();
                BluetoothMultiplayerAndroid.InitializeServer(GameConfig.Port);
                break;
            case BluetoothMultiplayerMode.Client:
                Network.Disconnect();
                BluetoothMultiplayerAndroid.ShowDeviceList();
                break;
        }

        desiredMode = BluetoothMultiplayerMode.None;
    }
	public void StartClientBluetooth(){
		if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled()) {
			Network.Disconnect(); // Just to be sure
			AndroidBluetoothMultiplayer.ShowDeviceList(); // Open device picker dialog
		} else {
			// Otherwise we have to enable Bluetooth first and wait for callback
			_desiredMode = BluetoothMultiplayerMode.Client;
			AndroidBluetoothMultiplayer.RequestEnableBluetooth();
		}
	}
        // Update is called once per frame
        private void OnGUI() {
            float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();

            // If initialization was successfull, showing the buttons
            if (_initResult) {
                // Show log
                float logY = 80f;
                if (_receivedTexture != null)
                    logY += kTextureSize;

                GUILayout.Space(logY);
                BluetoothExamplesTools.TouchScroll(ref _logPosition);
                _logPosition = GUILayout.BeginScrollView(
                    _logPosition,
                    GUILayout.MaxHeight(Screen.height / scaleFactor - logY),
                    GUILayout.MinWidth(Screen.width / scaleFactor - 10f / scaleFactor),
                    GUILayout.ExpandHeight(false));
                GUI.contentColor = Color.black;
                GUILayout.Label(_log, GUILayout.ExpandHeight(true), GUILayout.MaxWidth(Screen.width / scaleFactor));
                GUI.contentColor = Color.white;
                GUILayout.EndScrollView();

                // If there is no current Bluetooth connectivity
                BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();
                if (currentMode == BluetoothMultiplayerMode.None) {
                    if (GUI.Button(new Rect(10, 10, 150, 50), "Create server")) {
                        // If Bluetooth is enabled, then we can do something right on
                        if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled()) {
                            AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                            Network.Disconnect(); // Just to be sure
                            AndroidBluetoothMultiplayer.StartServer(kPort);
                        } else {
                            // Otherwise we have to enable Bluetooth first and wait for callback
                            _desiredMode = BluetoothMultiplayerMode.Server;
                            AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                        }
                    }

                    if (GUI.Button(new Rect(170, 10, 150, 50), "Connect to server")) {
                        // If Bluetooth is enabled, then we can do something right on
                        if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled()) {
                            Network.Disconnect(); // Just to be sure
                            AndroidBluetoothMultiplayer.ShowDeviceList(); // Open device picker dialog
                        } else {
                            // Otherwise we have to enable Bluetooth first and wait for callback
                            _desiredMode = BluetoothMultiplayerMode.Client;
                            AndroidBluetoothMultiplayer.RequestEnableBluetooth();
                        }
                    }
                } else {
                    // Stop all networking
                    if (GUI.Button(new Rect(10, 10, 150, 50), currentMode == BluetoothMultiplayerMode.Client ? "Disconnect" : "Stop server")) {
                        if (Network.peerType != NetworkPeerType.Disconnected)
                            Network.Disconnect();
                    }

                    // Display file transfer UI
                    if (Network.peerType != NetworkPeerType.Disconnected && (Network.isClient || Network.connections.Length > 0) && _fileTransfer != null) {
                        GUI.enabled = _fileTransfer.TransferState == FileTransfer.FileTransferState.None;
                        if (GUI.Button(new Rect(Screen.width / scaleFactor - 160, 10, 150, 50), "Send file")) {
                            Color32[] image = GenerateTexture(kTextureSize);

                            byte[] colorBytes = Color32ArrayToByteArray(image);
                            _fileTransfer.TransmitFile(colorBytes);
                        }
                        GUI.enabled = true;

                        // Display file transfer status
                        string status = null;
                        switch (_fileTransfer.TransferState) {
                            case FileTransfer.FileTransferState.Receiving:
                                status =
                                    string.Format("Receiving: {0:F1}% ({1} out of {2} bytes)",
                                        _transferSize / (float) _transferTotalSize * 100f,
                                        _transferSize,
                                        _transferTotalSize);
                                break;
                            case FileTransfer.FileTransferState.Transmitting:
                                status =
                                    string.Format("Transmitting: {0:F1}% ({1} out of {2} bytes)",
                                        _transferSize / (float) _transferTotalSize * 100f,
                                        _transferSize,
                                        _transferTotalSize);
                                break;
                            case FileTransfer.FileTransferState.None:
                                status = "Idle.";
                                break;
                        }

                        GUI.contentColor = Color.black;
                        GUI.Label(new Rect(10, 65, Screen.width, 20), status);

                        GUI.contentColor = Color.white;
                        if (_receivedTexture != null) {
                            GUI.Label(new Rect(10, 80, kTextureSize, kTextureSize), _receivedTexture);
                        }
                    }
                }
            } else {
                // Show a message if initialization failed for some reason
                GUI.contentColor = Color.black;
                GUI.Label(
                    new Rect(10, 10, Screen.width / scaleFactor - 10, 50),
                    "Bluetooth not available. Are you running this on Bluetooth-capable " +
                    "Android device and AndroidManifest.xml is set up correctly?");
            }

            DrawBackButton(scaleFactor);
        }
    void OnGUI()
    {
        // If initialization was successfull, showing the buttons
        if (_initResult)
        {
            // If there is no current Bluetooth connectivity
            if (BluetoothMultiplayerAndroid.CurrentMode() == BluetoothMultiplayerMode.None)
            {
                if (GUI.Button(new Rect(10, 10, 150, 50), "Create server"))
                {
                    // If Bluetooth is enabled, then we can do something right on
                    if (BluetoothMultiplayerAndroid.IsBluetoothEnabled())
                    {
                        Network.Disconnect(); // Just to be sure
                        BluetoothMultiplayerAndroid.InitializeServer(Port);
                    }
                    // Otherwise we have to enable Bluetooth first and wait for callback
                    else
                    {
                        _desiredMode = BluetoothMultiplayerMode.Server;
                        BluetoothMultiplayerAndroid.RequestBluetoothEnable();
                    }
                }

                if (GUI.Button(new Rect(170, 10, 150, 50), "Connect to server"))
                {
                    // If Bluetooth is enabled, then we can do something right on
                    if (BluetoothMultiplayerAndroid.IsBluetoothEnabled())
                    {
                        Network.Disconnect();                         // Just to be sure
                        BluetoothMultiplayerAndroid.ShowDeviceList(); // Open device picker dialog
                    }
                    // Otherwise we have to enable Bluetooth first and wait for callback
                    else
                    {
                        _desiredMode = BluetoothMultiplayerMode.Client;
                        BluetoothMultiplayerAndroid.RequestBluetoothEnable();
                    }
                }
            }
            else
            {
                // Stop all networking
                if (GUI.Button(new Rect(10, 10, 150, 50), "Disconnect"))
                {
                    if (Network.peerType != NetworkPeerType.Disconnected)
                    {
                        Network.Disconnect();
                    }
                }
            }
            // Showing message if initialization failed for some reason
        }
        else
        {
            GUI.Label(new Rect(10, 10, Screen.width, 100), "Bluetooth not available. Are you running this on Bluetooth-capable Android device and AndroidManifest.xml is set up correctly?");
        }

        if (GUI.Button(new Rect(Screen.width - 100f - 15f, Screen.height - 40f - 15f, 100f, 40f), "Back"))
        {
            // Gracefully closing all Bluetooth connectivity and loading the menu
            try {
                BluetoothMultiplayerAndroid.StopDiscovery();
                BluetoothMultiplayerAndroid.Disconnect();
            } catch {
                //
            }
            Application.LoadLevel("BluetoothMultiplayerAndroidDemoMenu");
        }
    }
        private void OnGUI()
        {
            float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();

            // If initialization was successfull, showing the buttons
            if (_initResult)
            {
                // If there is no current Bluetooth connectivity
                BluetoothMultiplayerMode currentMode = AndroidBluetoothMultiplayer.GetCurrentMode();
                if (currentMode == BluetoothMultiplayerMode.None)
                {
                    if (GUI.Button(new Rect(10, 10, 150, 50), "Create server"))
                    {
                        // If Bluetooth is enabled, then we can do something right on
                        if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
                        {
                            AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                            Network.Disconnect(); // Just to be sure
                            AndroidBluetoothMultiplayer.StartServer(kPort);
                        }
                        else
                        {
                            // Otherwise we have to enable Bluetooth first and wait for callback
                            _desiredMode = BluetoothMultiplayerMode.Server;
                            AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                        }
                    }

                    if (GUI.Button(new Rect(170, 10, 150, 50), "Connect to server"))
                    {
                        // If Bluetooth is enabled, then we can do something right on
                        if (AndroidBluetoothMultiplayer.GetIsBluetoothEnabled())
                        {
                            Network.Disconnect();                         // Just to be sure
                            AndroidBluetoothMultiplayer.ShowDeviceList(); // Open device picker dialog
                        }
                        else
                        {
                            // Otherwise we have to enable Bluetooth first and wait for callback
                            _desiredMode = BluetoothMultiplayerMode.Client;
                            AndroidBluetoothMultiplayer.RequestEnableBluetooth();
                        }
                    }
                }
                else
                {
                    // Stop all networking
                    if (GUI.Button(new Rect(10, 10, 150, 50), currentMode == BluetoothMultiplayerMode.Client ? "Disconnect" : "Stop server"))
                    {
                        if (Network.peerType != NetworkPeerType.Disconnected)
                        {
                            Network.Disconnect();
                        }

                        AndroidBluetoothMultiplayer.Stop();
                    }
                }

                GUI.contentColor    = Color.black;
                GUI.enabled         = currentMode == BluetoothMultiplayerMode.None;
                _isStressTestMode   = GUI.Toggle(new Rect(10, 70, 250, 30), _isStressTestMode, " Stress-test mode (" + kStressModeActors + " actors)");
                _isUseInterpolation = GUI.Toggle(new Rect(10, 100, 250, 30), _isUseInterpolation, " Use interpolation (smoothen movement)");
                GUI.enabled         = true;
                GUI.contentColor    = Color.white;
            }
            else
            {
                // Show a message if initialization failed for some reason
                GUI.contentColor = Color.black;
                GUI.Label(
                    new Rect(10, 10, Screen.width / scaleFactor - 10, 50),
                    "Bluetooth not available. Are you running this on Bluetooth-capable " +
                    "Android device and AndroidManifest.xml is set up correctly?");
            }

            DrawBackButton(scaleFactor);
        }
Ejemplo n.º 29
0
 public void disconnectNetWork()
 {
     Network.Disconnect();
     desiredMode = BluetoothMultiplayerMode.None;
 }