Android蓝牙OBDII通讯

时间:2016-01-21 16:59:30

标签: java android multithreading bluetooth obd-ii

我不知道这有什么问题。我正在尝试从我的蓝牙ELM327发送和接收数据。扭矩工作,我已经使用终端应用程序发送命令,并返回正确的结果。所以我不知道我在这里做错了什么。这是处理所有这一切的线程。我正在使用Pires java obd库,found here

我回来了“?”对于所有初始化命令,但是当我开始循环vin命令时,它开始返回“OK”,并且在某一时刻我能够正确地获得vin,但是我不得不循环多次以获得它。

在返回套接字时进行连接。命令以"AT L0"格式发送(按照库)

out.write((cmd + "\r").getBytes()); out.flush();

我尝试将父发送和接收命令放在单独的阻塞线程中(使用Thread.join())并且没有改变任何内容。我把延迟推迟到Thread.sleep(1500)而没有任何变化。我不知道还有什么可以尝试,而且我对Android和蓝牙开发也相当新,所以我们非常感谢您给予的任何帮助。

这是我从UI-thread调用的线程的代码:

public class spawnThread implements Runnable {
    protected BlockingQueue<obj> jobsQueue = new LinkedBlockingQueue<>();
    protected Long queueCounter = 0L;
    protected BluetoothSocket sock = null;
    protected ArrayList<obj> process_list;
    protected Handler main_handler;
    protected setting_objs temp_objs = new setting_objs();
    protected BluetoothDevice bt_device;
    protected boolean initialized = false;
    protected boolean can_continue = true;

public spawnThread(Handler handler, BluetoothDevice bt_device, ArrayList<obj> list) {
    this.jobsQueue = new LinkedBlockingQueue<>();
    this.queueCounter = 0L;
    this.bt_device = bt_device;
    this.process_list = list;
    this.main_handler = handler;
}
public void run() {

    while (!Thread.interrupted() && can_continue) {
        if(sock == null){
            bluetooth_init();
        }
        if(!initialized && can_continue){
            init_commands();
        }
        if(can_continue){
            testing_commands();
        }
    }
}

private void bluetooth_init(){
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    try {
        sock = bt_device.createRfcommSocketToServiceRecord(uuid);
        sock.connect();
    } catch (IOException e) {
        e.printStackTrace();
        can_continue = false;
    }
}

private void init_commands(){

    ArrayList<obj> init_objs = temp_objs.init_array;

    for (obj init_cmd:init_objs
         ) {
        queueJob(init_cmd);
    }

    try {
        executeQueue();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

private void testing_commands(){
    String vin = "";
    obj vin_obj = temp_objs.find_by_value("VIN");
    for(int j = 0; j < 15; j++){

        queueJob(vin_obj);
        try {
            executeQueue();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        vin = vin_obj.command.getFormattedResult();
        Log.d("vin count: " + String.valueOf(j)+ " ", vin);
    }
}
private void queueJob(obj current) {
    queueCounter++;
    current.getCommand_job().setId(queueCounter);
    try {
        jobsQueue.put(current);
    } catch (InterruptedException e) {
        current.getCommand_job().setState(ObdCommandJob.ObdCommandJobState.QUEUE_ERROR);
        Log.e("OBD LOG", "Failed to queue job.");
    }
}

private void executeQueue() throws InterruptedException {
    while (jobsQueue.size() > 0) {
        ObdCommandJob job = null;
        obj job_obj = null;

        try {
            job_obj = jobsQueue.take();
            job =  job_obj.getCommand_job();                    
            job.setState(ObdCommandJob.ObdCommandJobState.RUNNING);

                if (sock.isConnected()) {
                    job.getCommand().run(sock.getInputStream(), sock.getOutputStream());
                    //send data to be bundled.
                    if(job_obj != null){
                        bundle_data(job_obj);
                    }
                } else {
                    job.setState(ObdCommandJob.ObdCommandJobState.EXECUTION_ERROR);
                    Log.e("OBD LOG", "Can't run command on a closed socket.");
                }
        } catch (InterruptedException i) {
            Thread.currentThread().interrupt();
        } catch (UnsupportedCommandException u) {
            if (job != null) {
                job.setState(ObdCommandJob.ObdCommandJobState.NOT_SUPPORTED);
                Log.w("OBD LOG", "Command not supported. -> " + u.getMessage());
            }
            Log.w("OBD LOG", "Command not supported. -> " + u.getMessage());
            Log.w("OBD LOG", "Job is null");

        } catch (Exception e) {
            if (job != null) {
                job.setState(ObdCommandJob.ObdCommandJobState.EXECUTION_ERROR);
            }
            Log.e("OBD LOG", "Failed to run command. -> " + e.getMessage());
        }


    }
}

0 个答案:

没有答案