Exemple #1
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");
                }
            }
        }
    }
Exemple #2
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));
            }
        }
    }
Exemple #3
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");
            }
        }
    }
Exemple #4
0
    private void SendNext()
    {
        if (awatingMsg || bluetoothHelper == null)
        {
            return;
        }

        //Debug.Log(queue);

        int x = queue.IndexOf('|');

        if (x <= 0)
        {
            queue = "";
            return;
        }
        string msgg = queue.Substring(0, x);

        queue      = queue.Substring(x + 1);
        awatingMsg = true;

        msg.text += "Sending  " + msgg;

        bluetoothHelper.SendData(msgg);
    }
Exemple #5
0
 /// <summary>
 /// This method sends data to the bluetooth module.
 /// </summary>
 /// <param name="data"> the data that is supposed to be sent </param>
 public void SendData(string data)
 {
     if (bt != null && data != null && data != "")
     {
         bt.SendData(data);
     }
 }
Exemple #6
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");
            }
        }
    }
Exemple #7
0
 public void ButtonClick()
 {
     if (btManager.connectBT == true)
     {
         helper = btManager.GetBluetoothHelper(); //refact this to start
         helper.SendData(msg);
     }
     Debug.Log(msg);
 }
Exemple #8
0
 public void SendMsg(string msg)
 {
     if (bluetoothHelper != null)
     {
         if (bluetoothHelper.isConnected())
         {
             bluetoothHelper.SendData(msg);
         }
     }
 }
Exemple #9
0
    public void InputChanged(string s)
    {
        if (btManager.connectBT == true)
        {
            helper = btManager.GetBluetoothHelper(); //refact this to start
            helper.SendData(s);
        }

        Debug.Log(s);
    }
Exemple #10
0
    public void SliderChanged(float n)
    {
        string value = n.ToString();

        if (btManager.connectBT == true)
        {
            helper = btManager.GetBluetoothHelper(); //refact this to start
            helper.SendData(value);
        }
        Debug.Log(n);
        text.text = value;
    }
Exemple #11
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");
            }
        }
    }
    public void OnMessageSend()
    {
        send_Message = "5";            //inputText.text;
        //received_message = bluetoothHelper.Read();
        Debug.Log("msg 값은 = " + send_Message);
        Debug.Log("onmessage로 넘어옴");

        /*
         * if (send_Message == null)
         * {
         *  Debug.Log("Error : Messgae is Null");
         *  //Txt_Door.text = "Door is close";
         *  //onDoorClose.Invoke();
         * }*/

        if (send_Message != null)
        {
            Debug.Log("msg = " + send_Message);
            bluetoothHelper.SendData(send_Message);
        }
    }
Exemple #13
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();
         }
     }
 }
Exemple #14
0
    void OnConnected()
    {
        helper.StartListening();

        helper.SendData("Hi arduino!");
    }
Exemple #15
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("A");
     *      }
     * }
     */


    public void sendBlueToothMessage(String message)
    {
        bluetoothHelper.SendData(message);
    }