I want to establish a TCP/IP connection between my android phone and an arduino board connected to a wifi router using ethernet shield. Aim of the project is to drive a small remote car through an app with four buttons; forward,backward,left,right.I have written the code shown below but the problem is that there is NO data communication between the arduino( which is acting as a server) & the android phone.The code on the arduino side is fine as i have tried by sending data through my PC using LabView. Note bluetooth is not an option.same address was used in Labview & it worked fine, so the address or the port may not be the issue
So Can any one please guide me what is wrong with my code,though it builds fine?? I am using Android Studion 1.1.0
import android.app.AlertDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.DialogFragment;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class MainActivity extends ActionBarActivity {
String ip_add="192.168.1.104";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void move_forward(View view){
try{
InetAddress serverAddr=InetAddress.getByName(ip_add);
Socket s=new Socket(serverAddr,80);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
char c='b';
out.write(c);
out.flush();
//out.close();
s.close();
}catch (Exception ex){}
}
public void move_left(View view){
try{
Socket s=new Socket(ip_add,80);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
char c='c';
out.write(c);
out.flush();
// out.close();
s.close();
}catch (Exception ex){}
}
public void move_right(View view){
try{
Socket s=new Socket(ip_add,80);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
char c='d';
out.write(c);
out.flush();
//out.close();
s.close();
}catch (Exception ex){
}
}
public void move_back(View view){
try{
Socket s=new Socket(ip_add,80);
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
char c='e';
out.write(c);
out.flush();
//out.close();
s.close();
}catch (Exception ex){}
}
} `
According to your comment the server receiving the data is an HTTP server but you are sending raw TCP data. Sending just one character to an HTTP server as you do will not trigger anything.
You can do that however if you do so you have to send a complete HTTP request, not only one character. A simple HTTP response could be sent as follows:
out.write("GET /index.htm HTTP/1.0\r\n\r\n");