Send and Receive Arduino data for Android via Internet

Asked

Viewed 290 times

1

I am working on a project that I have communicate an Arduino with Android via Internet(Network), I have seen some tutorials on the subject but I could not finish yet, I would like to help to complete this project.

I’m using the Androidstudio. Arduino Mega2560 Shield Ethernet W5100

Goal: send and receive multiple data, and show on android.

Achieved so far: send on/off command.

Follows below code: Connected Class:

package arduino.cursoandroid.com.br.arduino;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Conexao {
  public String downloadUrl(String myurl) throws IOException {
    InputStream is=null;
    int len=5000;
    try {
      URL url=new URL(myurl);
      HttpURLConnection conn=(HttpURLConnection) url.openConnection();
      conn.setReadTimeout(10000/* milliseconds */
      );
      conn.setConnectTimeout(15000/* milliseconds */
      );
      conn.setRequestMethod("GET");
      conn.setDoInput(true);
      conn.connect();
      is=conn.getInputStream();
      String contentAsString=readIt(is, len);
      return contentAsString;
    }
    finally {
      if (is !=null) {
        is.close();
      }
    }
  }
  public String readIt(InputStream stream, int len) throws IOException,
  UnsupportedEncodingException {
    Reader reader=null;
    reader=new InputStreamReader(stream, "UTF-8");
    char[] buffer=new char[len];
    reader.read(buffer);
    return new String(buffer);
  }
}

Mainactivity:

package arduino.cursoandroid.com.br.arduino;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Conexao {


public String downloadUrl(String myurl) throws IOException {
    InputStream is = null;

    int len = 5000;

    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);

        conn.connect();
        is = conn.getInputStream();


        String contentAsString = readIt(is, len);
        return contentAsString;


    } finally {
        if (is != null) {
            is.close();
        }
    }
}


public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[len];
    reader.read(buffer);
    return new String(buffer);
}


}

Thanks in advance for the support of All!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.