0

I am trying to make a simple Android app to send strings over WiFi to a microcontroller that will later on be used as commands to turn an led on and off.

Below is the code that I have and I am having issues with understanding why it doesn't work. Basically an exception is thrown and the "connection failed" toast is envoked.

     String hostname = "192.168.50.1";
     int port = 5001;
     PrintWriter out = null;

     //access_point.connect(new InetSocketAddress(hostname, port));

    public void command() throws Exception {

        try{
             Socket access_point = new Socket(hostname,port);
            out = new PrintWriter(access_point.getOutputStream(), true);
            out.println("Turn on"); 
            Toast.makeText(getBaseContext(), "Connection successful", Toast.LENGTH_LONG).show();
        //  BufferedReader in = new BufferedReader(new InputStreamReader(access_point.getInputStream()));
        }
        catch(Exception e){

            Toast.makeText(getBaseContext(), "Connection failed", Toast.LENGTH_LONG).show();

        }



    }


     ToggleButton tbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fullscreen);

        // code for toggle button 
        tbutton = (ToggleButton) findViewById(R.id.toggleButton1);
        tbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(((ToggleButton)v).isChecked())
                {
                    try{
                        command();
                        Toast.makeText(getBaseContext(), "Communication ON string sent", Toast.LENGTH_SHORT).show();
                    }

                    catch(Exception e)
                    {
                        Toast.makeText(getBaseContext(), "Communication failed", Toast.LENGTH_LONG).show();
                    }


                }
                else
                {
                    Toast.makeText(getBaseContext(), "Communication OFF string sent", Toast.LENGTH_LONG).show();
                }       
              } 
            });
        }
}
bjb568
  • 11,089
  • 11
  • 50
  • 71

1 Answers1

0

Rather than just throwing a toast, you should use

    Log.d(getClass().getSimpleName(), "Connection failed", exception);

The likely problem you have is that you are running a network connection on the main thread, which can freeze the application on a timeout, hence why the Android OS does not allow it.

Check out this question for more information on how to run the network connection on a different thread, and fix your problem: Using AsyncTask for android network connection

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • Thanks a lot ! I was unaware of this since this is my first app :P – user3698584 Jun 02 '14 at 09:27
  • I'm glad I could help :) Right into the hard stuff, huh - it's important to note if you haven't heard before, that an Activity is destroyed and recreated on screen orientation change. This can mess up things for an AsyncTask if it's running in an Activity that it needs to update the UI on, but has since then been recreated! For this, you can use a Fragment inside the Activity, because Fragments are kept alive if you use setRetainInstance(true) on it. An example is here: https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/ – EpicPandaForce Jun 02 '14 at 09:37
  • Of course, the truly simplest solution is blocking screen orientation change, if you really don't need that. It causes a myriad of problems, and pretty much one of the hardest things to learn how to do in Android programming. – EpicPandaForce Jun 02 '14 at 09:38
  • I'm glad you told me that. I will eventually block the screen orientation change. The UI looks better on landscape anyway :P – user3698584 Jun 02 '14 at 10:57