Exemple #1
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("자동 연결중...");
    }
Exemple #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)
        {
        }
    }
Exemple #3
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);
        }
    }
Exemple #4
0
    void Start()
    {
        try{
            x        = "";
            BTHelper = BluetoothHelper.GetInstance("HC-05");
            BTHelper.setLengthBasedStream();
            BTHelper.OnConnected += OnBluetoothConnected;             //OnBluetoothConnected is a function defined later on

            // BTHelper.OnDataReceived += () => {
            //  //this is called when you receive data FROM your arduino
            //  string receivedData;
            //  receivedData = BTHelper.Read(); // returns a string
            //  //since you are sending an array, convert the string to array :
            //  char[] data = receivedData.ToCharArray();

            //  //do Whatever you want
            // };

            BTHelper.OnDataReceived += (helper) => {
                try{
                    string xx   = helper.Read();
                    char[] data = xx.ToCharArray();

                    if (data.Length != 3)
                    {
                        return;
                    }

                    int i = 0;
                    if (data[0] != 'S')
                    {
                        return;
                    }
                    if (data[1] == 'E')
                    {
                        i = 1;
                    }
                    if (data[2] > 7)
                    {
                        return;
                    }

                    spheres[data[2] - 2].GetComponent <Renderer>().material = materials[i];
                }catch (Exception ex)
                {
                    x += ex.Message;
                }
            };
        }catch (Exception ex) {
            Debug.Log(ex);
            x = ex.ToString();
        }
    }
Exemple #5
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 + " ";
            }
        }
    }
Exemple #6
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!";
            }
        }
    }
Exemple #7
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;
        }
    }
Exemple #8
0
    void Start()
    {
        timer = 0;
        try{
            Debug.Log("HI");
            BluetoothHelper.BLE = true;  //use Bluetooth Low Energy Technology
            bluetoothHelper     = BluetoothHelper.GetInstance("TEST");
            Debug.Log(bluetoothHelper.getDeviceName());
            bluetoothHelper.OnConnected += () => {
                Debug.Log("Connected");
                sendData();
            };
            bluetoothHelper.OnConnectionFailed += () => {
                Debug.Log("Connection failed");
            };
            bluetoothHelper.OnScanEnded       += OnScanEnded;
            bluetoothHelper.OnServiceNotFound += (serviceName) =>
            {
                Debug.Log(serviceName);
            };
            bluetoothHelper.OnCharacteristicNotFound += (serviceName, characteristicName) =>
            {
                Debug.Log(characteristicName);
            };
            bluetoothHelper.OnCharacteristicChanged += (value, characteristic) =>
            {
                Debug.Log(characteristic.getName());
                Debug.Log(System.Text.Encoding.ASCII.GetString(value));
            };

            // BluetoothHelperService service = new BluetoothHelperService("FFE0");
            // service.addCharacteristic(new BluetoothHelperCharacteristic("FFE1"));
            // BluetoothHelperService service2 = new BluetoothHelperService("180A");
            // service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
            // bluetoothHelper.Subscribe(service);
            // bluetoothHelper.Subscribe(service2);
            // bluetoothHelper.ScanNearbyDevices();

            BluetoothHelperService service = new BluetoothHelperService("19B10000-E8F2-537E-4F6C-D104768A1214");
            service.addCharacteristic(new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214"));
            bluetoothHelper.Subscribe(service);
            //bluetoothHelper.Subscribe(service2);
            bluetoothHelper.ScanNearbyDevices();
        }catch (Exception ex) {
            Debug.Log(ex.Message);
        }
    }
    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);
        }
    }
Exemple #10
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;
        }
    }
Exemple #11
0
    // Start is called before the first frame update
    void Start()
    {
        data = "";
        tmp  = "";
        try{
            BluetoothHelper.BLE = true;
            helper                     = BluetoothHelper.GetInstance();
            helper.OnConnected        += OnConnected;
            helper.OnConnectionFailed += OnConnectionFailed;
            helper.OnScanEnded        += OnScanEnded;
            helper.OnDataReceived     += OnDataReceived;

            helper.setCustomStreamManager(new MyStreamManager()); //implement your own way of delimiting the messages
            //helper.setTerminatorBasedStream("\n"); //every messages ends with new line character
        }catch (Exception e) {
            Debug.LogError(e);
        }
    }
Exemple #12
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);
        }
    }
Exemple #13
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;
        }
    }
Exemple #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!");
        }
    }
    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); }
    }
Exemple #16
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) {}
    }
Exemple #17
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;
        }
    }
Exemple #18
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) { }
    }
Exemple #19
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;
        }
    }
Exemple #20
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);
 }
Exemple #21
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;
        }
    }
Exemple #22
0
    void Start()
    {
        timer = 0;
        try{
            Debug.Log("HI");

            BluetoothHelper.BLE          = true; //use Bluetooth Low Energy Technology
            bluetoothHelper              = BluetoothHelper.GetInstance();
            bluetoothHelper.OnConnected += (helper) => {
                List <BluetoothHelperService> services = helper.getGattServices();
                foreach (BluetoothHelperService s in services)
                {
                    Debug.Log("Service : " + s.getName());
                    foreach (BluetoothHelperCharacteristic item in s.getCharacteristics())
                    {
                        Debug.Log(item.getName());
                    }
                }

                Debug.Log("Connected");
                BluetoothHelperCharacteristic c = new BluetoothHelperCharacteristic("ffe1");
                c.setService("ffe0");
                bluetoothHelper.Subscribe(c);
                //sendData();
            };
            bluetoothHelper.OnConnectionFailed += (helper) => {
                Debug.Log("Connection failed");
            };
            bluetoothHelper.OnScanEnded       += OnScanEnded;
            bluetoothHelper.OnServiceNotFound += (helper, serviceName) =>
            {
                Debug.Log(serviceName);
            };
            bluetoothHelper.OnCharacteristicNotFound += (helper, serviceName, characteristicName) =>
            {
                Debug.Log(characteristicName);
            };
            bluetoothHelper.OnCharacteristicChanged += (helper, value, characteristic) =>
            {
                Debug.Log(characteristic.getName());
                Debug.Log(value[0]);
            };

            // BluetoothHelperService service = new BluetoothHelperService("FFE0");
            // service.addCharacteristic(new BluetoothHelperCharacteristic("FFE1"));
            // BluetoothHelperService service2 = new BluetoothHelperService("180A");
            // service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
            // bluetoothHelper.Subscribe(service);
            // bluetoothHelper.Subscribe(service2);
            // bluetoothHelper.ScanNearbyDevices();

            // BluetoothHelperService service = new BluetoothHelperService("19B10000-E8F2-537E-4F6C-D104768A1214");
            // service.addCharacteristic(new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214"));
            //BluetoothHelperService service2 = new BluetoothHelperService("180A");
            //service.addCharacteristic(new BluetoothHelperCharacteristic("2A24"));
            // bluetoothHelper.Subscribe(service);
            //bluetoothHelper.Subscribe(service2);
            bluetoothHelper.ScanNearbyDevices();
        }catch (Exception ex) {
            Debug.Log(ex.StackTrace);
        }
    }