Ejemplo n.º 1
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)
        {
        }
    }
Ejemplo n.º 2
0
    void Start()
    {
        deviceName = "HC-05"; //bluetooth should be turned ON;
        try
        {
            BluetoothHelper.BLE                 = false;
            bluetoothHelper                     = BluetoothHelper.GetInstance(deviceName);
            bluetoothHelper.OnConnected        += OnConnected;
            bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
            bluetoothHelper.OnDataReceived     += OnMessageReceived; //read the data
            bluetoothHelper.OnScanEnded        += OnScanEnded;

            bluetoothHelper.setTerminatorBasedStream("\n");

            if (!bluetoothHelper.ScanNearbyDevices())
            {
                //scan didnt start (on windows desktop (not UWP))
                //try to connect
                bluetoothHelper.Connect();//this will work only for bluetooth classic.
                //scanning is mandatory before connecting for BLE.
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex.Message);
            write(ex.Message);
        }
    }
Ejemplo n.º 3
0
    /// <summary>
    /// //////////////////////Start ////////////////////////////////////
    /// </summary>

    void Start()
    {
        deviceName = "Park"; //블루투스 모듈이 깜박이는 상태일 것
        try
        {
            BluetoothHelper.BLE                 = true;
            bluetoothHelper                     = BluetoothHelper.GetInstance(deviceName);
            bluetoothHelper.OnConnected        += OnConnected;
            bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
            bluetoothHelper.OnDataReceived     += OnMessageReceived; //read the data
            bluetoothHelper.OnScanEnded        += OnScanEnded;

            bluetoothHelper.setTerminatorBasedStream("\n");

            if (!bluetoothHelper.ScanNearbyDevices())
            {
                //scan didnt start (on windows desktop (not UWP))
                //try to connect
                bluetoothHelper.Connect();//this will work only for bluetooth classic.
                //scanning is mandatory before connecting for BLE.
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex.Message);
            LogWrite(ex.Message);
        }

        LogWrite("자동 연결중...");
    }
Ejemplo n.º 4
0
    private void TryToConnect()
    {
        msg.text = "App started ";
        msg.text = "Search for BLE connection: " + connectBLE + " ";

        if (connectBLE == true)
        {
            try
            {
                msg.text += "Trying... ";

                awatingMsg = false;

                BluetoothHelper.BLE = true;  //use Bluetooth Low Energy Technology
                bluetoothHelper     = BluetoothHelper.GetInstance("TEST");

                bluetoothHelper.setTerminatorBasedStream("\n");

                Debug.Log(bluetoothHelper.getDeviceName());
                msg.text += "Device name: " + bluetoothHelper.getDeviceName() + " ";

                bluetoothHelper.OnConnected += () => {
                    msg.text  += "Connected ";
                    awatingMsg = false;
                    bluetoothHelper.StartListening();
                };

                bluetoothHelper.OnConnectionFailed += () => {
                    msg.text += "Connection failed ";
                };

                bluetoothHelper.OnScanEnded += OnScanEnded;

                bluetoothHelper.OnDataReceived += BluetoothHelper_OnDataReceived;

                BluetoothHelperCharacteristic txC = new BluetoothHelperCharacteristic(UUID_TX);
                txC.setService(UUID);

                BluetoothHelperCharacteristic rxC = new BluetoothHelperCharacteristic(UUID_RX);
                rxC.setService(UUID);


                bluetoothHelper.setRxCharacteristic(rxC);
                bluetoothHelper.setTxCharacteristic(txC);

                bluetoothHelper.ScanNearbyDevices();
            }
            catch (Exception ex)
            {
                exception.text += ex + " ";
            }
        }
    }
Ejemplo n.º 5
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!";
            }
        }
    }
Ejemplo n.º 6
0
    void Start()
    {
        try
        {
            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;

            //FOR CUSTOM UUID with BLE
            //BluetoothHelperCharacteristic characteristic = new BluetoothHelperCharacteristic("beb5483e-36e1-4688-b7f5-ea07361b26a8");
            //characteristic.setService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
            //bluetoothHelper.setTxCharacteristic(characteristic);
            //bluetoothHelper.setRxCharacteristic(characteristic);

            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(deviceName);
                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;
        }
    }
Ejemplo n.º 7
0
    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);
        }
    }
Ejemplo n.º 8
0
    void Start()
    {
        try
        {
            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");

            bluetoothHelper2                          = BluetoothHelper.GetNewInstance();
            bluetoothHelper2.OnConnected             += OnConnected2;
            bluetoothHelper2.OnConnectionFailed      += OnConnectionFailed2;
            bluetoothHelper2.OnScanEnded             += OnScanEnded2;
            bluetoothHelper2.OnCharacteristicChanged += (value, characteristic) =>
            {
                Debug.Log(characteristic.getName());
                Debug.Log(System.Text.Encoding.ASCII.GetString(value));
            };


            BluetoothHelperService service = new BluetoothHelperService("180D");
            service.addCharacteristic(new BluetoothHelperCharacteristic("2A37"));
            service.addCharacteristic(new BluetoothHelperCharacteristic("2A38"));
            service.addCharacteristic(new BluetoothHelperCharacteristic("2A39"));
            bluetoothHelper2.Subscribe(service);


            bluetoothHelper.ScanNearbyDevices();



            text.text = "start scan";
        }
        catch (BluetoothHelper.BlueToothNotEnabledException ex)
        {
            sphere.GetComponent <Renderer>().material.color = Color.yellow;
            Debug.Log(ex.ToString());
            text.text = ex.Message;
        }
    }
Ejemplo n.º 9
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) {}
    }
Ejemplo n.º 10
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!");
        }
    }
    private void Start()
    {
        ShowText("Connecting...");

        try
        {
            m_Helper                     = BluetoothHelper.GetInstance(m_DeviceName);
            m_Helper.OnConnected        += OnBluetoothConnected;
            m_Helper.OnConnectionFailed += OnConnectionFailed;
            m_Helper.OnDataReceived     += OnBluetoothDataReceived;

            m_Helper.setTerminatorBasedStream("\n");
            Connect();
        }
        catch (BluetoothHelper.BlueToothNotEnabledException ex) { ShowText(ex.Message); }
        catch (BluetoothHelper.BlueToothNotReadyException ex) { ShowText(ex.Message); }
        catch (BluetoothHelper.BlueToothNotSupportedException ex) { ShowText(ex.Message); }
        catch (BluetoothHelper.BlueToothPermissionNotGrantedException ex) { ShowText(ex.Message); }
    }
Ejemplo n.º 12
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) { }
    }
Ejemplo n.º 13
0
 // Start is called before the first frame update
 void Start()
 {
     deviceName                          = "VenusIMU";
     bluetoothHelper                     = BluetoothHelper.GetInstance(deviceName);
     bluetoothHelper.OnConnected        += OnConnected;
     bluetoothHelper.OnConnectionFailed += OnConnectionFailed;
     bluetoothHelper.OnDataReceived     += OnMessageReceived;
     bluetoothHelper.setTerminatorBasedStream("\n");
     bluetoothHelper.Connect();
     startPos  = new Vector3(-0.15f, 60f, -10f);
     endPos    = new Vector3(-0.15f, 60f, -10f);
     startPos2 = new Vector3(-80.9f, 90.5f, -118.24f);
     endPos2   = new Vector3(-80.9f, 90.5f, -118.24f);
     HallPos1  = new Vector3(-0.15f, 60f, -10f);
     HallPos2  = new Vector3(-0.15f, 60f, -7f);
     HallPos3  = new Vector3(-0.15f, 60f, -4f);
     HallPos4  = new Vector3(-0.15f, 60f, -1f);
     US1       = new Vector3(-80.9f, 90.5f, -118.24f);
     US2       = new Vector3(-81.57f, 91.05f, -117.14f);
     US3       = new Vector3(-82.24f, 91.68f, -115.87f);
     US4       = new Vector3(-83.12f, 92.5f, -114.21f);
     //Linea de codigo de prueba para referencia
     CateterGuia.transform.rotation = Quaternion.Euler(roll * factor, 0, pitch * factor);
 }
Ejemplo n.º 14
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!!!
             * }
             */

            LinkedList <BluetoothDevice> ds = bluetoothHelper.getPairedDevicesList();

            //Debug.Log(ds);
            // 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;
        }
    }