Android OBDKey蓝牙
我想通过蓝牙将我的摩托罗拉droid连接到OBDKey。我以Android OBDKey蓝牙,android,bluetooth,Android,Bluetooth,我想通过蓝牙将我的摩托罗拉droid连接到OBDKey。我以BluetoothChat为例连接蓝牙,KWP作为协议 然后我编写byte[]命令 command[0]=rawToByte(0x02); command[1]=rawToByte(0x85); command[2]=rawToByte(0x05); command[3]=rawToByte(0xc7); command[4]=rawToByte(0x33); command[5]=rawToByte(0xf1); com
BluetoothChat
为例连接蓝牙,KWP作为协议
然后我编写byte[]命令
command[0]=rawToByte(0x02);
command[1]=rawToByte(0x85);
command[2]=rawToByte(0x05);
command[3]=rawToByte(0xc7);
command[4]=rawToByte(0x33);
command[5]=rawToByte(0xf1);
command[6]=rawToByte(0x00);
command[7]=rawToByte(0x00);
command[8]=rawToByte(0x00);
command[9]=rawToByte(0x00);
command[10]=rawToByte(0x00);
command[11]=rawToByte(0x00);
command[12]=rawToByte(0x00);
command[13]=rawToByte(0x00);
command[14]=rawToByte(0x76);
其中函数“rawToByte”是:
在OBDKey发送到设备字节0x02的结果中,该值是第一个命令,因此它复制值。我做错了什么?OBDKey是基于ELM327的蓝牙OBD适配器。就功能和使用方法而言,它与DealExtreme和scantool.net蓝牙适配器一样。后两种我很熟悉 假设您已经打开了套接字和流,并且输出套接字名为mBTOutputStream,下面介绍如何向设备发送字符串
/**
* Send the exact string provided.
* We don't append a CRLF or anything like that - we just send the exact string to the device as-is.
* @param sendThis - exact string to send to the device.
* @return - returns true unless a problem occurs, in which case we return false;
*/
public boolean sendRaw(String sendThis) {
// Ya can't send data if we're not connected!
if (isConnected() != true)
return false;
byte bsendThis[] = sendThis.getBytes();
try {mBTOutputStream.write(bsendThis);} catch (Exception e) {
ioErrorOccurredDuringOutput();
return false;
}
ioResetOutputErrorCount();
return true;
}
还请记住还要从设备读取数据,否则输入缓冲区将变满并开始阻塞I/O。OBDKey将实际为您封装KWP2000帧(在默认ASCII模式下) 您只需告诉OBDKey装置将KWP2000协议用于ATSP5\r,然后在初始化后,将您的请求作为“模式pid”发送,例如010C以获得发动机转速 查看您的数据,很难确定您正在尝试做什么。您指定的数据是:
02,85,05,C7,33,F1,00,00,00,00,00,00,76
通常,第一个字节是头加长度,然后是目标地址字节、源地址字节、数据,然后是校验和,例如
C2,33、F1,01,00、E7
。要通过OBDKey sendATSP5\r
发送此示例,则0100\r
和OBDKey将获取车辆诊断协议框架的car。感谢您的响应,但字符串“sendThis”是什么?您可以使用如下方法:sendRaw(“ATI”+“\n”)代码>因此,当您执行该方法时,它会发送字符串“sendThis”。为什么我必须发送到设备字符串,而它只理解字节数组(命令)?ioResetOutputErrorCount()函数的作用是什么?ioError/ioReset方法用于跟踪我的应用程序中的IO错误。我使用它作为额外的安全措施,以了解连接的完整性。您可以在使用该方法之前删除它们。至于使用字符串的原因,在与该设备通信时使用字符串并让sendRaw()
方法在最后一分钟将字符串转换为字节数组会让您的生活变得更好,因此您不必这样做。您已经获得设备的数据表了吗?这样您就知道要发送什么命令了吗?事实上,我已经为ELM327蓝牙设备编写了一个端到端应用程序。它作为VoyagerConnect上市。我将来可能会发布部分源代码。
/**
* Send the exact string provided.
* We don't append a CRLF or anything like that - we just send the exact string to the device as-is.
* @param sendThis - exact string to send to the device.
* @return - returns true unless a problem occurs, in which case we return false;
*/
public boolean sendRaw(String sendThis) {
// Ya can't send data if we're not connected!
if (isConnected() != true)
return false;
byte bsendThis[] = sendThis.getBytes();
try {mBTOutputStream.write(bsendThis);} catch (Exception e) {
ioErrorOccurredDuringOutput();
return false;
}
ioResetOutputErrorCount();
return true;
}