I'm working on a OBD-ii Reader.
I am using a basic bluetooth chat to communicate with the OBD-ii device, regular expressions to capture the replies and displaying them in text views.
This is where I send the PID code's to request the data:
public void getData(int messagenumber) {
final TextView TX = (TextView) findViewById(R.id.TXView2);
switch(messagenumber) {
case 1:
sendMessage("01 2F" + '\r'); //get Fuel %
TX.setText("01 2F");
Log.d("Case1: ", String.valueOf(messagenumber));
messagenumber++;
Log.d("Case1: ", String.valueOf(messagenumber));
break;
case 2:
sendMessage("01 31" + '\r'); //get Mileage
TX.setText("01 31");
Log.d("Case2: ", String.valueOf(messagenumber));
messagenumber++;
Log.d("Case2: ", String.valueOf(messagenumber));
break;
case 3:
sendMessage("01 0C" + '\r'); //get RPM
TX.setText("01 0C");
Log.d("Case3: ", String.valueOf(messagenumber));
messagenumber++;
Log.d("Case3: ", String.valueOf(messagenumber));
break;
default: ;
}
}
Then pick up the data here:
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
switch (msg.arg1) {
case BluetoothChatService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mConversationArrayAdapter.clear();
break;
case BluetoothChatService.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case BluetoothChatService.STATE_LISTEN:
case BluetoothChatService.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
Log.d("BYTES: ", readMessage);
// ------- ADDED CODE FOR OBD -------- //
dataRecieved = readMessage;
RX.setText(dataRecieved);
if((dataRecieved != null) && (dataRecieved.matches("\\s*[0-9A-Fa-f]{2} [0-9A-Fa-f]{2}\\s*\r?\n?" ))) {
dataRecieved = dataRecieved.trim();
String[] bytes = dataRecieved.split(" ");
Log.d("DR 1val: ",dataRecieved + "--!");
if((bytes[0] != null)&&(bytes[1] != null)) {
PID = Integer.parseInt(bytes[0].trim(), 16);
value = Integer.parseInt(bytes[1].trim(), 16);
}
if (PID == 47) {
/*case 47: //PID(2F): Fuel % */
int fuelValue = ((value*100)/255);
String displayFuel = String.valueOf("PID: " + PID + "Val: " + value + "fuel: " + fuelValue + " %");
Fuel.setText(displayFuel);
/*break;
default: ;*/
}
}
else if((dataRecieved != null) && (dataRecieved.matches("\\s*[0-9A-Fa-f]{2} [0-9A-Fa-f]{2} [0-9A-Fa-f]{2}\\s*\r?\n?" ))) {
dataRecieved = dataRecieved.trim();
String[] bytes = dataRecieved.split(" ");
Log.d("DR 2vals: ",dataRecieved + "--!");
if((bytes[0] != null)&&(bytes[1] != null)&&(bytes[2] != null)) {
PID = Integer.parseInt(bytes[0].trim(), 16);
//if (PID == 12){
value1 = Integer.parseInt(bytes[1].trim(), 16);
value2 = Integer.parseInt(bytes[2].trim(), 16);
/*}
else if (PID == 49){
//mileVal = Integer.parseInt(bytes[1].trim(), 16);
}*/
}
//PID(0C): RPM
if (PID == 12) {
int RPM_value = ((value1*256)+value2)/4;
String displayRPM = String.valueOf("PID: " + PID + "A: " + value1 + " B: " + value2 + "RPM: " + RPM_value);
Throttle.setText(displayRPM);
}
else if (PID == 49) {
//PID(31): Distance Travelled (A*256)+B
int miles = (int) (((value1*256)+value2)*0.62137);
//String displayDistance = String.valueOf(miles + " miles");
String displayDistance = String.valueOf("PID: " + PID + "A: " + value1 + " B: " + value2 + "Miles: " + miles);
Distance.setText(displayDistance);
}
}
I then use this regular expression to listen for the > which means the OBD is finished processing the command and this should then go back to the start and request the PID codes again.
else if((dataRecieved != null) && (dataRecieved.matches("\\s*[ .A-Za-z0-9\\?*>\r\n]*\\s*>\\s*\r*\n*" ))) {
if(message_number == 4){
message_number = 1;
}
getData(message_number++);
}
When I run the application it takes a long time to start giving me any values, however they are accurate. I have a log of the data received from the OBD. My log however is coming out with some stuff I don't understand. I also notice that the textView that shows me the dataRecieved comes up with unknown characters and says STOPPED, then freezes and starts up again after a short while. Here is some of the log...
D/TextLayoutCache(5146): Cache value 0x520da3b8 deleted, size = 136
V/BluetoothSocket.cpp(5146): readNative
D/TextLayoutCache(5146): Cache value 0x4dbd5e00 deleted, size = 176
V/BluetoothSocket.cpp(5146): readNative
V/BluetoothSocket.cpp(5146): readNative
D/TextLayoutCache(5146): Cache value 0x4dfead50 deleted, size = 240
D/TextLayoutCache(5146): Cache value 0x520dcb10 deleted, size = 240
V/BluetoothSocket.cpp(5146): readNative
D/BYTES:(5146): C
D/BYTES:(5146): 4
D/BYTES:(5146): 4
D/BYTES:(5146): 4