Hi. I'm new to java/android programming, but you sure will know it by just reading the post, as I think it has to be a very simple issue for an experienced programmer.
What I am trying to get:
An application with 2 ToggleButtons. On click they have to connect to a socket over the LAN and send a few characters (to an Arduino board which is listening).
What I've got after some days:
Android SDK, Eclipse and AVD installed and running.
An application with 2 ToggleButtons which responds to mouse clicks and shows a Toast.
There are a main activity and a class to handle the onClick event.
I tried in many ways but still can't get it to create a socket and send 2 characters.
I can connect to the Arduino from my PC with a telnet client, and even from my smartphone using an example telnet app i found over the internet, and Arduino accepts the commands, so I think (I'm quite sure) the problem is not in the Arduino program.
In order to debug my app I cut off all non essential code lines, just trying to open a socket and send a fixed string "A1". Here is what remains, but when it runs in AVD, on click shows a Toast saying "Generic ERROR", so it's not "UnknownHost" neither "IO" Exception.
Cutting off other code lines I found that the error raises on the line which creates the socket
socket = new Socket("192.168.1.123",80);
The IP address and the port numbrer are correct, I can connect there using telnet.
What's wrong with it?
import android.os.Bundle; import android.app.Activity; import android.widget.ToggleButton; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ToggleButton toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1); toggleButton1.setOnClickListener(new MioClickListener()); ToggleButton toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2); toggleButton2.setOnClickListener(new MioClickListener()); } //onCreate } //MainActivity
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import android.view.View; import android.view.View.OnClickListener; //import android.widget.ToggleButton; import android.widget.Toast; public class MioClickListener implements OnClickListener { @Override public void onClick(View view) { Socket socket; PrintWriter out; try { socket = new Socket("192.168.1.123",80); out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); out.println("A1"); out.close(); socket.close(); } catch (UnknownHostException e) { Toast.makeText(view.getContext(), "UnknownHostException", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } catch (IOException e) { Toast.makeText(view.getContext(), "IOException", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } catch (Exception e) { Toast.makeText(view.getContext(), "Generic ERROR", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } }