java - 无法与 ELM327 蓝牙通信

标签 java bluetooth obd-ii

这是我与 ELM327 建立蓝牙连接并使用笔记本电脑与其通信的代码:

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");
    }
}


}

程序提供以下输出:

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.

(搜索设备、服务和 spp 的代码是从 www.jsr82.com 上提供的代码中借用的。我知道这是非常值得谴责的,但是代码提供了解释,我不知道如何制作阅读网站上解释的程序后的一些原创内容。)

控制台保持运行,但 elm 不对 pWriter 对象发送的 AT 命令提供任何响应。我哪里出错了?该设备在我的手机应用程序上提供速度和 RPM 读数。所以它不可能有缺陷。

最佳答案

我不知道 p.Writer.flush() 是否已经执行此操作,但尝试在每个命令后包含一个回车符,以便设备知道命令结束。

pWriter.write("AT Z"+ "\r");

编辑: 我认为它的默认设置为不读取空格,不确定。自从我使用 OBD-II 以来已经很久了。 pWriter.write("ATZ"+ "\r");

很可能不是这种情况,因为每个 CR (\r),设备都会发回一些东西。所以我认为问题出在其他地方。

关于java - 无法与 ELM327 蓝牙通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18431424/

相关文章:

android - android 2.1(三星 spica i5700)上的蓝牙问题,配对有效但连接无效

android - 如何请求OBD参数并接收它们

java - 了解如何计算二叉树的深度

android - Xamarin Android 蓝牙套接字连接失败

java - ArrayList <E> 类中的 addAll() 方法不起作用

ios - 在 iOS 中通过蓝牙或 wifi 发送文件

c# - 与蓝牙 OBDII 加密狗通信(我认为它是 ELM327)

java - 如何在 Android 中的 textView 上显示除示例文本之外的值

java - Jersey:如何在不更改地址栏中的 URL 的情况下重定向/转发

java - Spring是否通过Java Config的方法名称 Autowiring