md-sohrab-alam / android-obd-reader

Android OBD-II Reader application that support standard PIDs Mode 01
78 stars 39 forks source link

[Help wanted] Data update delay issue #3

Closed mjm918 closed 5 years ago

mjm918 commented 5 years ago

This library works totally fine. But i have been facing one issue. Let's say if I set ObdConfiguration.setmObdCommands(this, null); to add all the available commands and later get any value (eg: tripRecord.getEngineRpm()), it doesn't return real time data. It takes few seconds to update. But in ObdConfiguration.setmObdCommands, if I set few commands, the updating time is really fast and works like a charm. Is there any way to improve this? thanks

md-sohrab-alam commented 5 years ago

You are correct, Since total default commands is 37 and we are executing in a single thread, so every command repeat only if other 36 completed, therefore it is taking bit more time. I think, you should set the the command which you need because only few commands are useful. However, If you really need all commands and some command execute faster, then you have to import my library as module and do some changes as follows.

1: goto executeCommand() method inside ObdReaderService.java file e.g. for getting fast RPM

private void executeCommand() {
    L.i("executing commands thread is :: " + Thread.currentThread().getId());
    TripRecord tripRecord = TripRecord.getTripRecode(this);
    ArrayList<ObdCommand> commands = (ArrayList<ObdCommand>) ObdConfiguration.getmObdCommands().clone();
    int count = 0;
    RPMCommand rpmCommand = new RPMCommand();
    while (mSocket != null && mSocket.isConnected() && commands.size() > count && isConnected && ObdPreferences.get(getApplicationContext()).getServiceRunningStatus()) {

        ObdCommand command = commands.get(count);
        try {

            L.i("command run :: " + command.getName());
            command.run(mSocket.getInputStream(), mSocket.getOutputStream());
            rpmCommand.run(mSocket.getInputStream(), mSocket.getOutputStream());

            L.i("result is :: " + command.getFormattedResult() + " :: name is :: " + command.getName());
            tripRecord.updateTrip(command.getName(), command);
            rpmCommand.run(mSocket.getInputStream(), mSocket.getOutputStream());
            tripRecord.updateTrip(rpmCommand.getName(), rpmCommand);

            if (mIsFaultCodeRead) {
                try {
                    TroubleCodesCommand troubleCodesCommand = new TroubleCodesCommand();
                    troubleCodesCommand.run(mSocket.getInputStream(), mSocket.getOutputStream());
                    tripRecord.updateTrip(troubleCodesCommand.getName(), troubleCodesCommand);
                    mIsFaultCodeRead = false;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            if (mIntent == null)
                mIntent = new Intent(ACTION_READ_OBD_REAL_TIME_DATA);
            sendBroadcast(mIntent);

        } catch (Exception e) {
            L.i("execute command Exception  :: " + e.getMessage());

            if (!TextUtils.isEmpty(e.getMessage()) && (e.getMessage().equals("Broken pipe") || e.getMessage().equals("Connection reset by peer"))) {
                L.i("command Exception  :: " + e.getMessage());
                setDisconnection();
            }
        }
        count++;
        if (count == commands.size()) {
            count = 0;
        }
    }
    // exit loop means connection lost, so set connection status false
    isConnected = false;
}
mjm918 commented 5 years ago

@md-sohrab-alam Thank you for your response and sorry for late. I haven't tried your solution yet but will try soon. Actually i'm working on an android app and using your library. It's very helpful and working great. You are right that we should not run all the commands at the same time. Thanks again