Android OBD-II Reader library that support standard PIDs (Mode 01) in Mobile and Head Unit (insatalled in Car).
I was working for an application that read Car real time data through OBD-II. I found a library https://github.com/pires/obd-java-api , it is working fine in mobile, but there is problem to connect OBD with with Head Unit. So, for connectivity of Car Head Unit with OBD-II and read real time data, we spend lots of time because of no proper documentation and connectivity guide. Therefore, I developed this library so that if anyone looking for the same, they can use and save time.
Check which PIDs supported in your vehicle if you do not set any command. Based on standard PIDs, we are calculating followings:
Add the following to your build.gradle to use:
dependencies {
compile 'com.sohrab:obd-reader:1.0.0'
}
Follow sample-app:
ArrayList<ObdCommand> obdCommands = new ArrayList<>();
obdCommands.add(new SpeedCommand());
obdCommands.add(new RPMCommand());
ObdConfiguration.setmObdCommands(this, obdCommands);
//If you want to read all standard PIDs commands, pass null in the second argument like
ObdConfiguration.setmObdCommands(this, null);
//Set gas price per liter so that gas cost can be calculated accordingly. Default is 7 $/l
float gasPrice = 7; // per litre, you should initialize according to your requirement.
ObdPreferences.get(this).setGasPrice(gasPrice);
//Register receiver with some action related to OBD connection status and read PID values
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_READ_OBD_REAL_TIME_DATA);
intentFilter.addAction(ACTION_OBD_CONNECTION_STATUS);
registerReceiver(mObdReaderReceiver, intentFilter);
//start service that keep running in background for connecting and execute command until you stop
startService(new Intent(this, ObdReaderService.class));
//Broadcast Receiver to receive OBD connection status and real time data
BroadcastReceiver mObdReaderReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ACTION_OBD_CONNECTION_STATUS)) {
String connectionStatusMsg = intent.getStringExtra(ObdReaderService.INTENT_OBD_EXTRA_DATA); if (connectionStatusMsg.equals(getString(R.string.obd_connected))) {
//OBD connected do what want after OBD connection
} else if (connectionStatusMsg.equals(getString(R.string.connect_lost))) {
//OBD disconnected do what want after OBD disconnection
} else {
// here you could check OBD connection and pairing status
}
} else if (action.equals(ACTION_READ_OBD_REAL_TIME_DATA)) {
TripRecord tripRecord = TripRecord.getTripRecode(SampleActivity.this);
// here you can fetch real time data from TripRecord using getter methods like
//tripRecord.getSpeed();
//tripRecord.getEngineRpm();
}
}
};
Thank you to all. It feels good to see that our work is appreciated.
This project is licensed under the Apache License, Version 2.0 - see the LICENSE.md file for details