I'm out of ideas as to what is wrong with this. I'm trying to send and 
receive data from my Bluetooth ELM327. Torque works, and I've used a 
terminal app to send commands and that also returns correct results. So I 
don't get what I'm doing wrong here. This is the thread that handles all of 
it. I'm using Pires java OBDlibrary, found here 
<https://github.com/pires/obd-java-api/>

I'm getting back a "?" for all of the initialization commands, but when I 
start looping the VINcommand it starts returning "OK" and at one point I 
was able to actually get the VINcorrectly, but I had to loop a bunch of 
times to get it.

The connection is being made as a socket is returned. Commands are sent(per 
the library) in the format `"AT L0"` and as such

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


I have tried putting the parent send and receive commands in separate 
blocking threads using Thread.join() and that didn't change anything. I've 
tried a delay up to 1500ms using Thread.sleep.

I'm not sure what else to try, so any help you can give is greatly 
appreciated. Thanks in advance!

Here's my code for the thread called from the 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
());
            }




        }
    }

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/42c7eac7-785f-4725-80d6-c75a447b45c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to