Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old February 3rd, 2012, 06:41 PM   #1 (permalink)
New Member
 
Join Date: Feb 2012
Posts: 2
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default Problem with socket connection!

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?

MrAlt is offline  
Reply With Quote
Sponsors
Old February 4th, 2012, 12:59 PM   #2 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

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.
jonbonazza is online now  
Reply With Quote
The Following User Says Thank You to jonbonazza For This Useful Post:
MrAlt (February 15th, 2012)
Old February 15th, 2012, 09:23 AM   #3 (permalink)
New Member
 
Join Date: Feb 2012
Posts: 2
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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!
MrAlt is offline  
Reply With Quote
Old February 15th, 2012, 02:59 PM   #4 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

You're missing a full-stop in the android:name attribute of your second activity.

Code:
<activity 
    android:label="@string/app_name"
    android:name=".ControlActivity"
  />
jiminaus is offline  
Reply With Quote
Old February 16th, 2012, 03:21 PM   #5 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

Quote:
Originally Posted by jiminaus View Post
You're missing a full-stop in the android:name attribute of your second activity.

Code:
<activity 
    android:label="@string/app_name"
    android:name=".ControlActivity"
  />
Bingo. This should fix your issue.
jonbonazza is online now  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:13 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo