Ejemplo n.º 1
0
 public void Run(OBDConnectionService cs, byte[] receivedBuffer, int bufLen)
 {
     start = System.Environment.TickCount;
     SendCommand(cs);
     ReadResult(receivedBuffer, bufLen);
     end = System.Environment.TickCount;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Create the thread which, when it starts, handles periodic request and data storage.
 /// </summary>
 /// <param name="cs">ConnectionService to manage write and read to/from bt socket.</param>
 public RequestCommandThread(OBDConnectionService cs)
 {
     this.Name         = "RequestCommandThread";
     connectionService = cs;
     RPMstorage_raw    = new DataStorage("rpm_measure");
     SpeedStorage_raw  = new DataStorage("speed_measure");
     TimeOfResponses   = new DataStorage("time_of_response");
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Re-sends the OBD-II request.
        /// </summary>
        /// <param name="outputBuffer"></param>
        protected void ResendCommand(OBDConnectionService connectionService)
        {
            // Check that we're actually connected before trying anything
            if (connectionService.GetState() != OBDConnectionService.STATE_CONNECTED)
            {
                Toast.MakeText(Application.Context, Resource.String.not_connected, ToastLength.Short).Show();
                return;
            }
            // adds the carriage return character
            var message = new Java.Lang.String("\r");

            // Get the message bytes and tell the BluetoothConnectionService to write
            byte[] send = message.GetBytes();
            connectionService.Write(send);
            // Sleep Thread cause it has done its job (to check)
            if (responseDelayInMs != null && responseDelayInMs.Milliseconds > 0)
            {
                Thread.Sleep(responseDelayInMs.Milliseconds);
            }
        }
Ejemplo n.º 4
0
            public AcceptThread(OBDConnectionService service)
            {
                _service = service;
                BluetoothServerSocket tmp = null;

                // Create a new listening server socket
                try
                {
#if (TO_PHONE)
                    tmp = _service._adapter.ListenUsingRfcommWithServiceRecord(NAME, MY_UUID_PHONE);
#endif
#if (TO_OBD)
                    tmp = _service._adapter.ListenUsingRfcommWithServiceRecord(NAME, MY_UUID_OBD);
#endif
                }
                catch (Java.IO.IOException e)
                {
                    Log.Error(TAG, "listen() failed", e);
                }
                mmServerSocket = tmp;
            }
Ejemplo n.º 5
0
            public ConnectedThread(BluetoothSocket socket, OBDConnectionService service)
            {
                Log.Debug(TAG, "create ConnectedThread: ");

                mmSocket = socket;
                _service = service;
                Stream tmpIn  = null;
                Stream tmpOut = null;

                // Get the BluetoothSocket input and output streams
                try
                {
                    tmpIn  = socket.InputStream;
                    tmpOut = socket.OutputStream;
                }
                catch (Java.IO.IOException e)
                {
                    Log.Error(TAG, "temp sockets not created", e);
                }

                mmInStream  = tmpIn;
                mmOutStream = tmpOut;
            }
Ejemplo n.º 6
0
            public ConnectThread(BluetoothDevice device, OBDConnectionService service)
            {
                mmDevice = device;
                _service = service;
                BluetoothSocket tmp = null;

                // Get a BluetoothSocket for a connection with the
                // given BluetoothDevice
                try
                {
#if (TO_PHONE)
                    tmp = device.CreateRfcommSocketToServiceRecord(MY_UUID_PHONE);
#endif
#if (TO_OBD)
                    tmp = device.CreateRfcommSocketToServiceRecord(MY_UUID_OBD);
#endif
                }
                catch (Java.IO.IOException e)
                {
                    Log.Error(TAG, "create() in connected failed", e);
                }
                mmSocket = tmp;
            }
Ejemplo n.º 7
0
        private void SetupConnection()
        {
            // Initialize the BluetoothChatService to perform bluetooth connections
            Handler myHandler = new MyHandler(this);

            connectionService = new OBDConnectionService(this, myHandler);

            // Here initialize the main activity layout:

            // Initialize the array adapter for the conversation thread
            conversationArrayAdapter = new ArrayAdapter <string>(this, Resource.Layout.message);
            conversationView         = FindViewById <ListView>(Resource.Id.@in);
            conversationView.Adapter = conversationArrayAdapter;

            /* Initialize the AT commands buttons with a listener that for click events */

            button_atReset        = FindViewById <Button>(Resource.Id.button_atReset);
            button_atReset.Click += delegate(object sender, EventArgs e) {
                SendCommand(ListOfCommands.AT_Reset);
                string s = connectionService.Read();
                conversationArrayAdapter.Add("Rec:" + s);
            };
            button_atAutomaticProtocol        = FindViewById <Button>(Resource.Id.button_atAutomaticProtocol);
            button_atAutomaticProtocol.Click += delegate(object sender, EventArgs e) {
                SendCommand(ListOfCommands.AT_AutomaticProtocol);
                string s = connectionService.Read();
                conversationArrayAdapter.Add("Rec:" + s);
            };
            button_atReadVoltage        = FindViewById <Button>(Resource.Id.button_atReadVoltage);
            button_atReadVoltage.Click += delegate(object sender, EventArgs e) {
                SendCommand(ListOfCommands.AT_ReadVoltage);
                string s = connectionService.Read();
                conversationArrayAdapter.Add("Rec:" + s);
            };
            button_atEchoOff        = FindViewById <Button>(Resource.Id.button_atEchoOff);
            button_atEchoOff.Click += delegate(object sender, EventArgs e) {
                SendCommand(ListOfCommands.AT_ECO_Off);
                string s = connectionService.Read();
                conversationArrayAdapter.Add("Rec:" + s);
            };
            button_atLinefeedsOff        = FindViewById <Button>(Resource.Id.button_atLinefeedsOff);
            button_atLinefeedsOff.Click += delegate(object sender, EventArgs e) {
                SendCommand(ListOfCommands.AT_Linefeeds_Off);
                string s = connectionService.Read();
                conversationArrayAdapter.Add("Rec:" + s);
            };

            /* Initialize the OBD commands buttons with a listener that for click events */

            button_obdRPM        = FindViewById <Button>(Resource.Id.button_obdRPM);
            button_obdRPM.Click += delegate(object sender, EventArgs e) {
                SendCommand(ListOfCommands.OBD_rpmCommand);
                string s = connectionService.Read();
                conversationArrayAdapter.Add("Rec:" + s);
            };
            button_obdSpeed        = FindViewById <Button>(Resource.Id.button_obdSpeed);
            button_obdSpeed.Click += delegate(object sender, EventArgs e)
            {
                SendCommand(ListOfCommands.OBD_speedCommand);
                string s = connectionService.Read();
                conversationArrayAdapter.Add("Rec:" + s);
            };

            /* Initialize the buttons for periodic requests with a listener that for click events */

            button_startMeasure        = FindViewById <Button>(Resource.Id.button_startMeasure);
            button_startMeasure.Click += delegate(object sender, EventArgs e) {
                // launch the thread to begin continuous request, if not already launched
                if (requestCommandThread == null)
                {
                    requestCommandThread = new RequestCommandThread(connectionService);
                    requestCommandThread.Start();
                    // start timer to show the seconds flow
                    time_sec = 0;
                    timeFlow = CreateTimer(myHandler, TIMER_TIMEFLOW_RAISED, 1000);
                }
            };
            button_stopMeasure        = FindViewById <Button>(Resource.Id.button_stopMeasure);
            button_stopMeasure.Click += delegate(object sender, EventArgs e) {
                // stop measure session
                if (requestCommandThread != null && requestCommandThread.isRunning == false)
                {
                    // stop time flow timer
                    timeFlow.Dispose();
                    // cancel thread
                    //requestCommandThread.Cancel(); //done automatically by Run()
                    requestCommandThread = null;
                }
            };

            // Here there are universal operations:

            // Initialize the BluetoothChatService to perform bluetooth connections
            // -> done at the beginning of this function
        }