Arduino and bluetooth

Hi!

Total noob here, trying to get values from my arduino to my phone through bluetooth. I am using a library to get these values and this part seems to be working. There is just the aspect of me being very new to C# and I am having some issues actually doing something with the values. Basically, I will have a pressure sensor which will give me about 10 different values, which will be translated to my speed float. When I change the value of speed manually, I can see the effect when I run this, my app also tells me there is data available, but although I am sending '1’and ‘2’ every 5 seconds, I am not seeing anything change. I will include the essential part of my code.

I’m sure my code will tell you how new I am, I am also aware that I should start with basics but unfortunately that is not possible for this project… Hopefully someone can help me!

using UnityEngine;
using System.Collections;
using TechTweaking.Bluetooth;


public class BasicDemo : MonoBehaviour
{


    BluetoothDevice device;
    public static float speed = 1;

   
   
    string ver = "1";
    string content;
    void Start()
    {

        device = new BluetoothDevice();

        device.Name = "HC-06";

        device.setEndByte(10);
        device.connect();
        
       
        device.ReadingCoroutine = ManageConnection;
       
    }




    public void connect()//Connect to the public global variable "device" if it's not null.
    {
        if (device != null)
        {
            device.connect();
        }
    }


    IEnumerator ManageConnection(BluetoothDevice device)
    {//Manage Reading Coroutine

       

        while (device.IsConnected && device.IsReading)
        {
            if (device.IsDataAvailable)
            {
                byte[] msg = device.read();
                 content = System.Text.ASCIIEncoding.ASCII.GetString(msg);

                if (msg.Length > 0)
                {
                    if (msg[0]==1)
                    {
                        speed = 1;
                    }
                    if (msg[0]==2)
                    {
                        speed = 5;
                    }
                }
            }
            yield return null;
        }

       
    }


    void OnGUI()
    {

        if(content == ver)
        {
            GUI.Label(new Rect(10, 150, 400, 180), "zz");
        }


        if (!device.IsConnected)
        {
            GUI.Label(new Rect(10, 10, 100, 20), "Nee");

        }

        if (device.IsConnected)
        {
            GUI.Label(new Rect(10, 10, 100, 20), "Ja");

        }

        if (device.IsDataAvailable)
        {
            GUI.Label(new Rect(10, 120, 100, 140), "Jahoor");

        }


        if (GUI.Button(new Rect(10, 30, 100, 70), "Click"))

            connect();



    }

}

There seems to be nothing wrong with your c# coding skills :slight_smile: It’s a logical problem , you need to read the manual for the Bluetooth SDK / API and you need to check for the end byte I guess ( in your package parsing loop ).

Maybe while( device.IsReading… is not such a good idea… it might be better to check while( device.isCoected && deviceIsDataAvailable )