Android蓝牙OBDII通信

我对这个问题有什么不妥。 我正尝试从蓝牙ELM327发送和接收数据。 扭矩的作品,我用一个终端应用程序发送命令,也返回正确的结果。 所以我不明白我在这里做错了什么。 这是处理所有这些的线程。 我使用Pires java obd库,在这里找到

我回来了一个“?” 对于所有的初始化命令,但是当我开始循环vin命令时,它开始返回“OK”,并且有一点我能够正确地获得vin,但是我必须循环一堆才能得到它。

连接正在返回一个套接字。 命令以"AT L0"的格式发送(每个库),因此

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

我已经尝试把父发送和接收命令放在单独的阻塞线程中(使用Thread.join() ),并没有改变任何东西。 我已经延迟Thread.sleep(1500)没有任何变化。 我不知道还有什么可以尝试的,我对android和蓝牙开发相当陌生,所以你可以给予的任何帮助都非常感谢。

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

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


    }
}
链接地址: http://www.djcxy.com/p/95121.html

上一篇: Android Bluetooth OBDII communication

下一篇: II Perl code hangs after specific number of AT requests