Beispiel #1
0
    void InitBT()
    {
        deviceName = "NONAME-ARDUINO"; //bluetooth should be turned ON;
        try
        {
            bluetoothHelper                     = BluetoothHelper.GetInstance(deviceName);
            bluetoothHelper.OnConnected        += OnConnected;
            bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
            bluetoothHelper.OnDataReceived     += OnMessageReceived;

            bluetoothHelper.setFixedLengthBasedStream(1);
        }
        catch (Exception ex)
        {
            Middletext.text = ex.Message;
            IGConsole.Instance.Main.println(ex.Message);
        }
    }
Beispiel #2
0
    void Start()
    {
        try
        {
            // Debug.Log(getNumber());
            Debug.Log(Application.unityVersion);
            BluetoothHelper.BLE                 = true; //use Bluetooth Low Energy Technology
            bluetoothHelper                     = BluetoothHelper.GetInstance();
            bluetoothHelper.OnConnected        += OnConnected;
            bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
            bluetoothHelper.OnDataReceived     += OnMessageReceived; //read the data
            bluetoothHelper.OnScanEnded        += OnScanEnded;

            //bluetoothHelper.setTerminatorBasedStream("\n");
            //bluetoothHelper.setLengthBasedStream();
            bluetoothHelper.setFixedLengthBasedStream(10);

            // if(bluetoothHelper.isDevicePaired())
            //  sphere.GetComponent<Renderer>().material.color = Color.blue;
            // else
            //  sphere.GetComponent<Renderer>().material.color = Color.grey;
            // bluetoothHelper.ScanNearbyDevices();
            if (!bluetoothHelper.ScanNearbyDevices())
            {
                //text.text = "cannot start scan";
                sphere.GetComponent <Renderer>().material.color = Color.black;

                // bluetoothHelper.setDeviceAddress("00:21:13:02:16:B1");
                bluetoothHelper.setDeviceName("HC-08");
                bluetoothHelper.Connect();
            }
            else
            {
                text.text = "start scan";
                // sphere.GetComponent<Renderer>().material.color = Color.green;
            }
        }
        catch (BluetoothHelper.BlueToothNotEnabledException ex)
        {
            sphere.GetComponent <Renderer>().material.color = Color.yellow;
            Debug.Log(ex.ToString());
            text.text = ex.Message;
        }
    }
Beispiel #3
0
    void Start()
    {
        deviceName = "HC-05";         //bluetooth should be turned ON;
        try
        {
            bluetoothHelper                     = BluetoothHelper.GetInstance(deviceName);
            bluetoothHelper.OnConnected        += OnConnected;
            bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
            bluetoothHelper.OnDataReceived     += OnMessageReceived; //read the data

            bluetoothHelper.setFixedLengthBasedStream(3);            //receiving every 3 characters together
            // bluetoothHelper.setTerminatorBasedStream("\n"); //delimits received messages based on \n char
            //if we received "Hi\nHow are you?"
            //then they are 2 messages : "Hi" and "How are you?"


            // bluetoothHelper.setLengthBasedStream();

            /*
             * will received messages based on the length provided, this is useful in transfering binary data
             * if we received this message (byte array) :
             * {0x55, 0x55, 0, 3, 'a', 'b', 'c', 0x55, 0x55, 0, 9, 'i', ' ', 'a', 'm', ' ', 't', 'o', 'n', 'y'}
             * then its parsed as 2 messages : "abc" and "i am tony"
             * the first 2 bytes are the length data writted on 2 bytes
             * byte[0] is the MSB
             * byte[1] is the LSB
             *
             * on the unity side, you dont have to add the message length implementation.
             *
             * if you call bluetoothHelper.SendData("HELLO");
             * this API will send automatically :
             * 0x55 0x55    0x00 0x05   0x68 0x65 0x6C 0x6C 0x6F
             |________|   |________|  |________________________|
             * preamble      Length             Data
             *
             *
             * when sending data from the arduino to the bluetooth, there's no preamble added.
             * this preamble is used to that you receive valid data if you connect to your arduino and its already send data.
             * so you will not receive
             * on the arduino side you can decode the message by this code snippet:
             * char * data;
             * char _length[2];
             * int length;
             *
             * if(Serial.avalaible() >2 )
             * {
             *      _length[0] = Serial.read();
             *      _length[1] = Serial.read();
             *      length = (_length[0] << 8) & 0xFF00 | _length[1] & 0xFF00;
             *
             *      data = new char[length];
             *      int i=0;
             *      while(i<length)
             *      {
             *              if(Serial.available() == 0)
             *                      continue;
             *              data[i++] = Serial.read();
             *      }
             *
             *
             *      ...process received data...
             *
             *
             *      delete [] data; <--dont forget to clear the dynamic allocation!!!
             * }
             */

            if (bluetoothHelper.isDevicePaired())
            {
                sphere.GetComponent <Renderer>().material.color = Color.blue;
            }
            else
            {
                sphere.GetComponent <Renderer>().material.color = Color.grey;
            }
        }
        catch (Exception ex)
        {
            sphere.GetComponent <Renderer>().material.color = Color.yellow;
            Debug.Log(ex.Message);
            text.text = ex.Message;
            //BlueToothNotEnabledException == bluetooth Not turned ON
            //BlueToothNotSupportedException == device doesn't support bluetooth
            //BlueToothNotReadyException == the device name you chose is not paired with your android or you are not connected to the bluetooth device;
            //								bluetoothHelper.Connect () returned false;
        }
    }