• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with socket connection!

MrAlt

Lurker
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?
 
Can you post your manifest file? I'm willing to bet you either (a) forgot to add an activity, or (b) you forgot a permission somewhere.
 
Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="car.test.namespace" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="13"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".CarTestAppActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:name="ControlActivity"/>
</application>
</manifest>

Here You Go :) I Hope this helps somehow! Thank you so much for looking at this!
 
You're missing a full-stop in the android:name attribute of your second activity.

Code:
<activity 
    android:label="@string/app_name"
    android:name="[COLOR=red][B].[/B][/COLOR]ControlActivity"
  />
 
Back
Top Bottom