无法与蓝牙ELM327沟通 [英] Unable to communicate with ELM327 bluetooth
问题描述
下面是我的code建立与ELM327蓝牙连接,并使用一台笔记本电脑与它comunicating:
Here is my code for establishing a bluetooth connection with an ELM327 and comunicating with it using a laptop:
import java.io.*;
import java.util.Vector;
import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.swing.*;
public class elmbtooth implements DiscoveryListener
{
private static Object lock=new Object();
private static Vector remdevices=new Vector();
private static String connectionURL=null;
public static void main(String args[]) throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
elmbtooth obj=new elmbtooth();
LocalDevice locdevice = LocalDevice.getLocalDevice();
String add= locdevice.getBluetoothAddress();
String friendly_name= locdevice.getFriendlyName();
System.out.println("Local Bluetooth Address : "+add);
System.out.println("" +
"" +
"Local Friendly name : "+friendly_name);
DiscoveryAgent dis_agent= locdevice.getDiscoveryAgent();
System.out.println("********Locating Devices******");
dis_agent.startInquiry(DiscoveryAgent.GIAC,obj);
try
{
synchronized (lock)
{
lock.wait();
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
if(remdevices.size()<=0)
{
System.out.println("No devices found");
}
else
{
for(int i=0;i<remdevices.size();i++)
{
RemoteDevice remote_device=(RemoteDevice)remdevices.elementAt(i);
System.out.println((i+1)+".)"+remote_device.getFriendlyName(true)+" "+remote_device.getBluetoothAddress());
}
System.out.println("Choose Device to establish SPP");
int index=Integer.parseInt(b.readLine());
RemoteDevice des_device=(RemoteDevice)remdevices.elementAt(index-1);
UUID[] uuidset=new UUID[1];
uuidset[0]=new UUID("1101",true);
dis_agent.searchServices(null, uuidset, des_device, obj);
try
{
synchronized(lock)
{
lock.wait();
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
if(connectionURL==null)
{
System.out.println("Device does not support SPP.");
}
else
{
System.out.println("Device supports SPP.");
StreamConnection st_connect=(StreamConnection)Connector.open(connectionURL);
OutputStream outStream=st_connect.openOutputStream();
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
InputStream inStream=st_connect.openInputStream();
BufferedReader in=new BufferedReader(new InputStreamReader(inStream));
pWriter.write("AT Z");
pWriter.flush();
pWriter.write("AT E0");
pWriter.flush();
pWriter.write("AT SP 00");
pWriter.flush();
pWriter.write("0100");
pWriter.flush();
String line=in.readLine();
System.out.print(line);
pWriter.write("AT DP");
pWriter.flush();
}
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
if(!remdevices.contains(btDevice))
{
remdevices.addElement(btDevice);
}
}
@Override
public void servicesDiscovered(int transID, ServiceRecord[] servRecord)
{
if(!(servRecord==null)&&servRecord.length>0)
{
connectionURL=servRecord[0].getConnectionURL(0, false);
}
}
@Override
public void serviceSearchCompleted(int transID, int respCode)
{
synchronized(lock)
{
lock.notify();
}
}
@Override
public void inquiryCompleted(int discType)
{
synchronized(lock)
{
lock.notify();
}
switch(discType)
{
case DiscoveryListener.INQUIRY_COMPLETED:
System.out.println("Inquiry Completed");
break;
case DiscoveryListener.INQUIRY_TERMINATED:
System.out.println("Inquiry Terminated");
break;
case DiscoveryListener.INQUIRY_ERROR:
System.out.println("Inquiry Error");
break;
default:
System.out.println("Unknown Response Code");
}
}
}
该程序提供了以下的输出:
The program provides the following output:
BlueCove version 2.1.1-SNAPSHOT on winsock
Local Bluetooth Address : 7***********1
Local Friendly name : ******PC
********Locating Devices******
Inquiry Completed
1.)OBDII 0**********3
Choose Device to establish SPP
1
Device supports SPP.
(将code搜索设备,服务和SPP已经从提供www.jsr82.com的code借来的。我知道这是非常受到谴责,但code得到了解释和我没有看到我怎么可能使一些原来的阅读过程之后在网站上解释说。)
(The code to search devices , services and spp has been borrowed from the code provided on www.jsr82.com. I know that's highly condemnable but the code was provided with the explanation and i didn't see how I could make something original after reading the procedure explained on the website.)
控制台保持operatiing但榆树不提供由pWriter object.Where发送的AT命令任何回应我该怎么错了?该器件提供了我的手机应用程序的速度和RPM读数。因此它不能有缺陷。
The console keeps operatiing but the elm doesn't provide any response to the AT commands sent by the pWriter object.Where am I going wrong? The device provides readings for speed and RPM on my cellphone app. So it can't be defective.
推荐答案
我不知道,如果p.Writer.flush()已经这样做了,但尽量包括每个命令后回车,因此设备知道命令结束。
I don't know if the p.Writer.flush() already does this, but try to include a carriage return after each command, so the device knows the command ends.
pWriter.write(AT Z+\\ r);
pWriter.write("AT Z" + "\r");
编辑:
我认为这是默认设置为不识字的空间,不知道。很长一段时间,因为以前我和OBD-II合作。
pWriter.write(ATZ+\\ r);
I think it's default set to not read spaces, not sure. Been a long time ago since I worked with OBD-II. pWriter.write("ATZ" + "\r");
最有可能不是这种情况,因为每一个的CR(\\ r)时,设备发送回馈。所以,问题是别的地方,我想。
Most likely not the case, as each CR (\r), the device sends something back. So the problem is somewhere else, I think.
这篇关于无法与蓝牙ELM327沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!