Exemple #1
0
        private void OnBluetoothDevicePicked(BluetoothDevice device)
        {
            Debug.Log("Event - DevicePicked: " + BluetoothExamplesTools.FormatDevice(device));

            // Trying to connect to a device user had picked
            AndroidBluetoothMultiplayer.Connect(device.Address, kPort);
        }
Exemple #2
0
        private void OnBluetoothDisconnectedFromServer(BluetoothDevice device)
        {
            Debug.Log("Event - DisconnectedFromServer: " + BluetoothExamplesTools.FormatDevice(device));

            // Stopping Unity networking on Bluetooth failure
            Network.Disconnect();
        }
        protected void GoBackToMenu()
        {
#if UNITY_ANDROID
            OnGoingBackToMenu();
#endif
            CameraFade.StartAlphaFade(Color.black, false, 0.25f, 0f, () => BluetoothExamplesTools.LoadLevel("BluetoothDemoMenu"));
        }
Exemple #4
0
        private void OnBluetoothConnectedToServer(BluetoothDevice device)
        {
            Debug.Log("Event - ConnectedToServer: " + BluetoothExamplesTools.FormatDevice(device));

            // Trying to negotiate a Unity networking connection,
            // when Bluetooth client connected successfully
            Network.Connect(kLocalIp, kPort);
        }
Exemple #5
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);
                        }
                    }
                }
            }
        }
        private void OnGUI()
        {
            const float width        = 250f;
            const float buttonHeight = 35f;
            const float height       = 100f + buttonHeight * 3f;

            float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();

            GUI.color = Color.white;
            GUILayout.BeginArea(
                new Rect(
                    Screen.width / 2f / scaleFactor - width / 2f,
                    Screen.height / 2f / scaleFactor - height / 2f,
                    width,
                    height),
                "Android Bluetooth Multiplayer Demo",
                "Window");
            GUILayout.BeginVertical();

            GUILayout.Space(10);
            if (GUILayout.Button("Basic multiplayer", GUILayout.Height(buttonHeight)))
            {
                CameraFade.StartAlphaFade(Color.black, false, 0.25f, 0f, () => Application.LoadLevel("BluetoothMultiplayerDemo"));
            }

            if (GUILayout.Button("Device discovery", GUILayout.Height(buttonHeight)))
            {
                CameraFade.StartAlphaFade(Color.black, false, 0.25f, 0f, () => Application.LoadLevel("BluetoothDiscoveryDemo"));
            }

            if (GUILayout.Button("Basic RPC file transfer", GUILayout.Height(buttonHeight)))
            {
                CameraFade.StartAlphaFade(Color.black, false, 0.25f, 0f, () => Application.LoadLevel("BluetoothFileTransferDemo"));
            }

            GUILayout.Space(15);
            GUI.backgroundColor = new Color(1f, 0.6f, 0.6f, 1f);
            if (GUILayout.Button("Quit", GUILayout.Height(buttonHeight)))
            {
                CameraFade.StartAlphaFade(Color.black, false, 0.25f, 0f, Application.Quit);
            }

            GUILayout.EndVertical();
            GUILayout.EndArea();
        }
        private void OnGUI()
        {
            bool isBluetoothEnabled = AndroidBluetoothMultiplayer.GetIsBluetoothEnabled();
            bool isDiscoverable     = false;
            bool isDiscovering      = false;

            try {
                isDiscoverable = isBluetoothEnabled && AndroidBluetoothMultiplayer.GetIsDiscoverable();
                isDiscovering  = isBluetoothEnabled && AndroidBluetoothMultiplayer.GetIsDiscovering();
            } catch (BluetoothNotEnabledException) {
                // This may happen in some rare cases when Bluetooth actually gets disabled
                // in the middle of C# code execution. In that case we may get a
                // BluetoothNotEnabledException, but it is safe to ignore it here.
            }

            float scaleFactor = BluetoothExamplesTools.UpdateScaleMobile();

            // Show the buttons if initialization succeeded
            if (_initResult)
            {
                // Simple text log view
                GUILayout.Space(190f);
                BluetoothExamplesTools.TouchScroll(ref _logPosition);
                _logPosition = GUILayout.BeginScrollView(
                    _logPosition,
                    GUILayout.MaxHeight(Screen.height / scaleFactor - 190f),
                    GUILayout.MinWidth(Screen.width / scaleFactor - 10f),
                    GUILayout.ExpandHeight(false));
                GUI.contentColor = Color.black;
                GUILayout.Label(_log, GUILayout.ExpandHeight(true), GUILayout.MaxWidth(Screen.width / scaleFactor));
                GUI.contentColor = Color.white;
                GUILayout.EndScrollView();

                // Generic GUI for calling the methods
                GUI.enabled = !isBluetoothEnabled;
                if (GUI.Button(new Rect(10, 10, 140, 50), "Request enable\nBluetooth"))
                {
                    AndroidBluetoothMultiplayer.RequestEnableBluetooth();
                }

                GUI.enabled = isBluetoothEnabled;
                if (GUI.Button(new Rect(160, 10, 140, 50), "Disable Bluetooth"))
                {
                    AndroidBluetoothMultiplayer.DisableBluetooth();
                }
                GUI.enabled = !isBluetoothEnabled || !isDiscoverable;
                if (GUI.Button(new Rect(310, 10, 150, 50), "Request discoverability"))
                {
                    AndroidBluetoothMultiplayer.RequestEnableDiscoverability(120);
                }

                GUI.enabled = isBluetoothEnabled && !isDiscovering;
                if (GUI.Button(new Rect(10, 70, 140, 50), "Start discovery"))
                {
                    AndroidBluetoothMultiplayer.StartDiscovery();
                }

                GUI.enabled = isBluetoothEnabled && isDiscovering;
                if (GUI.Button(new Rect(160, 70, 140, 50), "Stop discovery"))
                {
                    AndroidBluetoothMultiplayer.StopDiscovery();
                }

                GUI.enabled = isBluetoothEnabled;
                if (GUI.Button(new Rect(310, 70, 150, 50), "Get current\ndevice"))
                {
                    Debug.Log("Current device:");
                    BluetoothDevice device = AndroidBluetoothMultiplayer.GetCurrentDevice();
                    if (device != null)
                    {
                        // Result can be null on error or if Bluetooth is not available
                        Debug.Log(string.Format("Device: " + BluetoothExamplesTools.FormatDevice(device)));
                    }
                    else
                    {
                        Debug.LogError("Error while retrieving current device");
                    }
                }

                // Just get the device lists and prints them
                if (GUI.Button(new Rect(10, 130, 140, 50), "Show bonded\ndevice list"))
                {
                    Debug.Log("Listing known bonded (paired) devices");
                    BluetoothDevice[] list = AndroidBluetoothMultiplayer.GetBondedDevices();

                    if (list != null)
                    {
                        // Result can be null on error or if Bluetooth is not available
                        if (list.Length == 0)
                        {
                            Debug.Log("No devices");
                        }
                        else
                        {
                            foreach (BluetoothDevice device in list)
                            {
                                Debug.Log("Device: " + BluetoothExamplesTools.FormatDevice(device));
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Error while retrieving GetBondedDevices()");
                    }
                }

                if (GUI.Button(new Rect(160, 130, 140, 50), "Show new discovered\ndevice list"))
                {
                    Debug.Log("Listing devices discovered during last discovery session...");
                    BluetoothDevice[] list = AndroidBluetoothMultiplayer.GetNewDiscoveredDevices();

                    if (list != null)
                    {
                        // Result can be null on error or if Bluetooth is not available
                        if (list.Length == 0)
                        {
                            Debug.Log("No devices");
                        }
                        else
                        {
                            foreach (BluetoothDevice device in list)
                            {
                                Debug.Log("Device: " + BluetoothExamplesTools.FormatDevice(device));
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Error while retrieving GetNewDiscoveredDevices()");
                    }
                }

                if (GUI.Button(new Rect(310, 130, 150, 50), "Show full\ndevice list"))
                {
                    Debug.Log("Listing all known or discovered devices...");
                    BluetoothDevice[] list = AndroidBluetoothMultiplayer.GetDiscoveredDevices();

                    if (list != null)
                    {
                        // Result can be null on error or if Bluetooth is not available
                        if (list.Length == 0)
                        {
                            Debug.Log("No devices");
                        }
                        else
                        {
                            foreach (BluetoothDevice device in list)
                            {
                                Debug.Log("Device: " + BluetoothExamplesTools.FormatDevice(device));
                            }
                        }
                    }
                    else
                    {
                        Debug.LogError("Error while retrieving GetDiscoveredDevices()");
                    }
                }

                GUI.enabled = true;
                // Show a message if initialization failed for some reason
            }
            else
            {
                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);
        }
Exemple #8
0
 public void LoadLevel(string levelName)
 {
     CameraFade.StartAlphaFade(Color.black, false, 0.25f, 0f, () => BluetoothExamplesTools.LoadLevel(levelName));
 }
Exemple #9
0
 private void OnBluetoothConnectionToServerFailed(BluetoothDevice device)
 {
     Debug.Log("Event - ConnectionToServerFailed: " + BluetoothExamplesTools.FormatDevice(device));
 }
Exemple #10
0
 private void OnBluetoothClientConnected(BluetoothDevice device)
 {
     Debug.Log("Event - ClientConnected: " + BluetoothExamplesTools.FormatDevice(device));
 }
Exemple #11
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 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);
        }