Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Slow speed after connection to socket #127

Open
Odmink0 opened this issue Feb 14, 2017 · 0 comments
Open

Slow speed after connection to socket #127

Odmink0 opened this issue Feb 14, 2017 · 0 comments

Comments

@Odmink0
Copy link

Odmink0 commented Feb 14, 2017

Hello. First of all: thank you for obd-java-api!

I trying to develop own OBDII trouble codes scanner app for Android phones.
For connection and reading data i use this obd-java-api.
For testing i use software OBDsim on my laptop and ELM327 Mini(small blue dongle) on my car.

Connection and TroubleCodesCommand run wery well on OBDsim. But when i trying to connect to my car i got fail: after opening a BT socket, obd-api run at very-very-very slow speed and can execute ObdResetCommand 2-3 minutes. At that time app Torque Lite on same ELM and same car connect fast and read trouble codes successfully.

My code
`import android.app.Application;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Process;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.pires.obd.commands.control.TroubleCodesCommand;
import com.github.pires.obd.commands.protocol.AdaptiveTimingCommand;
import com.github.pires.obd.commands.protocol.EchoOffCommand;
import com.github.pires.obd.commands.protocol.HeadersOffCommand;
import com.github.pires.obd.commands.protocol.LineFeedOffCommand;
import com.github.pires.obd.commands.protocol.ObdResetCommand;
import com.github.pires.obd.commands.protocol.SelectProtocolCommand;
import com.github.pires.obd.commands.protocol.SpacesOffCommand;
import com.github.pires.obd.commands.protocol.TimeoutCommand;
import com.github.pires.obd.enums.ObdProtocols;
import com.github.pires.obd.exceptions.NoDataException;
//import com.github.pires.obd.reader.io.BluetoothManager;

import java.io.IOException;
import java.net.Socket;
import java.util.UUID;

import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE;

public class BTConnection {
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
private static final String TAG = "Activity";
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
private Context context;

String result="";
TextView debug, tempdebug;
Button elmbtn;
ProgressBar elmprogress;
EditText input;
LinearLayout resultlayout;
TextView alldtc;

public BTConnection(Context context){
    this.context = context;

    resultlayout = (LinearLayout)((OActivity)context).findViewById(R.id.dtcoutput);
    debug = (TextView)((OActivity)context).findViewById(R.id.debugview);        
    debug.setText("Debug ON");

    elmbtn = (Button)((OActivity)context).findViewById(R.id.elmbtn);
    elmprogress = (ProgressBar) ((OActivity)context).findViewById(R.id.pbar);
    input = (EditText)((OActivity)context).findViewById(R.id.input_code);
    alldtc = (TextView)((OActivity)context).findViewById(R.id.dtccounter);
    //resultlayout.removeAllViewsInLayout();
    // resultlayout.setVisibility(View.GONE);
}


public class ConnectThread extends AsyncTask<BluetoothDevice, String, String> {

    protected void onPreExecute(){
        elmprogress.setProgress(0);
        elmbtn.setEnabled(false);
        resultlayout.removeAllViewsInLayout();
        alldtc.setText(context.getResources().getString(R.string.currentdtcelm));
    }
    protected String doInBackground(BluetoothDevice... device) {            
        Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY_MORE_FAVORABLE);
        
        Log.d(TAG,"Start connection Thread");
        
        synchronized (this) {
            BluetoothSocket tmp = null;
            mmDevice = device[0];

            try {
                // Get a BluetoothSocket to connect with the given BluetoothDevice.
                // MY_UUID is the app's UUID string, also used in the server code.
                tmp = device[0].createRfcommSocketToServiceRecord(MY_UUID);
                Log.d(TAG, "Create socket from UUID success");
                publishProgress("Create socket from UUID success");

                //debug.setText("Create socket from UUID success");
            } catch (IOException e) {
                Log.e(TAG, "Socket's create() method failed", e);
                publishProgress("Create socket from UUID failed");
            }
            mmSocket = tmp;

            final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            mBluetoothAdapter.cancelDiscovery();

            Log.d(TAG, "Cancel discovery");
            publishProgress("Cancel discovery");
            try {                    
                mmSocket.connect();
                Log.d(TAG, "Connect socket from UUID success");
                publishProgress("Connect socket from UUID success");
                ResetOBD();
                publishProgress("Reset OBD success");
                EchoOFF();
                EchoOFF();
                publishProgress("Echo OFF OBD success");
                LineFeedOFF();
                publishProgress("LineFeedOFF OBD success");
                SpacesOFF();
                HeadersOff();
                AdaptiveTiming();
                //SetTimeout();
                //publishProgress("Set timeout success");
                OBDSetProtocolAuto();
                publishProgress("Set OBD protocols auto success");
                result = ReadTroubleCodes();
                publishProgress(context.getString(R.string.text_field));

            } catch (IOException connectException) {
                // Unable to connect; close the socket and return.
                try {
                    mmSocket.close();
                } catch (IOException closeException) {
                    Log.d(TAG, "Could not close the client socket", closeException);
                    publishProgress("Could not close the client socket");
                }
            }

        }
        return result;
    }

    protected void onProgressUpdate(String... progress) {
        //debug.setText(debug.getText()+"\n"+progress[0]);
        elmprogress.incrementProgressBy(1);
        input.setHint(progress[0]);
    }

    protected void onPostExecute(String result) {
        try {                
            mmSocket.close();
            elmprogress.incrementProgressBy(1);
            Log.d(TAG,"Close socket from UUID success");
            //debug.setText(debug.getText()+"\n"+"Close socket from UUID success");
        } catch (IOException e) {
            Log.e(TAG, "Could not close the client socket", e);
            //debug.setText(debug.getText()+"\n"+"Could not close the client socket");
        }
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
        elmbtn.setEnabled(true);                        
    }

    public void ResetOBD(){
        try {
            new ObdResetCommand().run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            Log.d(TAG,"Reset OBD success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }

    }

    public void EchoOFF(){
        try {
            new EchoOffCommand().run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            Log.d(TAG,"Echo OFF OBD success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }

    }

    public void LineFeedOFF(){
        try {
            new LineFeedOffCommand().run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            Log.d(TAG,"Line Feed OFF OBD success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
    }

    public void SetTimeout(){
        try {
            new TimeoutCommand(125).run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            Log.d(TAG,"Set timeouts success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
    }

    public void OBDSetProtocolAuto(){
        try {
            new SelectProtocolCommand(ObdProtocols.AUTO).run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            Log.d(TAG,"Set OBD protocol to AUTO success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
    }

    public void SpacesOFF(){
        try {
            new SpacesOffCommand().run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            Log.d(TAG,"SpacesOFF success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
    }

    public void HeadersOff(){
        try {
            new HeadersOffCommand().run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            Log.d(TAG,"HeadersOff success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
    }

    public void AdaptiveTiming(){
        try {
            new AdaptiveTimingCommand(1).run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            Log.d(TAG,"AdaptiveTiming success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
    }

    public String ReadTroubleCodes(){
        String troublecodes = "";
        try {
            ModifiedTroubleCodesObdCommand tcoc = new ModifiedTroubleCodesObdCommand();
            tcoc.run(mmSocket.getInputStream(), mmSocket.getOutputStream());
            troublecodes = tcoc.getFormattedResult();
            Log.d(TAG,"Read OBD trouble codes success");
        }
        catch (IOException e){
            e.printStackTrace();
            Log.d("DTCIOERR", e.getMessage());
        }
        catch (InterruptedException e) {
            e.printStackTrace();
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
        catch (NoDataException e) {
            e.printStackTrace();
            troublecodes = " --- ";
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
        catch (StringIndexOutOfBoundsException e) {
            e.printStackTrace();
            troublecodes = " --- ";
            Log.d("DTCERR", e.getMessage());
            //mHandler.obtainMessage(OBD_COMMAND_FAILURE_IE).sendToTarget();
        }
        return troublecodes;
    }
    public class ModifiedTroubleCodesObdCommand extends TroubleCodesCommand {
        @Override
        public String getResult() {
            // remove unwanted response from output since this results in erroneous error codes
            return rawData.replace("SEARCHING...", "").replace("NODATA", "");
        }
    }
}

}
`

I call it from another class by execute
`BTConnection.ConnectThread elm = new BTConnection(this).new ConnectThread();

Log.d(TAG,"Create elm as child success");

elm.execute(device);

Log.d(TAG,"Run elm Async success");`

OBDsim software can record logs and i collect AT commands from Torque and obd-java-api. Difference in usage of space between AT and commands.

Torque log:

`ATZ
ELM327 v1.3a OBDGPSLogger

ATE0
OK
ATE0
OK
ATM0
?`

And log from my app with obd-java-api:

`AT Z
ELM327 v1.3a OBDGPSLogger

AT E0
OK
AT E0
OK`

My issue: i can't send commands and receive result using ELM327 Mini and obd-java-api.

P.S.: sorry for my english.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant