android arduino OTG

Hello to every one. For a long time now I wanted to connect arduino UNO with a mobile phone via OTG cable. The reason is that if someone can use a mobile phone as a shield for arduino then he will have in his hands all kind of sebsors (3g,wifi,accelerometer,camera...) After a long period of searching I have found a library that gives you the ability to write an android app and control arduino directly by connecting android phone and arduino via OTG cable, so far so good.
(if someone wants I will post the link). What I want to do for start is create an html page that has two buttons on and off and write to a txt file ledSTATE.txt the numbers 1 or 0. Then I want my android app to read that file automatically when ever the data in the file changes and send the accordingly command to the arduino.
So far I have a working android code that sends commands to the arduino via OTG
and an android code that makes an http get request to the server and reads the data of the txt file, my problem is that it reads the data only once and if I change the data of the ledSTATE.txt I have to tilt the phone in order to take the new data from the ledSTATE.txt file. Here is the code for the httpget request. What am I doing wrong? I want the android app to listen automatically for changes to that file. Please help.

package com.hmkcode.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

EditText etResponse;
TextView tvIsConnected;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // get reference to the views
    etResponse = (EditText) findViewById(R.id.etResponse);
    tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);

    // check if you are connected or not
    if(isConnected()){
        tvIsConnected.setBackgroundColor(0xFF00CC00);
        tvIsConnected.setText("You are conncted");
    }
    else{
        tvIsConnected.setText("You are NOT conncted");
    }

    // show response on the EditText etResponse
    //etResponse.setText(GET("http://hmkcode.com/examples/index.php"));

    // call AsynTask to perform network operation on separate thread
   // new HttpAsyncTask().execute("http://hmkcode.com/examples/index.php");
    new HttpAsyncTask().execute("http://192.168.33.126/test2/LEDstate.txt");
}

public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}

private static String convertInputStreamToString(InputStream inputStream) throws 
 IOException{
    BufferedReader bufferedReader = new BufferedReader( new 
  InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}

public boolean isConnected(){
  ConnectivityManager connMgr = (ConnectivityManager)      
  getSystemService(this.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected())
            return true;
        else
            return false;  
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        return GET(urls[0]);
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
        etResponse.setText(result);
   }
}
1 Like