Menu

Java Example_(en)

Joël Matteotti

Java program exmeple

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

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 > 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 > 31) /* de 0 à 31 are special character from ASCII */
                str=str+(char)x;
        }
        manageEvents(str);
    }
}

});
System.out.println(


Related

English: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.