我正在开发一款Android应用,可修改Eclipse提供的蓝牙聊天示例,以创建板载诊断(OBD)阅读器。我的代码向车辆的ECU发送消息,并使用开关检索数据。
现在,我的问题是,由于某种原因,当我运行我的程序时,我只接收了6个案例的数据,而我没有收到剩下的数据。
代码有点冗长。
public void startTransmission() {
sendMessage("01 00" + '\r');
}
public void getData(int messagenumber) {
final TextView TX = (TextView) findViewById(R.id.TXView2);
switch (messagenumber) {
case 1:
sendMessage("01 0C" + '\r'); // get RPM
TX.setText("01 0C");
messagenumber++;
break;
case 2:
sendMessage("01 0D" + '\r'); // get MPH
TX.setText("01 0D");
messagenumber++;
break;
case 3:
sendMessage("01 04" + '\r'); // get Engine Load
TX.setText("01 04");
messagenumber++;
break;
case 4:
sendMessage("01 05" + '\r'); // get Coolant Temperature
TX.setText("01 05");
messagenumber++;
break;
case 5:
sendMessage("01 0F" + '\r'); // get Intake Temperature
TX.setText("01 0F");
messagenumber++;
break;
case 6:
sendMessage("AT RV" + '\r'); // get Voltage
TX.setText("AT RV");
messagenumber++;
break;
case 7:
sendMessage("01 06" + '\r'); // get Fuel Trim Bank 1 Sensor 1
TX.setText("01 06");
messagenumber++;
case 8:
sendMessage("01 07" + '\r'); // get Fuel Trim Bank 1 Sensor 2
TX.setText("01 07");
messagenumber++;
case 9:
sendMessage("01 0B" + '\r'); // get Intake Manifold Pressure
TX.setText("01 0B");
messagenumber++;
case 10:
sendMessage("01 0E" + '\r'); // get Timing Advance
TX.setText("01 0E");
messagenumber++;
case 11:
sendMessage("01 11" + '\r'); // get Throttle Position
TX.setText("01 11");
messagenumber++;
case 12:
sendMessage("01 14" + '\r'); // get Oxygen Bank 1 Sensor 1
TX.setText("01 14");
messagenumber++;
case 13:
sendMessage("01 15" + '\r'); // get Oxygen Bank 1 Sensor 2
TX.setText("01 15");
messagenumber++;
default:
;
}
}
private void sendMessage(String message) {
// Check that we're actually connected before trying anything
if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT)
.show();
return;
}// end if
// Check that there's actually something to send
if (message.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
mChatService.write(send);
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
// mOutEditText.setText(mOutStringBuffer);
}// end if
}// end sendMessage method
// The Handler that gets information back from the BluetoothChatService
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// <--------- Initialize Data Display Fields ---------->//
/* PID = 0C */final TextView RPM = (TextView) findViewById(R.id.numRPM);
/* PID = 0D */final TextView MPH = (TextView) findViewById(R.id.numSpeed);
/* PID = 04 */final TextView engineLoad = (TextView) findViewById(R.id.numLoad);
/* PID = 05 */final TextView coolantTemperature = (TextView) findViewById(R.id.numCoolant);
/* PID = 0F */final TextView intakeTemperature = (TextView) findViewById(R.id.numIntakeAir);
/* PID = RV */final TextView voltage = (TextView) findViewById(R.id.numVoltage);
/* PID = 06 */final TextView fuelTrimB1S1 = (TextView) findViewById(R.id.numFTb1s1);
/* PID = 07 */final TextView fuelTrimB1S2 = (TextView) findViewById(R.id.numFTb1s2);
/* PID = 0A */final TextView intakeManifold = (TextView) findViewById(R.id.numManifold);
/* PID = 0E */final TextView timingAdvance = (TextView) findViewById(R.id.numTiming);
/* PID = 11 */final TextView throttlePos = (TextView) findViewById(R.id.numThrottle);
/* PID = 14 */final TextView oxygenB1s1 = (TextView) findViewById(R.id.numO2b1s1);
/* PID = 15 */final TextView oxygenB1s2 = (TextView) findViewById(R.id.numO2b1s2);
/* displays recieved string */final TextView RX = (TextView) findViewById(R.id.RXView2);
String dataRecieved;
int value = 0;
int value2 = 0;
int PID = 0;
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);
// ------- 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(" ");
if ((bytes[0] != null) && (bytes[1] != null)) {
PID = Integer.parseInt(bytes[0].trim(), 16);
value = Integer.parseInt(bytes[1].trim(), 16);
}
switch (PID) {
case 15:// PID(0F): Intake Temperature
value = value - 40; // Formula for Intake Temperature
value = ((value * 9) / 5) + 32; // Convert from Celsius
// to Farenheit
String displayIntakeTemp = String.valueOf(value);
intakeTemperature.setText(displayIntakeTemp + " F");
break;
// end Intake Temperature case
case 4:// PID(04): Engine Load
value = (value * 100) / 255;
String displayEngineLoad = String.valueOf(value);
engineLoad.setText(displayEngineLoad + " %");
break;
// end Engine Load case
case 5:// PID(05): Coolant Temperature
value = value - 40;
value = ((value * 9) / 5) + 32; // convert to deg F
String displayCoolantTemp = String.valueOf(value);
coolantTemperature.setText(displayCoolantTemp);
break;
// end Coolant Temperature case
case 12: // PID(0C): RPM
int RPM_value = (value * 256) / 4;
String displayRPM = String.valueOf(RPM_value);
RPM.setText(displayRPM);
break;
// end RPM case
case 13:// PID(0D): MPH
value = (value * 5) / 8; // convert KPH to MPH
String displayMPH = String.valueOf(value);
MPH.setText(displayMPH + " MPH");
break;
// end MPH case
case 6:// PID(06): Fuel Trim Bank 1 Sensor 1
value = (value - 128) * 100 / 128;
String displayFtB1s1 = String.valueOf(value);
fuelTrimB1S1.setText(displayFtB1s1);
break;
case 7:// PID(07): Fuel Trim Bank 1 Sensor 2
value = (value - 128) * 100 / 128;
String displayFtB1s2 = String.valueOf(value);
fuelTrimB1S2.setText(displayFtB1s2);
break;
case 11: // PID(0A): Intake Manifold Pressure
String displayIntakeManifold = String.valueOf(value);
intakeManifold.setText(displayIntakeManifold);
break;
case 14: // PID(0E): Timing Advance
value = value / 2 - 64;
String displayTimingAdvance = String.valueOf(value);
timingAdvance.setText(displayTimingAdvance);
break;
case 17: // PID(11): Throttle Position
value = value * 100 / 255;
String displayThrottlePos = String.valueOf(value);
throttlePos.setText(displayThrottlePos);
case 20: // PID(14): Oxygen Bank 1 Sensor 1
String displayO2B1s1 = String.valueOf(value);
oxygenB1s1.setText(displayO2B1s1);
break;
case 21: // PID(14): Oxygen Bank 1 Sensor 2
String displayO2B1s2 = String.valueOf(value);
oxygenB1s2.setText(displayO2B1s2);
break;
default:
;
}
} else if ((dataRecieved != null)
&& (dataRecieved
.matches("\\s*[0-9A-Fa-f]{1,2} [0-9A-Fa-f]{2} [0-9A-Fa-f]{2}\\s*\r?\n?"))) {
dataRecieved = dataRecieved.trim();
String[] bytes = dataRecieved.split(" ");
if ((bytes[0] != null) && (bytes[1] != null)
&& (bytes[2] != null)) {
PID = Integer.parseInt(bytes[0].trim(), 16);
value = Integer.parseInt(bytes[1].trim(), 16);
value2 = Integer.parseInt(bytes[2].trim(), 16);
}
// PID(0C): RPM
if (PID == 12) {
int RPM_value = ((value * 256) + value2) / 4;
String displayRPM = String.valueOf(RPM_value);
RPM.setText(displayRPM + " RPM");
} else if ((PID == 1) || (PID == 65)) {
switch (value) {
case 15:// PID(0F): Intake Temperature
value2 = value2 - 40; // formula for INTAKE AIR TEMP
value2 = ((value2 * 9) / 5) + 32; // convert to deg
// F
String displayIntakeTemp = String.valueOf(value2);
intakeTemperature
.setText(displayIntakeTemp + " °F");
break;
// end Intake Temperature case
case 4:// PID(04): Engine Load
value2 = (value2 * 100) / 255;
String displayEngineLoad = String.valueOf(value2);
engineLoad.setText(displayEngineLoad + " %");
break;
// end Engine Load case
case 5:// PID(05): Coolant Temperature
value2 = value2 - 40;
value2 = ((value2 * 9) / 5) + 32; // convert to deg
// F
String displayCoolantTemp = String.valueOf(value2);
coolantTemperature.setText(displayCoolantTemp
+ " °F");
break;
case 13:// PID(0D): MPH
value2 = (value2 * 5) / 8; // convert to MPH
String displayMPH = String.valueOf(value2);
MPH.setText(displayMPH + " MPH");
break;
// case 6:// PID(06): Fuel Trim Bank 1 Sensor 1
//
// value = (value - 128) * 100 / 128;
//
// String displayFtB1s1 = String.valueOf(value);
// fuelTrimB1S1.setText(displayFtB1s1 + " V");
// break;
//
// case 7:// PID(07): Fuel Trim Bank 1 Sensor 2
//
// value = (value - 128) * 100 / 128;
//
// String displayFtB1s2 = String.valueOf(value);
// fuelTrimB1S2.setText(displayFtB1s2 + " V");
// break;
//
// case 11: // PID(0A): Intake Manifold Pressure
//
// String displayIntakeManifold = String
// .valueOf(value);
// intakeManifold.setText(displayIntakeManifold
// + " psi");
// break;
//
// case 14: // PID(0E): Timing Advance
//
// value = value / 2 - 64;
//
// String displayTimingAdvance = String.valueOf(value);
// timingAdvance.setText(displayTimingAdvance + " °");
// break;
//
// case 17: // PID(11): Throttle Position
//
// value = value * 100 / 255;
//
// String displayThrottlePos = String.valueOf(value);
// throttlePos.setText(displayThrottlePos + " %");
//
// case 20: // PID(14): Oxygen Bank 1 Sensor 1
//
// String displayO2B1s1 = String.valueOf(value);
// oxygenB1s1.setText(displayO2B1s1 + " V");
// break;
//
// case 21: // PID(14): Oxygen Bank 1 Sensor 2
//
// String displayO2B1s2 = String.valueOf(value);
// oxygenB1s2.setText(displayO2B1s2 + " V");
// break;
default:
;
}
}
} else if ((dataRecieved != null)
&& (dataRecieved
.matches("\\s*[0-9]+(\\.[0-9]?)?V\\s*\r*\n*"))) {
dataRecieved = dataRecieved.trim();
voltage.setText(dataRecieved);
} else if ((dataRecieved != null)
&& (dataRecieved
.matches("\\s*[0-9]+(\\.[0-9]?)?V\\s*V\\s*>\\s*\r*\n*"))) {
dataRecieved = dataRecieved.trim();
// String volt_number = dataRecieved.substring(0,
// dataRecieved.length() - 1);
voltage.setText(dataRecieved);
} else if ((dataRecieved != null)
&& (dataRecieved
.matches("\\s*[ .A-Za-z0-9\\?*>\r\n]*\\s*>\\s*\r*\n*"))) {
if (message_number == 7)
message_number = 1;
getData(message_number++);
}
break;
}
}
我认为我想关注的地方是:
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(" ");
if ((bytes[0] != null) && (bytes[1] != null)) {
PID = Integer.parseInt(bytes[0].trim(), 16);
value = Integer.parseInt(bytes[1].trim(), 16);
}
此代码基于我所做的一些研究,我不确定dataRecieved.matches背后的字符串是什么。它看起来像一个数组,我认为收到的参数导致我的开关案例不会触发。
感谢您的任何意见。