Ejemplo n.º 1
0
        private TimeSpan SCAN_INTERVAL_PRESENT = new TimeSpan(0, 30, 1); // Keep a multiple of this slightly longer than AWAY_TIMEOUT to prevent evaulating 1sec before the timeout.

        #endregion Fields

        #region Constructors

        public BluetoothService(ServiceCreationInfo info)
            : base("bluetooth", info)
        {
            mBluetooth = new BluetoothHelper();

            // Refresh available devices
            mBluetooth.RefreshDevices();
            mDeviceNames = mBluetooth.GetDevices();
            Log.Info("Bluetooth devices: {0}", mDeviceNames);

            // Set up scan thread.
            mEvents = new EventWaitHandle[] { mStopEvent, mDeviceEvent };
            mThread = new Thread(_searchThread);
            mThread.Start();
        }
    // [DllImport ("TestPlugin")]
    // private static extern int getNumber();

    void Start()
    {
        try
        {
            // Debug.Log(getNumber());
            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(3);

            // 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;
        }
    }
Ejemplo n.º 3
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.º 5
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;
        }
    }
Ejemplo n.º 6
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)
        {
        }
    }
Ejemplo n.º 7
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.º 8
0
 void OnConnected(BluetoothHelper helper)
 {
     sphere.GetComponent <Renderer>().material.color = Color.green;
     try
     {
         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());
             }
         }
         helper.StartListening();
     }
     catch (Exception ex)
     {
         Debug.Log(ex.Message);
     }
 }
Ejemplo n.º 9
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;
        }
    }
Ejemplo n.º 10
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                mRunning = false;

                if (mStopEvent != null)
                {
                    mStopEvent.Set();
                }

                // Shut down thread
                if (mThread != null)
                {
                    mThread.Join(10000);
                    mThread = null;
                }

                if (mDeviceEvent != null)
                {
                    mDeviceEvent.Dispose();
                }
                mDeviceEvent = null;

                if (mStopEvent != null)
                {
                    mStopEvent.Dispose();
                }
                mStopEvent = null;

                if (mBluetooth != null)
                {
                    mBluetooth.Dispose();
                }
                mBluetooth = null;
            }

            base.Dispose(disposing);
        }
    void Start()
    {
        try{
            Debug.Log("HI");
            BluetoothHelper.BLE          = true; //use Bluetooth Low Energy Technology
            bluetoothHelper              = BluetoothHelper.GetInstance();
            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();
        }catch (Exception ex) {
            Debug.Log(ex.Message);
        }
    }
Ejemplo n.º 12
0
    void OnScanEnded2(BluetoothHelper helper, LinkedList <BluetoothDevice> nearbyDevices)
    {
        Debug.Log("2 ended " + nearbyDevices.Count);
        if (nearbyDevices.Count == 0)
        {
            helper.ScanNearbyDevices();
            return;
        }


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


        helper.setDeviceName("HUAWEI Y7 Prime 2018");
        helper.Connect();
    }
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 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();
    }
Ejemplo n.º 15
0
 void OnMessageReceived(BluetoothHelper helper)
 {
     received_message = helper.Read();
     Debug.Log(received_message);
     write("Received : " + received_message);
 }
Ejemplo n.º 16
0
 private static bool CanDisableBluetooth(BluetoothAdapter adapter)
 {
     return(adapter.IsEnabled &&
            // only disconnect if bluetooth is not in use
            !BluetoothHelper.GuessHasActiveConnection(adapter));
 }
Ejemplo 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);
        }
    }
Ejemplo n.º 18
0
        //public static void findPrinters(Context paramContext, DiscoveryHandler paramDiscoveryHandler)
        //{
        //    BluetoothDiscoverer local1 = new BluetoothDiscoverer();
        //    FindDevices(paramContext, paramDiscoveryHandler, local1);
        //}

        public static void FindServices(Context paramContext, String paramString, IServiceDiscoveryHandler paramServiceDiscoveryHandler)
        {
            BtServiceDiscoverer localBtServiceDiscoverer = new BtServiceDiscoverer(BluetoothHelper.FormatMacAddress(paramString), paramServiceDiscoveryHandler);

            localBtServiceDiscoverer.DoDiscovery(paramContext);
        }
Ejemplo n.º 19
0
 void OnConnected(BluetoothHelper helper)
 {
     isConnecting = false;
     helper.StartListening();
 }
Ejemplo n.º 20
0
 void OnScanEnded(BluetoothHelper helper, LinkedList <BluetoothDevice> devices)
 {
     this.isScanning = false;
     this.devices    = devices;
 }
Ejemplo n.º 21
0
 void OnDataReceived(BluetoothHelper helper)
 {
     data += "\n<" + helper.Read();
 }
Ejemplo n.º 22
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            DisplayMetrics metric = new DisplayMetrics();

            this.Window.WindowManager.DefaultDisplay.GetMetrics(metric);
            float density = metric.Density;  // 屏幕密度(0.75 / 1.0 / 1.5)

            //Rate = (float)metric.WidthPixels / (float)1280;

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            blueH = new BluetoothHelper();
            int err = 0;
            Action <string, string> ConnectedAction = new Action <string, string>((name, address) =>
            {
                System.Console.WriteLine(name);
                System.Console.WriteLine(address);
            });
            Action <string> ConnectingAction = new Action <string>((t) =>
            {
            });
            Action <string> ConnFailedAction = new Action <string>((t) =>
            {
            });



            if (blueH.Init(ConnectedAction, ConnectingAction, ConnFailedAction) == 1)
            {
                //蓝牙已打开
            }
            else
            {
                if (blueH.IsOpen() == 1)
                {
                    //打开蓝牙
                    blueH.Open(this);
                }
            }



            Button btnScan = FindViewById <Button>(Resource.Id.MyButton);

            btnScan.Click += (o, e) =>
            {
                var serverIntent = new Intent(this, typeof(DeviceManager));
                StartActivityForResult(serverIntent, DeviceManager.REQUEST_CONNECT_DEVICE);
            };
            Button Print = FindViewById <Button>(Resource.Id.Print);

            Print.Click += (o, e) =>
            {
                //Java.Lang.String str0 = new Java.Lang.String("时段:晚市    手机:18721636793   人数:1");

                //if (blueH.IsConnected()!=1)
                //{
                //    if (blueH.Connect(address)==1)
                //    {
                //        blueH.SendMessage(str0);
                //    }
                //}
                //else
                //{
                //    blueH.SendMessage(str0);
                //    //Bitmap bm = BitmapFactory.DecodeStream(Resources.Assets.Open("android.png"));
                //    //blueH.SendImg(bm,576,0);
                //    //blueH.WalkPaper(2);
                //    //blueH.CutPage();
                //}
                /*  */
                //if (blueH.IsConnected() != 1)
                //{
                //    blueH.Connect(address);
                //}

                var cmd = Ticket.CreateTicketCMD("星巴克", "晚餐", "18721636753", "5", "2015-01-26 19:12:12", "小桌A001", "之前还有17位客人等待,过号请重新领号,不要走开哦", "021-0000000", "上海市徐汇区商业区", "扫描二维码查看您的排号队列,还可以下载官方APP,获取等多优惠", "", "如所持票已等待30分钟,可凭票销售8.8折优惠(团购不享受此优惠)");
                blueH.SendCommand(cmd.ToArray());

                //Bitmap bm = BitmapFactory.DecodeStream(Resources.Assets.Open("qr.jpg"));
                //blueH.SendImg(bm,576,0);

                //blueH.WalkPaper(6);
                //blueH.CutPage();
                //blueH.SendCommand(SerialPortPrint.PrintCommand.Inite());
                //blueH.SendCommand(PrintFormatCommand.FontSize(FontSizeEnum.Size16));
                //Java.Lang.String str = new Java.Lang.String("猫管家");
                //blueH.SendMessage(str);
                //blueH.SendCommand(PrintFormatCommand.Wrap());
                //blueH.SendCommand(PrintFormatCommand.Wrap());
                //blueH.SendCommand(PrintFormatCommand.FontSize(FontSizeEnum.Normal));
                //Java.Lang.String str0 = new Java.Lang.String("时段:晚市    手机:18721636793   人数:1");
                //blueH.SendMessage(str0);
                //blueH.SendCommand(PrintFormatCommand.Wrap());
                //blueH.SendCommand(PrintFormatCommand.Wrap());

                //Java.Lang.String str1 = new Java.Lang.String("领号时间:2015-01-17 17:14:55");
                //blueH.SendMessage(str1);
                //blueH.SendCommand(PrintFormatCommand.Wrap());

                //blueH.SendCommand(PrintFormatCommand.FontSize(FontSizeEnum.Size32));
                //blueH.SendCommand(PrintFormatCommand.Align(1));
                //Java.Lang.String str3 = new Java.Lang.String("小桌 A001");
                //blueH.SendMessage(str3);
                //blueH.SendCommand(PrintFormatCommand.Wrap());
            };
        }
Ejemplo n.º 23
0
 void OnConnectionFailed(BluetoothHelper helper)
 {
     sphere.GetComponent <Renderer>().material.color = Color.red;
     Debug.Log("Connection Failed");
 }
Ejemplo n.º 24
0
 void OnMessageReceived(BluetoothHelper helper)
 {
     payload  = helper.Read();
     payloada = payload.Split(';');
     Debug.Log(payload);
 }
Ejemplo n.º 25
0
 void OnConnectionFailed(BluetoothHelper helper)
 {
     write("Connection Failed");
     Debug.Log("Connection Failed");
 }
Ejemplo n.º 26
0
 void OnConnectionFailed(BluetoothHelper helper)
 {
     isConnecting = false;
 }
Ejemplo n.º 27
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            DisplayMetrics metric = new DisplayMetrics();

            this.Window.WindowManager.DefaultDisplay.GetMetrics(metric);
            float density = metric.Density;  // 屏幕密度(0.75 / 1.0 / 1.5)

            //Rate = (float)metric.WidthPixels / (float)1280;

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            blueH = new BluetoothHelper();
            int err = 0;
            Action <string, string> ConnectedAction = new Action <string, string>((name, address) =>
            {
            });
            Action <string> ConnectingAction = new Action <string>((t) =>
            {
            });
            Action <string> ConnFailedAction = new Action <string>((t) =>
            {
            });



            if (blueH.Init(ConnectedAction, ConnectingAction, ConnFailedAction) == 1)
            {
                //蓝牙已打开
            }
            else
            {
                if (blueH.IsOpen() == 1)
                {
                    //打开蓝牙
                    blueH.Open(this);
                }
            }



            Button btnScan = FindViewById <Button>(Resource.Id.MyButton);

            btnScan.Click += (o, e) =>
            {
                var serverIntent = new Intent(this, typeof(DeviceManager));
                StartActivityForResult(serverIntent, DeviceManager.REQUEST_CONNECT_DEVICE);
            };
            Button Print = FindViewById <Button>(Resource.Id.Print);

            Print.Click += (o, e) =>
            {
                Java.Lang.String str = new Java.Lang.String("hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111hello111111111111111111111111111111111111111111111111111111111111111111111111111111111111");

                if (blueH.IsConnected() != 1)
                {
                    if (blueH.Connect(address) == 1)
                    {
                        blueH.SendMessage(str);
                    }
                }
                else
                {
                    //blueH.SendMessage(str);
                    Bitmap bm = BitmapFactory.DecodeStream(Resources.Assets.Open("android.png"));
                    blueH.SendImg(bm, 576, 0);
                    blueH.WalkPaper(2);
                    blueH.CutPage();
                }
            };
        }
Ejemplo n.º 28
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;
        }
    }
Ejemplo n.º 29
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                mRunning = false;

                if (mStopEvent != null)
                    mStopEvent.Set();

                // Shut down thread
                if (mThread != null)
                {
                    mThread.Join(10000);
                    mThread = null;
                }

                if (mDeviceEvent != null)
                    mDeviceEvent.Dispose();
                mDeviceEvent = null;

                if (mStopEvent != null)
                    mStopEvent.Dispose();
                mStopEvent = null;

                if (mBluetooth != null)
                    mBluetooth.Dispose();
                mBluetooth = null;
            }

            base.Dispose(disposing);
        }