我正在开发 OBD-ii 阅读器。
我正在使用基本的蓝牙聊天与 OBD-ii 设备进行通信,使用正则表达式来捕获回复并在文本视图中显示它们。
这是我发送 PID 代码以请求数据的地方:
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: ;
}
}
然后在这里取数据:
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);
}
}
然后我使用这个正则表达式来监听 > 这意味着 OBD 已经完成了对命令的处理,然后应该回到开始并再次请求 PID 代码。
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++);
}
当我运行应用程序时,需要很长时间才能开始给我任何值,但是它们是准确的。我有从 OBD 收到的数据的日志。然而,我的日志出现了一些我不明白的东西。我还注意到向我显示 dataRecieved 的 textView 出现了未知字符并说 STOPPED,然后冻结并在片刻后再次启动。这是一些日志...
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