-1

I am trying to get sensor data from arduino to an android phone using the asynctask onprogress update method. The sensors's data is displayed in a TextView and there are 2 buttons for turning on and off a led.

The problem is that the android phone is getting real-time data from the arduino but i cannot turn on and off the light at the same time. I tried to stop the android phone from getting the sensor's data and tried to turn on and off the light. This is working. Both getting the sensor's data and turning on/off the led are not working simultaneously. Can somebody help me with this problem. It's very important for me as am using it for a project.

The android code:

package com.example.arduinoandroidasyntasktesting6;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView data;
    Button On,Off;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Sensors().execute("");

        data=(TextView)findViewById(R.id.textView2);
        On=(Button)findViewById(R.id.buttonOn2);
        Off=(Button)findViewById(R.id.buttonOff2);

         On.setOnClickListener(new OnClickListener()  
            {  
                @Override  
                public void onClick(View v)  
                {  
                    new Sensors().execute("?led2=1");
                }  
            }); 

         Off.setOnClickListener(new OnClickListener()  
            {  
                @Override  
                public void onClick(View v)  
                {  
                    new Sensors().execute("?led2=0");
                }  
            }); 
    }

    class Sensors extends AsyncTask<String, byte[], String>{

        InputStream nis;
        OutputStream nos;
        BufferedReader in;
        DefaultHttpClient httpclient =new DefaultHttpClient();
        URL url;
        URLConnection urlconn=null;
        InputStreamReader isn;
        @Override


        protected String doInBackground(String... params) {

            try{
                while(true){
                url =new URL("http://192.168.1.177/"+params[0]);
                urlconn=(HttpURLConnection)url.openConnection();

                nis = urlconn.getInputStream();
                in= new BufferedReader(new InputStreamReader(nis));

                String msg = in.readLine();
                byte[] thebyte =msg.getBytes();
                publishProgress(thebyte);

                }
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }


            return null;
        }

         protected void onProgressUpdate(byte[]... values) {
                String command=new String(values[0]);//get the String from the recieved bytes
                data.setText(command);
            }
    }
}
George Mulligan
  • 11,813
  • 6
  • 37
  • 50

1 Answers1

2

AsyncTasks will be executed one after another. You are starting several async tasks but if the first one does not finish the other ones will never run.

To let them really run in parallel use an executor or use threads.

You could use one async task for the sensing and use threads for on/off buttons.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • Wrong. Many asynctasks can run concurrently. You should see again Asynctask. http://developer.android.com/reference/android/os/AsyncTask.html – hqt Jan 27 '16 at 15:37
  • sir, can you provide me with an example or a link of both task running in parallel. – akshay270494 Jan 27 '16 at 15:39
  • 2
    @hqt. Have a look at `Order of execution` in your link. – greenapps Jan 27 '16 at 15:41
  • @greenapps thanks for your comment. I have a misunderstanding here. You are right. Android maintains order when run asynctask – hqt Jan 27 '16 at 15:47
  • @akshay270494 may you use `Executor` for testing `Asynctask` in your case and report here? thanks :) – hqt Jan 27 '16 at 15:51
  • @greenapps sir can you provide me with an example where both task can run in parallel. i am not getting a good example in google .. pls help – akshay270494 Jan 27 '16 at 15:51
  • ExecuteOnExecutor: http://developer.android.com/intl/ja/reference/android/os/AsyncTask.html#executeOnExecutor – greenapps Jan 27 '16 at 16:45