首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法与ELM327蓝牙通信

无法与ELM327蓝牙通信
EN

Stack Overflow用户
提问于 2013-08-26 00:56:23
回答 2查看 2.1K关注 0票数 1

以下是我的代码,用于建立与ELM327的蓝牙连接,并使用笔记本电脑进行通信:

代码语言:javascript
复制
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");
    }
}


}

该程序提供以下输出:

代码语言:javascript
复制
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 object.Where发送的AT命令提供任何响应。我是不是错了?该设备在我的手机应用程序上提供速度和转速的读数。所以它不可能是有缺陷的。

EN

回答 2

Stack Overflow用户

发布于 2013-08-26 15:32:36

我不知道p.Writer.flush()是否已经这样做了,但是尝试在每个命令之后包含一个回车符,这样设备就知道命令结束了。

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

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

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

票数 2
EN

Stack Overflow用户

发布于 2013-08-31 00:43:25

很抱歉写这篇文章作为回答,因为我没有足够的名气来添加评论。

尝试一下:先发送数据,然后关闭接收方的inputStream (或套接字)。它适用于Java中的UDP。Java中接收UDP的方法在收到数据后不会停止等待,即使缓冲区中没有更多的空间也是如此。也许蓝牙接收方法也有同样的问题。

我无法关闭我的Android设备上接收器的插座。如果有帮助,请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18431424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档