Exemplo n.º 1
0
    private void OnScanEnded(BluetoothHelper helper, LinkedList <BluetoothDevice> devices)
    {
        Debug.Log("FOund " + devices.Count);
        if (devices.Count == 0)
        {
            bluetoothHelper.ScanNearbyDevices();
            return;
        }

        foreach (var d in devices)
        {
            Debug.Log(d.DeviceName);
        }

        try
        {
            bluetoothHelper.setDeviceName("HC-08");
            bluetoothHelper.Connect();
            Debug.Log("Connecting");
        }catch (Exception ex)
        {
            bluetoothHelper.ScanNearbyDevices();
            Debug.Log(ex.Message);
        }
    }
Exemplo n.º 2
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();
    }
Exemplo n.º 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);
        }
    }
Exemplo n.º 4
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("자동 연결중...");
    }
Exemplo n.º 5
0
 void OnScanEnded(BluetoothHelper helper, LinkedList <BluetoothDevice> devices)
 {
     if (helper.isDevicePaired()) //we did found our device (with BLE) or we already paired the device (for Bluetooth Classic)
     {
         helper.Connect();
     }
     else
     {
         helper.ScanNearbyDevices(); //we didn't
     }
 }
Exemplo n.º 6
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 + " ";
            }
        }
    }
Exemplo n.º 7
0
    void OnConnected()
    {
        sphere.GetComponent <Renderer>().material.color = Color.green;
        try{
            bluetoothHelper.StartListening();

            bluetoothHelper2              = BluetoothHelper.GetNewInstance();
            bluetoothHelper2.OnScanEnded += ScanEnded2;
            bluetoothHelper2.ScanNearbyDevices();
        }catch (Exception ex) {
            Debug.Log(ex.Message);
        }
    }
Exemplo n.º 8
0
    void OnScanEnded2(LinkedList <BluetoothDevice> nearbyDevices)
    {
        Debug.Log("2 ended " + nearbyDevices.Count);
        if (nearbyDevices.Count == 0)
        {
            bluetoothHelper2.ScanNearbyDevices();
            return;
        }


        foreach (BluetoothDevice device in nearbyDevices)
        {
            Debug.Log(device.DeviceName);
            if (device.DeviceName == "HUAWEI Y7 Prime 2018")
            {
                Debug.Log("FOUND!!");
            }
        }


        bluetoothHelper2.setDeviceName("HUAWEI Y7 Prime 2018");
        bluetoothHelper2.Connect();
    }
Exemplo n.º 9
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;
        }
    }
Exemplo n.º 10
0
 void OnConnected(BluetoothHelper helper)
 {
     sphere.GetComponent <Renderer>().material.color = Color.green;
     try
     {
         helper.StartListening();
         if (helper.getId() == bluetoothHelper.getId()) //1st instance connected, connect the second
         {
             bluetoothHelper2.ScanNearbyDevices();
         }
     }
     catch (Exception ex)
     {
         Debug.Log(ex.Message);
     }
 }
Exemplo n.º 11
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);
        }
    }
Exemplo n.º 12
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;
        }
    }
Exemplo n.º 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;
        }
    }
Exemplo n.º 14
0
    private void OnScanEnded(LinkedList <BluetoothDevice> devices)
    {
        msg.text += "Found " + devices.Count + " ";

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

        try
        {
            bluetoothHelper.setDeviceName("ASTRA_K_LED_BLE");
            bluetoothHelper.Connect();

            msg.text += "Connecting ";
        }
        catch (Exception ex)
        {
            exception.text += ex + " ";
        }
    }
Exemplo n.º 15
0
 void OnGUI()
 {
     if (helper == null)
     {
         return;
     }
     if (!helper.isConnected() && !isScanning && !isConnecting)
     {
         if (GUI.Button(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Start Scanning"))
         {
             isScanning = helper.ScanNearbyDevices();
         }
         if (devices != null && devices.First != null)
         {
             draw();
         }
     }
     else if (!helper.isConnected() && isScanning)
     {
         GUI.TextArea(new Rect(Screen.width / 2 - Screen.width / 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Scanning...");
     }
     else if (helper.isConnected())
     {
         GUI.TextArea(new Rect(Screen.width / 4, 2 * Screen.height / 10, Screen.width / 2, 7 * Screen.height / 10), data);
         tmp = GUI.TextField(new Rect(Screen.width / 4, Screen.height / 10, Screen.width / 2, Screen.height / 10 - 10), tmp);
         if (GUI.Button(new Rect(3 * Screen.width / 4 + 10, Screen.height / 10, Screen.width / 5, Screen.height / 10), "Send"))
         {
             helper.SendData(tmp);
             data += "\n>" + tmp;
             tmp   = "";
         }
         if (GUI.Button(new Rect(3 * Screen.width / 4 + 10, 8 * Screen.height / 10, Screen.width / 5, Screen.height / 10), "Disconnect"))
         {
             helper.Disconnect();
         }
     }
 }
Exemplo n.º 16
0
    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();
    }
Exemplo n.º 17
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);
        }
    }