So Im writing an application to communicate to a Wi-Fi enabled Arduino board with a static IP...
This is what Im trying to do...
1.) Enter Ip and Host
2.) Connect
3.) Simply send either a "0" or "1" to the arduino board
There are two activities at the moment, one to enter the IP and port of the Board (which is set from the board itself) and one to send data to the board. When i comment out the connection methods in the Communicator class the app swithches activities without any issues, but when i un-comment them and attempt to connect, the app immidiately closes after i attempt to connect, any ideas?
Code:
package car.test.namespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.net.*;
public class CarTestAppActivity extends Activity implements OnClickListener {
Communication bot;
EditText enteredIPaddress;
EditText enteredPort;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
enteredIPaddress = (EditText)findViewById(R.id.enteredIPaddress);
enteredPort = (EditText)findViewById(R.id.entereredPort);
Button connect = (Button)findViewById(R.id.Connect);
connect.setOnClickListener(this);
}
public void onClick(View v) {
String IP = enteredIPaddress.getText().toString().trim();
String Port = enteredPort.getText().toString().trim();
Intent myIntent = new Intent(CarTestAppActivity.this, ControlActivity.class);
myIntent.putExtra("IP", IP);
myIntent.putExtra("Port", Port);
startActivity(myIntent);
//CarTestAppActivity.this.
}
}
Code:
package car.test.namespace;
import java.io.*;
import java.net.*;
public class Communication {
Socket comSocket;
DataOutputStream netOut;
public Communication(String IP, String Port) throws IOException{
comSocket = new Socket(IP,Integer.parseInt(Port));
netOut = new DataOutputStream(comSocket.getOutputStream());
}
public void Send(int toSend)throws IOException{
netOut.writeByte(toSend);
}
}
Code:
package car.test.namespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.io.IOException;
import java.net.*;
public class ControlActivity extends Activity implements OnClickListener{
boolean send;
Communication bot;
String IP;
String Port;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.control);
Intent intent = getIntent();
IP = intent.getStringExtra("IP");
Port = intent.getStringExtra("Port");
Button signal = (Button)findViewById(R.id.control);
signal.setOnClickListener(this);
send = false;
try{
bot = new Communication(IP,Port);
}catch(IOException IOE){
System.out.println("Connection failed!");
System.exit(1);
}
}
public void onClick(View v) {
try{
send = !send;
if(send)
{
int x =1;
bot.Send(x);
}else
{
int x = 0;
bot.Send(x);
}
}catch(IOException IOE)
{
System.exit(1);
}
}
}
Also, im not even seeing the "Connection Failed" message, the app is just shutting down, any ideas?