Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, September 7, 2015

Profiling UltraESB with YourKit Java Profiler

YourKit Java Profiler is a rich Java profiling tool that can be used to easily identify CPU usage, memory usage, thread utilisation, garbage collections and possible dead locks of your  Java applications. In this post I'll briefly go through how to profile an UltraESB instance hosted on a EC2 instance using YourKit Java Profiler.

Before move into further details, you have to download YourKit Java profiler from their site. If you don't have a distribution of UltraESB, you can download a binary distribution of UltraESB from here.

EC2 Setup


Because this is a remote profiling between a EC2 hosted UltraESB instance and your local YourKit application, at the setup of ESC2 instance, ports 10001 - 10010 should be opened for external access.

Figure 1
Both UltraESB distribution and YourKit has to be on EC2 instance. To configure UltraESB with YourKit, JVM_OPTS line of ultraesb.sh in <UltraESB Home>/bin directory should be changed as figure 2.

Figure 2


Path of the libyjpagnet.so file should be changed according to the platform. Once those configurations are done, now you can start UltraESB from

<UltraESB Home>/bin/ultaesb.sh

If configuration is correct, you can see a line like following at the top of ultraesb log.

[YourKit Java Profiler 2015 build 15070] Log file: /home/ubuntu/.yjp/log/java-
1445.log


You can put a load on UltraESB using jb-run tool that is shipped with UltraESB distribution.

cd <UltraESB Home>/bin
./uterm.sh
jbrun -c 100 -d 1 -k -m POST -n 1000 -p /home/ubuntu/payload.txt -s 100 -t 150000 http://localhost:8280/service/echo-back 


YourKit (local machine) setup


Start YourKit by running <YourKit Home>/bin/yjp.sh
Click "Connect to remote application". Fill EC2 machine username and domain. (Figure 3)

Figure 3
Add security credentials that you used to log into EC2 instance over ssh (Figure 4)

Figure 4


Then you can see the dashboard of YourKit that describes memory usage statistics, thread utilisation and etc,




To do a CPU profiling, click start CPU profiling button





Wednesday, October 8, 2014

Java Wrapper for Tesseract OCR Library

Tesseract is a very popular OCR library written in C++. It can be simply used to identify characters in a given image that contains text. In addition to that it can be used to get positions of each word/ character. Tesseract provides a command line tool and a C++ api to give services to users. However there is not a implementation for Java users that can directly use Tesseract for their applications.

As a part of my GSoC project in Apache PDFBox  I implemented a Java wrapper for Tesseract C++ api that can be used by Java users to directly use Tesseract in their applications. Code repository can be found from here.

To use Java API simply import Tesseract-JNI-Wrapper-1.0.0.jar to your project. If you are using maven, add this to your pom

<dependency>
  <groupId>org.apache.pdfbox.ocr</groupId>
  <artifactId>Tesseract-JNI-Wrapper</artifactId>
  <name>Tesseract Jni Wrapper</name>
  <version>1.0.0</version>
</dependency>


Here is a sample code that can use Java API invoke Tesseract.

public String getOCRText(BufferedImage image){ //You need to send BufferedImage (RGB) of scanned image
  TessBaseAPI api = new TessBaseAPI();
  boolean init = api.init("src/main/resources/data", "eng"); // position of Training data files
  api.setBufferedImage(image);
  String text = api.getUTF8Text();
  System.out.println(text);
  api.end();
  return text;
}


Getting positions of each OCRed word

public void printOCRTextPositions(BufferedImage image){
  TessBaseAPI api = new TessBaseAPI();
  boolean init = api.init("src/main/resources/data", "eng");
  api.setBufferedImage(image);
  api.getResultIterator();
  if (api.isResultIteratorAvailable()) {
    do {
      System.out.println(api.getWord().trim());
      String result = api.getBoundingBox();
      System.out.println(result);
    } while (api.resultIteratorNext());
  }
  api.end();
}


P.S.
This wrapper currently is working in MacOS and Linux environments. It wasn't tested in Windows environments. If anyone is willing to develop or improve functionalities of this wrapper please let me know.

Tuesday, October 7, 2014

Continuous Integration for GitHub - Travis CI

Travis CI is a very impressive and cool CI tool that can directly fetch and automatically build your GitHub projects. Following few steps you can easily integrate your GitHub projects with Travis CI

1. Got to https://travis-ci.org/ and log in using your GitHub account

2. click + button and add your project to Travis CI




3. Add .travis.yml file to the root folder of the project and push it to GitHub
This is the file that contains configuration details to Travis CI about your project details like language and build instructions
If your project is a java maven project, you can simply add

language: java

install: mvn install -Dmaven.compiler.target=1.6 -Dmaven.compiler.source=1.6 -DskipTests=true

script: mvn test -Dmaven.compiler.target=1.6 -Dmaven.compiler.source=1.6

For more configuration details refer to the documentation of Travis CI

4. Do some change to your project and push it to GitHub. Commit will be reflected in Travis Console same time and it will start to build project automatically and send build details to your mail.


Thursday, December 26, 2013

Adding Google Maps to your Java Standalone applications

Google maps is a very impressive tool to present details of particular location in any part of world. It is very common to see google maps integration in web applications using java scripts. However it can be included in to Java SE applications also. It can be simply done using google static maps api and java swing components. Here is my code.

public class Main {
     JFrame frame = new JFrame();
     JPanel panel;
     BufferedImage image;
     public void show(String gps) {
            panel = new JPanel();
            try {
                   image = ImageIO.read(new URL("http://maps.google.com/staticmapcenter="+gps+"&zoom=14&size=600x300&maptype=roadmap&markers="+gps+"&sensor=false&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg"));

                   JLabel label = new JLabel(new ImageIcon(image));
                   panel.add(label);
                   frame.add(panel);
                   frame.pack();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setLocationRelativeTo(null);
                   frame.setVisible(true);
            } catch (MalformedURLException e) {
                   e.printStackTrace();
            } catch (Exception e) {
                  e.printStackTrace();
            }
     }
     public Main(){
             show("6.423601,79.996755");
     }

     public static void main(String[] args){
           new Main();
     }

}





More details can be added to map by referring to google static maps api.

Friday, February 3, 2012

Create a bluetooth joystick for your laptop using your java phone

So, hi all........:)
This is my first technical post. In this session, I hope to share some knowledge about java mobile bluetooth programming using J2ME. This is a project I did after 1 st semester exam in university (So this is bit old). However there are so many mobile technologies available today other than J2ME but there is a considerable amount of crowd using J2ME yet.

In this project main intention is to connect my mobile Nokia 3120 classic with my laptop using bluetooth and control my laptop using the phone. So I had to write two applications for both lap and phone using java.
I hope this works with all mobile phones support java. If not please let me know

First I would like to give the code for bluetooth server for the computer. To use this code there are few requirements
1 - A bluetooth dongle connected to to your pc and properly installed driver softwares
2- Set it to discoverable  mode.

3 - Your mobile phone should be paired with with this dongle



Server application consists with three classes. You can use Netbeans or Eclipse or other IDE to develop the application. In netbeans create a new java application project (With a Main class) and crate 3 classes named Frame , Key and Receive. Then following body to those classes.



***** You will need to add bluecove2.1.0.jar to libraries
http://code.google.com/p/bluecove/downloads/detail?name=bluecove-2.1.0.jar&can=2&q=

----------------------------------------------------------------------------------------

Main.java


public class Main {
    public static void main(String args[]){
        new Frame().setVisible(true);
     
    }
}

----------------------------------------------------------------------------------------
Frame.java


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DataElement;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;


public class Frame extends javax.swing.JFrame {

    final Object inquiryCompletedEvent = new Object();
    private LocalDevice local;
    private StreamConnectionNotifier server = null;
    private StreamConnection conn = null;
    private InputStream is;
    private OutputStream os;
    private Key key;

    public Frame() {
        initComponents();
        key = new Key(this);
        Thread t = new Thread(key);
        t.start();
        this.setResizable(false);

    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        lblMessage = new javax.swing.JLabel();
        btStart = new javax.swing.JButton();
        lblTitle = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblMessage.setText("W.D.Upeksha, University of Moratuwa");
        getContentPane().add(lblMessage, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 230, 20));

        btStart.setText("Start");
        btStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btStartActionPerformed(evt);
            }
        });
        getContentPane().add(btStart, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 60, -1, -1));

        lblTitle.setText("Bluetooth Server");
        getContentPane().add(lblTitle, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 180, -1));

        pack();
    }// </editor-fold>

    private void btStartActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            try {

                //doDeviceDiscovery();
                lblMessage.setText("Waiting");
                createService();
                conn = server.acceptAndOpen();
                lblMessage.setText("Connected");
            } catch (IOException ex) {
                lblMessage.setText("Error");
                Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
            }


            String msg = "hello there, client";
            is = conn.openInputStream();
            os = conn.openOutputStream();
            // send data to the server
            os.write(msg.getBytes());
            os.flush();
            // read data from the server
            connection();
            conn.close();
            btStart.setVisible(false);
            lblMessage.setText("Connected");
        } catch (IOException ex) {
            Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }                                        

    void connection() {
        Receive rec = new Receive(is, this);
        Thread thr = new Thread(rec);
        thr.start();

    }

    void reset(String str) {
        lblMessage.setText(str);
        key.setStr(str.trim());    
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Frame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btStart;
    private javax.swing.JLabel lblMessage;
    private javax.swing.JLabel lblTitle;
    // End of variables declaration

    private void createService() {
        try {
            ServiceRecord record = null;
            try {
                try {
                    local = LocalDevice.getLocalDevice();
                    local.setDiscoverable(DiscoveryAgent.GIAC);
                } catch (BluetoothStateException ex) {
                    Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
                }
                String connectionURL = "btspp://localhost:393a84ee7cd111d89527000bdb544cb1;" + "authenticate=false;encrypt=false;name=RFCOMM Server";
                server = (StreamConnectionNotifier) Connector.open(connectionURL);
            } catch (IOException ex) {
                Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
            }
            record = local.getRecord(server);
            DataElement elm = null;
            elm = new DataElement(DataElement.DATSEQ);
            elm.addElement(new DataElement(DataElement.UUID, new UUID(0x1002)));
            record.setAttributeValue(0x0005, elm);

            elm = new DataElement(DataElement.STRING, "BT Benchmark");
            record.setAttributeValue(0x101, elm);

            elm = new DataElement(DataElement.STRING, "Upeksha");
            record.setAttributeValue(0x102, elm);

        } catch (Exception ex) {
            Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

---------------------------------------------------------------------------------------------------------
Key.java

import java.awt.AWTException;
import java.awt.Robot;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Key implements Runnable{
    private String str = "RELEASE";
    private Robot robo;
    private Frame frm;
    public Key(Frame frm){
        try {
            this.frm = frm;
            robo = new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(Key.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setStr(String str) {
        this.str = str;
    }

    
    @Override
    public void run() {
       while(true){
            try {
                if (str.equals("UP")) {
                    robo.keyPress(65);
                } else if (str.equals("DOWN")) {
                    robo.keyPress(66);
                } else if (str.equals("RIGHT")) {
                    robo.keyPress(67);
                } else if (str.equals("LEFT")) {
                    robo.keyPress(68);
                } else if (str.equals("UP_RELEASE")) {
                    robo.keyRelease(65);
                
                }else if (str.equals("DOWN_RELEASE")) {
                    robo.keyRelease(66);
                
                }else if (str.equals("RIGHT_RELEASE")) {
                    robo.keyRelease(67);
                
                }else if (str.equals("LEFT_RELEASE")) {
                    robo.keyRelease(68);
                
                } 
                Thread.sleep(100);
                
            } catch (InterruptedException ex) {
                Logger.getLogger(Key.class.getName()).log(Level.SEVERE, null, ex);
            }           
       }
    }    
}

--------------------------------------------------------------------------------------
Receive.java

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Receive implements Runnable {

    InputStream is;
    Frame frm;

    public Receive(InputStream is, Frame frm) {
        this.is = is;
        this.frm = frm;
    }

    public void run() {
        String msg;
        try {
            while (true) {
                byte[] buffer = new byte[100];
                is.read(buffer);
                msg = new String(buffer);
                frm.reset(msg.trim());
                Thread.sleep(100);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(Receive.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Receive.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

Then we need an application to install into mobile phone to connect to server
Here is the code for it. For that it is recommended to use a netbeans mobile application or you can use eclipse (I'm not familiar with eclipse :) ).


Then create a new midlet named Midlet and two java classes named KeyCanvas and Communication

-------------------------------------------------------------------------------------------------------

Midlet.java

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DataElement;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class Midlet extends MIDlet implements DiscoveryListener{

    private Display display;
    final Object inquiryCompletedEvent = new Object();
    private Vector devicesDiscovered;
    private ServiceRecord[] servicesFound = null;
    private ServiceRecord service;
    private DiscoveryAgent agent = null;
    private LocalDevice local;
    private StreamConnection conn = null;
    private Form form;
    private List lst;
    private Command cmd;
    int face = 0;

    public void startApp() {
        try {
            StringItem stringItem = new StringItem("Remort", "Testing Connectio\n");
            doDeviceDiscovery();
            form = new Form(null, new Item[]{stringItem});
            lst = new List("Devices", List.IMPLICIT);
            display = Display.getDisplay(this);
            display.setCurrent(form);
            cmd = new Command("select", Command.EXIT, 0);
            lst.addCommand(cmd);
            lst.setCommandListener(new CommandListener() {
                public void commandAction(Command arg0, Displayable arg1) {
                    if (face == 0) {
                        int num = lst.getSelectedIndex();
                        doServiceSearch((RemoteDevice) (devicesDiscovered.elementAt(num)));
                    }
                    if (face == 1) {
                        try {
                            int num = lst.getSelectedIndex();
                            service = servicesFound[num];
                            DataElement el = (DataElement) service.getAttributeValue(0x100);
                            form.deleteAll();
                            form.append(el.getValue() + "");
                            display.setCurrent(form);

                            String connectionURL = service.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
                            conn = (StreamConnection) Connector.open(connectionURL);
                            form.append(" Connected");
                            OutputStream os = conn.openOutputStream();
                            String msg = "Connected to Client";
                            os.write(msg.getBytes());
                            os.flush();
                            form.append(" Data sent");
                            byte[] buffer = new byte[100];
                            connection();
                            Displayable can = new KeyCanvas(os);
                            display.setCurrent(can);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }

                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    void connection() throws IOException {
        InputStream is = conn.openInputStream();
        Communication com = new Communication(this, is);
        new Thread(com).start();

    }

    void scan() {
        synchronized (inquiryCompletedEvent) {
            try {
                boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, this);
                if (started) {
                    form.append("wait for device inquiry to complete...");
                    inquiryCompletedEvent.wait();
                    form.append(devicesDiscovered.size() + " device(s) found");
                }
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            } catch (BluetoothStateException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
        try {
            form.append("Device " + btDevice.getBluetoothAddress() + " found");
            devicesDiscovered.addElement(btDevice);
            try {
                form.append(" name " + btDevice.getFriendlyName(false));
            } catch (IOException cantGetDeviceName) {
            }
            lst.append(btDevice.getFriendlyName(false), null);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void servicesDiscovered(int transID, ServiceRecord[] serviceRecord) {
        DataElement nameElement = null;
        lst.deleteAll();
        servicesFound = serviceRecord;
        for (int i = 0; i < serviceRecord.length; i++) {
            nameElement = (DataElement) serviceRecord[i].getAttributeValue(0x100);
            if (nameElement != null && nameElement.getDataType() == DataElement.STRING) {
                lst.append((String) nameElement.getValue(), null);
            }
        }

    }

    public void serviceSearchCompleted(int arg0, int arg1) {
        face = 1;
    }

    public void inquiryCompleted(int discType) {
        form.append("Device Inquiry completed!");
        display.setCurrent(lst);
        synchronized (inquiryCompletedEvent) {
            inquiryCompletedEvent.notifyAll();

        }
    }

    private void doDeviceDiscovery() {
        try {
            local = LocalDevice.getLocalDevice();
        } catch (BluetoothStateException bse) {
            // Error handling code here
        }
        agent = local.getDiscoveryAgent();
        devicesDiscovered = new Vector();
        try {
            if (!agent.startInquiry(DiscoveryAgent.GIAC, this)) {
             
            }
        } catch (BluetoothStateException bse) {
        }
    }

    private void doServiceSearch(RemoteDevice device) {

        int[] attributes = {0x100, 0x101, 0x102};
        UUID[] uuids = new UUID[1];
        uuids[0] = new UUID(0x1002);
        try {
            agent.searchServices(attributes, uuids, device, this);
        } catch (BluetoothStateException e) {
            // Error handling code here
        }
    }

    public Form getForm() {
        return form;
    }
}

------------------------------------------------------------------------------------------

Communication.java


import java.io.IOException;
import java.io.InputStream;

public class Communication implements Runnable {
    Midlet mid;
    InputStream is;
    Communication(Midlet aThis, InputStream is) {
        this.mid=aThis;
        this.is=is;
    }
    public void run() {
        while(true){
            try {
                Thread.sleep(100);
                byte buffer[] = new byte[100];
                is.read(buffer);
                String msg = new String(buffer);
                mid.getForm().deleteAll();
                mid.getForm().append(msg.trim());
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
         
        }
    }
 
}

----------------------------------------------------------------------------------------
KeyCanvas.java


import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class KeyCanvas extends Canvas {

    private Font mFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    private String mMessage = "[Press keys]";
    private OutputStream os;

    public KeyCanvas(OutputStream os) {
        this.os = os;
    }

    public void paint(Graphics g) {
        int w = getWidth();
        int h = getHeight();

        g.setGrayScale(255);
        g.fillRect(0, 0, w - 1, h - 1);
        g.setGrayScale(0);
        g.drawRect(0, 0, w - 1, h - 1);

        g.setFont(mFont);

        int x = w / 2;
        int y = h / 2;

        g.drawString(mMessage, x, y, Graphics.BASELINE | Graphics.HCENTER);
    }

    protected void keyPressed(int keyCode) {
        try {
            int gameAction = getGameAction(keyCode);
            switch (gameAction) {
                case UP:
                    mMessage = "UP";
                    os.write(mMessage.getBytes());
                    break;
                case DOWN:
                    mMessage = "DOWN";
                    os.write(mMessage.getBytes());
                    break;
                case LEFT:
                    mMessage = "LEFT";
                    os.write(mMessage.getBytes());
                    break;
                case RIGHT:
                    mMessage = "RIGHT";
                    os.write(mMessage.getBytes());
                    break;
                case FIRE:
                    mMessage = "FIRE";
                    os.write(mMessage.getBytes());
                    break;
                case GAME_A:
                    mMessage = "GAME_A";
                    os.write(mMessage.getBytes());
                    break;
                case GAME_B:
                    mMessage = "GAME_B";
                    os.write(mMessage.getBytes());
                    break;
                case GAME_C:
                    mMessage = "GAME_C";
                    os.write(mMessage.getBytes());
                    break;
                case GAME_D:
                    mMessage = "GAME_D";
                    os.write(mMessage.getBytes());
                    break;
                default:
                    mMessage = "";
                    os.write(mMessage.getBytes());
                    break;
            }
            os.flush();
            repaint();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    protected void keyReleased(int keyCode) {
        try {
            int gameAction = getGameAction(keyCode);
            switch (gameAction) {
                case UP:
                    mMessage = "UP_RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                case DOWN:
                    mMessage = "DOWN_RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                case LEFT:
                    mMessage = "LEFT_RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                case RIGHT:
                    mMessage = "RIGHT_RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                case FIRE:
                    mMessage = "RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                case GAME_A:
                    mMessage = "RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                case GAME_B:
                    mMessage = "RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                case GAME_C:
                    mMessage = "RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                case GAME_D:
                    mMessage = "RELEASE";
                    os.write(mMessage.getBytes());
                    break;
                default:
                    mMessage = "RELEASE";
                    os.write(mMessage.getBytes());
                    break;
            }
            os.flush();
            repaint();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

-----------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------
After compiling the project you will get a jar file and jad file in the dist folder of your project. You can simply copy those files in to your mobile phone and run it.


So... Now we have completed the programming part. Then lets's run the projects. First you have to run Server application. Make sure your bluetooth device is turned on. You will get a window like this





He he .... You can put your name instead of my name :D. Then click start. You will realize that start button is   pressed. It means that server waits for a client. Then you can run the application in your mobile phone. Fisrt turn on your mobile phone's bluetooth device.


As soon as you turn on the application, it will search for the mobile devices. If your computer's bluetooth device is discoverable, it will be shown.


Then you have to select it from the list.


After selecting the device, application will search for the services given by the server. Choose RFCOMM Server. It creates serial data transfer between client and server.



Then you will see that Server application shows that it has connected with the client. It means everything is ok. :)



Press down button of the phone. Can you see that server gets the message? if yes you are done. :)



Open notepad and press top, down, left and right buttons. You will see that something is written on the notepad. Server generates keyboard events according to the input you gave to the phone. If you want to change these key events go to Key.java and change those events. Ahh haaaa :)
Then open your favorite racing game and configure the controllers according to the key events generated by server. Then enjoy your wireless Joystick

I know that there is lot of stuff in this post is bit hard to the people who are new to bluetooth programming. Same happened to me at the start of the project. However I can provide you the referring  materials I have used.
Use this thesis to learn about bluetooth programming in J2ME.
All the stuff I have used is clearly described here.

Finally.. It's your part. If you feel this post was useful to you in anyway please put a comment. Doesn't matter whether it is good or bad. I only need is a feedback to improve myself

Thank you