Beispiel #1
0
 void OnScanEnded(LinkedList <BluetoothDevice> devices)
 {
     if (bluetoothHelper.isDevicePaired()) //we did found our device (with BLE) or we already paired the device (for Bluetooth Classic)
     {
         bluetoothHelper.Connect();
     }
     else
     {
         bluetoothHelper.ScanNearbyDevices(); //we didn't
     }
 }
Beispiel #2
0
    // Start is called before the first frame update
    void Start()
    {
        deviceName = "BikeSpeed";

        try
        {
            BTHelper                     = BluetoothHelper.GetInstance(deviceName);
            BTHelper.OnConnected        += OnConnected;
            BTHelper.OnConnectionFailed += OnConnFailed;

            BTHelper.setTerminatorBasedStream("\n");
            //BTHelper.setLengthBasedStream();

            //if we have already paired with the device
            if (BTHelper.isDevicePaired())
            {
                BTHelper.Connect(); //we manage to connect successfully
            }
        }
        catch (BluetoothHelper.BlueToothNotEnabledException ex)
        {
        }
        catch (BluetoothHelper.BlueToothNotReadyException ex)
        {
        }
        catch (BluetoothHelper.BlueToothNotSupportedException ex)
        {
        }
    }
Beispiel #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(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);
        //    }
    }
Beispiel #4
0
    void OnScanEnded(LinkedList <BluetoothDevice> nearbyDevices)
    {
        Debug.Log("1 ended");

        if (nearbyDevices.Count == 0)
        {
            bluetoothHelper.ScanNearbyDevices();
            return;
        }


        foreach (BluetoothDevice device in nearbyDevices)
        {
            if (device.DeviceName == "HC-08")
            {
                Debug.Log("FOUND!!");
            }
        }

        text.text = "HC-08";
        bluetoothHelper.setDeviceName("HC-08");
        // bluetoothHelper.setDeviceAddress("00:21:13:02:16:B1");
        bluetoothHelper.Connect();
        bluetoothHelper.isDevicePaired();
    }
Beispiel #5
0
    void OnGUI()
    {
        if (BTHelper == null)
        {
            return;
        }


        BTHelper.DrawGUI();

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

        if (BTHelper.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"))
            {
                BTHelper.Disconnect();
            }
        }
    }
Beispiel #6
0
 public void button()
 {
     if (!bluetoothHelper.isConnected())
     {
         if (bluetoothHelper.isDevicePaired())
         {
             bluetoothHelper.Connect(); // tries to connect
         }
     }
 }
Beispiel #7
0
 void ConnectBT()
 {
     if (!bluetoothHelper.isConnected())
     {
         Middletext.text = "Disconnected";
         if (bluetoothHelper.isDevicePaired())
         {
             bluetoothHelper.Connect(); // tries to connect
         }
     }
 }
Beispiel #8
0
 public void connectButtonClick()
 {
     if (bluetoothHelper.isDevicePaired())
     {
         bluetoothHelper.Connect(); // tries to connect
     }
     else
     {
         LogWrite("Cannot connect, device is not found, for bluetooth classic, pair the device\n\tFor BLE scan for nearby devices");
     }
 }
Beispiel #9
0
    private void TryToConnect()
    {
        // Get saved changes, or the default value
        deviceName = PlayerPrefs.GetString("DeviceName", deviceName);

        debbugerMsg.text  = "Device name: " + deviceName + "; ";
        debbugerMsg.text += "Looking for a connection: " + connectBT + "; ";

        if (connectBT == true)
        {
            try
            {
                helper = BluetoothHelper.GetInstance(deviceName);

                helper.OnConnected        += OnConnected;
                helper.OnConnectionFailed += OnConnFailed;

                helper.setTerminatorBasedStream("\n");

                if (helper.isDevicePaired()) // old isDeviceFound
                {
                    helper.Connect();
                }

                debbugerMsg.text += "Bluetooth connected: yes!";
            }
            catch (BluetoothHelper.BlueToothNotEnabledException ex)
            {
                Debug.LogError("Excetion founded in Btmagaer: " + ex);
                exT.text         += " " + ex;
                debbugerMsg.text += "Bluetooth connected: No!";
            }
            catch (BluetoothHelper.BlueToothNotReadyException ex)
            {
                Debug.LogError("Excetion founded in Btmagaer: " + ex);
                exT.text         += " " + ex;
                debbugerMsg.text += "Bluetooth connected: No!";
            }
            catch (BluetoothHelper.BlueToothNotSupportedException ex)
            {
                Debug.LogError("Excetion founded in Btmagaer: " + ex);
                exT.text         += " " + ex;
                debbugerMsg.text += "Bluetooth connected: No!";
            }
            catch (BluetoothHelper.BlueToothPermissionNotGrantedException ex)
            {
                Debug.LogError("Excetion founded in Btmagaer: " + ex);
                exT.text         += " " + ex;
                debbugerMsg.text += "Bluetooth connected: No!";
            }
        }
    }
Beispiel #10
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");
            }
        }
    }
    void Start()
    {
        Btn_Connect.onClick.AddListener(() => {
            if (bluetoothHelper.isDevicePaired())
            {
                Debug.Log("try to connect");
                bluetoothHelper.Connect();                  // tries to connect
            }
            else
            {
                Debug.Log("not DevicePaired");
            }
        });
        Btn_Disconnect.onClick.AddListener(() => {
            //bluetoothHelper.Disconnect ();
            Score.score++;
            Debug.Log("try to Disconnect");
        });
        //=============================================================================================
        //=============================================================================================

        deviceName = "cgproj1234";         //bluetooth should be turned ON; // 페어링되는 아두이노 블루투스 이름과 같아야 합니다.

        //=============================================================================================
        //=============================================================================================
        try {
            bluetoothHelper                     = BluetoothHelper.GetInstance(deviceName);
            bluetoothHelper.OnConnected        += OnConnected;
            bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
            bluetoothHelper.OnDataReceived     += OnMessageReceived;         //read the data

            bluetoothHelper.setTerminatorBasedStream("\n");

            if (bluetoothHelper.isDevicePaired())
            {
                Toggle_isDevicePaired.isOn = true;
            }
            else
            {
                Toggle_isDevicePaired.isOn = false;
            }
        } catch (Exception ex) {
            Toggle_isDevicePaired.isOn = false;
            Debug.Log(ex.Message);
        }
    }
Beispiel #12
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");
            }
        }
    }
Beispiel #13
0
    // Start is called before the first frame update
    void Start()
    {
        msg        = "";
        deviceName = "BabyDozer";
        try
        {
            helper                     = BluetoothHelper.GetInstance(deviceName);
            helper.OnConnected        += OnConnected;
            helper.OnConnectionFailed += OnConnectionFailed;
            helper.setTerminatorBasedStream("\n"); //every messages ends with new line character

            //Debug.Log(helper.isDevicePaired());
            if (helper.isDevicePaired())
            {
                helper.Connect();
            }
        }
        catch (Exception) {}
    }
Beispiel #14
0
    /// <summary>
    /// This method creates a connection between the HC-055 module and the current device.
    /// </summary>
    public void Connect()
    {
        try
        {
            bt                     = BluetoothHelper.GetInstance("Regenfallrohr");
            bt.OnConnected        += OnConnected;
            bt.OnConnectionFailed += OnConnectionFailed;
            bt.setTerminatorBasedStream("\n");

            if (bt.isDevicePaired())
            {
                bt.Connect();
            }
        }
        catch (System.Exception)
        {
            print("Could not connect!");
        }
    }
Beispiel #15
0
    void Start()
    {
        min [0] = 1023;
        min [1] = 1023;
        min [2] = 1023;
        min [3] = 1023;
        min [4] = 1023;

        max [0]    = 0;
        max [1]    = 0;
        max [2]    = 0;
        max [3]    = 0;
        max [4]    = 0;
        deviceName = "HC05";         //bluetooth should be turned ON;
        try
        {
            bluetoothHelper                     = BluetoothHelper.GetInstance(deviceName);
            bluetoothHelper.OnConnected        += OnConnected;
            bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
            bluetoothHelper.OnDataReceived     += OnMessageReceived;         //read the data



            bluetoothHelper.setLengthBasedStream();


            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;
        }
    }
Beispiel #16
0
    void Start()
    {
        deviceName = "ARcon_1";
        try
        {
            helper                     = BluetoothHelper.GetInstance(deviceName);
            helper.OnConnected        += OnConnected;
            helper.OnConnectionFailed += OnConnFailed;

            helper.setTerminatorBasedStream("\n");
            //or
            //helper.setLengthBasedStream();

            if (helper.isDevicePaired())
            {
                helper.Connect();
            }
        }catch (BluetoothHelper.BlueToothNotEnabledException ex) { }
        catch (BluetoothHelper.BlueToothNotReadyException ex) { }
        catch (BluetoothHelper.BlueToothPermissionNotGrantedException ex) { }
    }
Beispiel #17
0
    void Start()
    {
        lauf  = false;
        greif = false;
        info  = false;

        for (int i = 0; i <= 5; i++) // Setzt die Begrenzungs Werte
        {
            zu[i]  = 0;
            auf[i] = 1023;
        }


        deviceName = "HC05"; //Wichtig: Bluethoot AN !

        try
        {
            bluetoothHelper                     = BluetoothHelper.GetInstance(deviceName);
            bluetoothHelper.OnConnected        += OnConnected;
            bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
            bluetoothHelper.OnDataReceived     += OnMessageReceived; //list die Daten ein

            bluetoothHelper.setLengthBasedStream();

            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;
        }
    }
    void OnScanEnded(BluetoothHelper helper, LinkedList <BluetoothDevice> nearbyDevices)
    {
        text.text = "Found " + nearbyDevices.Count + " devices";
        if (nearbyDevices.Count == 0)
        {
            helper.ScanNearbyDevices();
            return;
        }


        foreach (BluetoothDevice device in nearbyDevices)
        {
            if (device.DeviceName == deviceName)
            {
                Debug.Log("FOUND!!");
            }
        }

        text.text = deviceName;
        bluetoothHelper.setDeviceName(deviceName);
        // bluetoothHelper.setDeviceAddress("00:21:13:02:16:B1");
        bluetoothHelper.Connect();
        bluetoothHelper.isDevicePaired();
    }
Beispiel #19
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;
        }
    }