Example #1
0
        //***********************************************************************************************************************************
        //***********************************************************************************************************************************
        //****************************************      BLUETOOTH THREAD      ***************************************************************
        //***********************************************************************************************************************************
        //***********************************************************************************************************************************

        /***
         * The Main part of the bluetooth input/output thread.
         * It sends the arduino the "data" command for it to begin sending data.
         * Then, it repeatedly calls the getWholeValue method which is responsible for reading the data from the arduino.
         * Every 100 milliSeconds it invokes a UI update method on the MainThread.
         * when the askToQuit flag is updated to false (in the MainThread), it stops reading data from the arduino,
         * sends it a "end" command, and clears the bluetooth buffer.
         */
        public void bluetoothManager()
        {
            bool result = btConnection.SendDataOrEndCommand("data");

            if (!result)
            {
                //failed sending command to arduino. exit the app
                DependencyService.Get <ICloseApp>().closeApplication();
            }
            string recieved = btConnection.RecieveDataOneByte();

            while (recieved == "")
            {
                //Currently there is Nothing to read
                recieved = btConnection.RecieveDataOneByte();
            }
            currReciever = recieved;
            while (!askToQuit)
            {
                getWholeValue();
                Int64 currTime = sw.ElapsedMilliseconds;
                if (currTime - lastUpdateTime > 100)
                {
                    lastUpdateTime = currTime;
                    Device.BeginInvokeOnMainThread(updateSpeedDistTime);
                }
            }
            result = btConnection.SendDataOrEndCommand("end");
            if (!result)
            {
                //failed sending command to arduino. exit the app
                DependencyService.Get <ICloseApp>().closeApplication();
            }
            emptyBTBuffer();
        }