Beispiel #1
0
        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);
        }