Send and Receive Arduino data for Android via the Internet

1

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

I'm using AndroidStudio. Arduino Mega2560 Shield Ethernet W5100

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

Reached so far : Send on / off command.

Follow the code below: Connection 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 all the support!

    
asked by anonymous 12.10.2017 / 20:57

0 answers