Exemple #1
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
        }
Exemple #2
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;
        }
        public override void OnStopClient()
        {
            base.OnStopClient();

#if UNITY_ANDROID
            // Stopping all Bluetooth connectivity on Unity networking disconnect event
            AndroidBluetoothMultiplayer.Stop();
#endif
        }
Exemple #4
0
        protected virtual void OnBluetoothListeningStopped()
        {
            if (_bluetoothNetworkManagerSettings.LogBluetoothEvents)
            {
                Debug.Log("Bluetooth Event - ListeningStopped");
            }

            if (_bluetoothNetworkManagerSettings.StopBluetoothServerOnListeningStopped)
            {
                AndroidBluetoothMultiplayer.Stop();
            }
        }
Exemple #5
0
        protected virtual void OnBluetoothDevicePicked(BluetoothDevice device)
        {
            if (_bluetoothNetworkManagerSettings.LogBluetoothEvents)
            {
                Debug.Log("Bluetooth Event - DevicePicked: " + device);
            }

            if (_customDeviceBrowser != null)
            {
                _customDeviceBrowser.Close();
            }

            // Trying to connect to the device picked by user
            AndroidBluetoothMultiplayer.Connect(device.Address, (ushort)_networkManager.networkPort);
        }
        /// <summary>
        /// Tries to find an existing instance in the scene, 
        /// and creates one if there were none.
        /// </summary>
        private static void UpdateInstance() {
            if (_instance != null)
                return;

            // Trying to find an existing instance in the scene
            _instance = (AndroidBluetoothMultiplayer) FindObjectOfType(typeof(AndroidBluetoothMultiplayer));

            // Creating a new instance in case there are no instances present in the scene
            if (_instance != null)
                return;

            GameObject gameObject = new GameObject(kGameObjectName);
            _instance = gameObject.AddComponent<AndroidBluetoothMultiplayer>();

            // Make it hidden and indestructible
            gameObject.hideFlags = HideFlags.NotEditable | HideFlags.HideInHierarchy;
        }
Exemple #7
0
        private void Awake()
        {
            // Kill other instances
            if (FindObjectsOfType(typeof(AndroidBluetoothMultiplayer)).Length > 1)
            {
                Debug.LogError("Multiple " + kGameObjectName + " instances found, destroying...");
                DestroyImmediate(gameObject);
                return;
            }

            _instance = this;

            // Set the GameObject name to the class name for UnitySendMessage
            gameObject.name = kGameObjectName;

            // We want this object to persist across scenes
            DontDestroyOnLoad(gameObject);
            DontDestroyOnLoad(this);
        }
Exemple #8
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
        }
Exemple #9
0
        protected virtual void OnEnable()
        {
            _networkManager = GetComponent <NetworkManager>();

            // Setting the UUID. Must be unique for every application
            _isInitialized = AndroidBluetoothMultiplayer.Initialize(_bluetoothNetworkManagerSettings.Uuid);

            // Registering the event listeners
            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;
        }
Exemple #10
0
        /// <summary>
        /// Tries to find an existing instance in the scene,
        /// and creates one if there were none.
        /// </summary>
        private static void UpdateInstance()
        {
            if (_instance != null)
            {
                return;
            }

            // Trying to find an existing instance in the scene
            _instance = (AndroidBluetoothMultiplayer)FindObjectOfType(typeof(AndroidBluetoothMultiplayer));

            // Creating a new instance in case there are no instances present in the scene
            if (_instance != null)
            {
                return;
            }

            GameObject gameObject = new GameObject(kGameObjectName);

            _instance = gameObject.AddComponent <AndroidBluetoothMultiplayer>();

            // Make it hidden and indestructible
            gameObject.hideFlags = HideFlags.NotEditable | HideFlags.HideInHierarchy;
        }
        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
        }
Exemple #12
0
 protected void StopAll()
 {
     AndroidBluetoothMultiplayer.Stop();
     _networkManager.StopHost();
 }
        private void Awake() {
            // Kill other instances
            if (FindObjectsOfType(typeof(AndroidBluetoothMultiplayer)).Length > 1) {
                Debug.LogError("Multiple " + kGameObjectName + " instances found, destroying...");
                DestroyImmediate(gameObject);
                return;
            }

            _instance = this;

            // Set the GameObject name to the class name for UnitySendMessage
            gameObject.name = kGameObjectName;

            // We want this object to persist across scenes
            DontDestroyOnLoad(gameObject);
            DontDestroyOnLoad(this);
        }