Esempio n. 1
0
    IEnumerator blinkLED()
    {
        byte[] turn_on  = new byte[] { (byte)'E' /*E stands for enable */, 2 };
        byte[] turn_off = new byte[] { (byte)'D' /*D stands for disable */, 2 };
        x += BTHelper.isConnected().ToString();

        while (BTHelper.isConnected())
        {
            Debug.Log("ON");
            for (byte i = 2; i < 8; i++)
            {
                turn_on[1] = i;
                try{
                    BTHelper.SendData(turn_on);
                }catch (Exception) {}
                yield return(new WaitForSeconds(0.3f));
            }
            Debug.Log("OFF");
            for (byte i = 2; i < 8; i++)
            {
                turn_off[1] = i;
                try{
                    BTHelper.SendData(turn_off);
                }catch (Exception) {}
                yield return(new WaitForSeconds(0.3f));
            }
        }
    }
Esempio n. 2
0
    //Call this function to emulate message receiving from bluetooth while debugging on your PC.
    void OnGUI()
    {
        if (isDebugOn)
        {
            if (bluetoothHelper != null)
            {
                bluetoothHelper.DrawGUI();
            }
            else
            {
                return;
            }

            if (!bluetoothHelper.isConnected())
            {
                Btn_Connect.interactable    = true;
                Btn_Disconnect.interactable = false;
                Toggle_isConnected.isOn     = false;
            }

            if (bluetoothHelper.isConnected())
            {
                Btn_Connect.interactable    = false;
                Btn_Disconnect.interactable = true;
                Toggle_isConnected.isOn     = true;
            }

            // Screen Debug
            GUIStyle myStyle = new GUIStyle();
            myStyle.fontSize         = 16;
            myStyle.normal.textColor = Color.blue;
            GUI.Label(new Rect(10, 100, 1080, 1920), myLog, myStyle);
        }
    }
Esempio n. 3
0
    //Call this function to emulate message receiving from bluetooth while debugging on your PC.
    void OnGUI()
    {
        if (bluetoothHelper != null)
        {
            bluetoothHelper.DrawGUI();
        }
        else
        {
            return;
        }

        if (bluetoothHelper.isConnected())
        {
            if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
            {
                bluetoothHelper.Disconnect();
                sphere.GetComponent <Renderer>().material.color = Color.blue;
            }
        }

        if (bluetoothHelper.isConnected())
        {
            if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Send text"))
            {
                //bluetoothHelper.SendData(new byte[] { 0, 1, 2, 3, 4 });
                bluetoothHelper.SendData("Hi From unity");
            }
        }
    }
Esempio n. 4
0
    /// <summary>
    /// //////////////////////Update/////////////////////////////////
    /// </summary>
    void Update()
    {
        if (bluetoothHelper == null)
        {
            return;
        }

        if (bluetoothHelper.isConnected())
        {
            connectedButtons.gameObject.SetActive(true);
            disconnectedButtons.gameObject.SetActive(false);
        }

        if (!bluetoothHelper.isConnected())
        {
            connectedButtons.gameObject.SetActive(false);
            disconnectedButtons.gameObject.SetActive(true);
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (bluetoothHelper != null)
            {
                bluetoothHelper.Disconnect();
            }
            Application.Quit();
        }
    }
Esempio n. 5
0
 void ConnectBT()
 {
     if (!bluetoothHelper.isConnected())
     {
         Middletext.text = "Disconnected";
         if (bluetoothHelper.isDevicePaired())
         {
             bluetoothHelper.Connect(); // tries to connect
         }
     }
 }
Esempio n. 6
0
    //Call this function to emulate message receiving from bluetooth while debugging on your PC.
    void OnGUI()
    {
        tmp = GUI.TextField(new Rect(Screen.width / 4, Screen.height / 10, Screen.width / 2, Screen.height / 10 - 10), tmp);

        if (bluetoothHelper != null)
        {
            bluetoothHelper.DrawGUI();
        }
        else
        {
            return;
        }

        if (!bluetoothHelper.isConnected())
        {
            if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Connect"))
            {
                if (bluetoothHelper.isDevicePaired())
                {
                    bluetoothHelper.Connect(); // tries to connect
                }
                else
                {
                    write("Cannot connect, device is not found, for bluetooth classic, pair the device\n\tFor BLE scan for nearby devices");
                }
            }
        }

        if (bluetoothHelper.isConnected())
        {
            if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
            {
                bluetoothHelper.Disconnect();
                write("Disconnected");
            }

            if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Turn On"))
            {
                bluetoothHelper.SendData("O");
                write("Sending O");
            }

            if (GUI.Button(new Rect(Screen.width / 2 + Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Turn Off"))
            {
                bluetoothHelper.SendData("F");
                write("Sending F");
            }
        }
    }
Esempio n. 7
0
    // Update is called once per frame
    void Update()
    {
        if (BTHelper != null)
        {
            if (BTHelper.isConnected())
            {
                /* 看最後用哪個程式碼 */
                Speed = PlayerMovement.RPM;
                string msg = Speed.ToString();

                BTHelper.SendData(msg);
                //print("Speed: " + Speed);

                /* Determine whether to enter the flower area */
                if (isFlower)
                {
                    BTHelper.SendData("on");
                }
                else
                {
                    BTHelper.SendData("off");
                }
            }
        }
    }
Esempio n. 8
0
    //Call this function to emulate message receiving from bluetooth while debugging on your PC.
    void OnGUI()
    {
        if (bluetoothHelper != null)
        {
            bluetoothHelper.DrawGUI();
        }
        else
        {
            return;
        }

        if (!bluetoothHelper.isConnected())
        {
            if (GUI.Button(new Rect(0, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Connect"))
            {
                if (bluetoothHelper.isDevicePaired())
                {
                    bluetoothHelper.Connect(); // tries to connect
                }
            }
        }

        //if (bluetoothHelper.isConnected())
        //    if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "SetActive"))
        //    {
        //        Camera.SetActive(true);
        //    }
    }
Esempio n. 9
0
    //Call this function to emulate message receiving from bluetooth while debugging on your PC.
    void OnGUI()
    {
        if (bluetoothHelper != null)
        {
            bluetoothHelper.DrawGUI();
        }
        else
        {
            return;
        }

        if (!bluetoothHelper.isConnected())
        {
            if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Connect"))
            {
                if (bluetoothHelper.isDevicePaired())
                {
                    bluetoothHelper.Connect();              // tries to connect
                }
                else
                {
                    sphere.GetComponent <Renderer>().material.color = Color.magenta;
                }
            }
        }

        if (bluetoothHelper.isConnected())
        {
            if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height - 2 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
            {
                bluetoothHelper.Disconnect();
                sphere.GetComponent <Renderer>().material.color = Color.blue;
            }
        }

        if (bluetoothHelper.isConnected())
        {
            if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Send text"))
            {
                bluetoothHelper.SendData(new Byte[] { 0, 0, 85, 0, 85 });
                // bluetoothHelper.SendData("This is a very long long long long text");
            }
        }
    }
Esempio n. 10
0
 public void button()
 {
     if (!bluetoothHelper.isConnected())
     {
         if (bluetoothHelper.isDevicePaired())
         {
             bluetoothHelper.Connect(); // tries to connect
         }
     }
 }
Esempio n. 11
0
 void OnGUI()
 {
     if (helper == null)
     {
         return;
     }
     if (!helper.isConnected() && !isScanning && !isConnecting)
     {
         if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Start Scanning"))
         {
             isScanning = helper.ScanNearbyDevices();
         }
         if (devices != null && devices.First != null)
         {
             draw();
         }
     }
     else if (!helper.isConnected() && isScanning)
     {
         GUI.TextArea(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Scanning...");
     }
     else if (helper.isConnected())
     {
         GUI.TextArea(new Rect(Screen.width / 4, 2 * Screen.height / 10, Screen.width / 2, 7 * Screen.height / 10), data);
         tmp = GUI.TextField(new Rect(Screen.width / 4, Screen.height / 10, Screen.width / 2, Screen.height / 10 - 10), tmp);
         if (GUI.Button(new Rect(3 * Screen.width / 4 + 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Send"))
         {
             helper.SendData(tmp);
             data += "\n>" + tmp;
             tmp   = "";
         }
         if (GUI.Button(new Rect(3 * Screen.width / 4 + 10, 8 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
         {
             helper.Disconnect();
         }
     }
 }
Esempio n. 12
0
    void Update()
    {
        if (bluetoothHelper == null)
        {
            return;
        }
        if (!bluetoothHelper.isConnected())
        {
            return;
        }
        timer += Time.deltaTime;

        if (timer < 5)
        {
            return;
        }
        timer = 0;
        sendData();
    }