<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Java Example_(en)</title><link>https://sourceforge.net/p/tuxdroidserver/english/Java%2520Example_%2528en%2529/</link><description>Recent changes to Java Example_(en)</description><atom:link href="https://sourceforge.net/p/tuxdroidserver/english/Java%20Example_(en)/feed" rel="self"/><language>en</language><lastBuildDate>Mon, 02 Jul 2012 22:13:04 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/tuxdroidserver/english/Java%20Example_(en)/feed" rel="self" type="application/rss+xml"/><item><title>WikiPage Java Example_(en) modified by Joe</title><link>https://sourceforge.net/p/tuxdroidserver/english/Java%2520Example_%2528en%2529/</link><description>**Java program exmeple **

 &lt;font color="red"&gt; Warning !! The programing of this new project is often modified, it's
 possible that's this sample don't work with the last version of SVN.color red&lt;/font&gt;

 It's only a simple X program skeleton to comunicate with
 TuxDroidServer. With this code, you may be able to create a complete
 IHM to drive your TuxDroid.
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.Socket;

    public class TuxDroid 
    {
    private static Socket socket = null;        /* socket's declaration */
    private static PrintWriter out = null;      /* output buffer's declaration */
    private static BufferedReader in = null;    /* input buffer's declaration */

    /***
     * use this function when you push head button
     */
    private static void onHeadButton()
    {
        System.out.println("bouton de la tête");
    }

    /***
     * use this function when you push Left wing
     */
    private static void onLeftButton()
    {
        System.out.println("bouton gauche");
    }

    /***
     * use  this function when you push right wing
     */
    private static void onRightButton()
    {
        System.out.println("bouton droit");
    }

    /***
     * use  this function when you plug battery charger
     */
    private static void onChargerPlugged()
    {
        //
    }

    /***
     * use  this function when you unplug battery charger
     */
    private static void onChargerUnPlugged()
    {
        //
    }

    /***
     * use  this function when you plug dongle on you computer
     */
    private static void onDongleConnected()
    {
        //
    }

    /***
     *  use  this function when you unplug dongle on you computer
     */
    private static void onDongleDisConnected()
    {
        //
    }

    /***
     *  use  this function when you push something in the remote controle 
     * 
     * @param button is the name of the button
     */
    private static void onRemoteButton(String button)
    {
        switch(button)
        {
            case "K_RED": // red button of remote controle
            {
                out.println("Tux_State(16)");
                System.out.println("battery level : "+getServerResponse());
            }
            break;
        }
    }

    /***
     *  to obtain a respons from server after a request
     * 
     * @return server's answer
     */
    private static String getServerResponse()
    {
        String str="";

        try
        {
            int x;
            while((x = in.read()) != '\r')
            {
                if(x &gt; 1)
                    str=str+(char)x;
            }
            return str;
        }
        catch(IOException ioe)
        {
            return "";
        }
    }

    /***
     * function to handle events from Tuxdroid
     * 
     * @param message
     */
    private static void manageEvents(String message)
    {
        String[] sp = null;

        if(message.startsWith("#TUXBUTTON") || message.startsWith("#TUXREMOTE"))
        {
            //c'est un bouton
            sp = message.split(":");

            if(sp[0].equalsIgnoreCase("#TUXBUTTON"))
            {
                switch(sp[1])
                {
                    case "LEFT": onLeftButton();
                    break;

                    case "RIGHT": onRightButton();
                    break;

                    case "HEAD": onHeadButton();
                    break;
                }
            }

            if(sp[0].equalsIgnoreCase("#TUXREMOTE"))
            {
                //remote controle
                onRemoteButton(sp[1]);
            }
        }
        else
        {
            if(message.startsWith("#TUXCHARGER"))
            {
                sp = message.split(":");

                switch(sp[1])
                {
                    case "PLUGGED": onChargerPlugged();
                    break;

                    case "UNPLUGGED": onChargerUnPlugged();
                    break;
                }
            }

            if(message.startsWith("#TUXDONGLE"))
            {
                sp = message.split(":");

                switch(sp[1])
                {
                    case "CONNECTED": onDongleConnected();
                    break;

                    case "DISCONNECTED": onDongleDisConnected();
                    break;
                }
            }
        }
    }

    public static void main(String[] args) throws IOException 
    {
        /* try to connect to server/
        try 
        {
            socket = new Socket("192.168.1.43", 9090); // ip adress et listener of TuxDroidServer
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            out.println("Tux_Key(test)"); //more information about Tux_Key are in the wiki
        } 
        catch (IOException e) 
        {
            return;
        }

        String str="";
        int x;

        // to handle event
        while(true)
        {
            str="";
            while((x = in.read()) != '\r')
            {
                if(x &gt; 31) /* de 0 à 31 are special character from ASCII */
                    str=str+(char)x;
            }
            manageEvents(str);
        }
    }
});
                System.out.println(</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Joe</dc:creator><pubDate>Mon, 02 Jul 2012 22:13:04 -0000</pubDate><guid>https://sourceforge.net7cb54deed5ffca701e9b3e9c9288ef9ae5bf07e9</guid></item></channel></rss>