J2ME - Help me organise my code

Posted on 2005-04-18
Last Modified: 2013-11-24
Hi there,

Am very new to the world of J2ME programming and Java in general.
I have a Bluetooth device with which i can connect to using hyperterminal - when i send the device a character it returns a packet of data so i am trying to write a J2ME program to do this.
From various internet sources have managed to piece together some vague code but I dont know enough to organise it into the standard J2ME format to give me something that I can at least test.  I dont really know where to start.  Here is what I have - I know it is an absolute mess but any help will be gratefully recieved:

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

// jsr082 API
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;

// midp/cldc API
import javax.microedition.lcdui.Image;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

public final class BTMIDlet {

// set the port number
// set the baud rate
// set the connectable mode
// set the discovery mode to Limited Inquiry Access Code

// retrieve the device that is at the other end of
// the Bluetooth Serial Port Profile connection,
// L2CAP connection, or OBEX over RFCOMM connection
RemoteDevice remote =
    RemoteDevice.getRemoteDevice( c);

// retrieve the Bluetooth address of the remote device
String remoteAddress = remote.getBluetoothAddress();
// retrieve the name of the remote Bluetooth device
String remoteName = local.getFriendlyName(true);

String ServiceURL =

try {
    // create a server connection
    StreamConnectionNotifier notifier =
    // accept client connections
    StreamConnection connection = notifier.acceptAndOpen();

    // prepare to send/receive data
    byte buffer[] = new byte[100];
    String msg = "hello there, client";
    InputStream is = connection.openInputStream();
    OutputStream os = connection.openOutputStream();

    // send data to the client
    // read data from client;
catch(IOException e) {

    public BTMIDlet() { }

    public void startApp() { }
    protected void destroyApp(boolean unconditional) { }
    protected void pauseApp() {}

Many thanks in advance and good luck!

Question by:mingbaden
    LVL 9

    Expert Comment

    First of all, your BTMIDlet will need to extend javax.microedition.midlet.MIDlet so it will execute as a J2ME application.
    This is an abstract class so you would need to override startApp(), pauseApp() and destroyApp(boolean) which you have already done.

    The only other thing you'll need to do is to put your code inside the startApp() method or the constructor.
    Having the code in the constructor will ensure that it is only executed when an instance of BTMIDlet is created.
    By putting the code in the startApp() method means that the code will execute when the MIDlet is started and each time it's resumed (An incomming phone call would pause the app and hanging up will resume the app).

    Apart from all of this, the code looks fine to me. :-)
    LVL 9

    Accepted Solution

    It would also be a better idea to close your streams and connections in a finally block. This will ensure everything has been closed despite any exceptions that may have previously been thrown.


    Connection conn = null;
    InputStream in = null;
    OutputStream out = null;

      // Open streams, read and write data
    catch (IOException e)
            if (out != null)

            if (in != null)

            if (conn != null)
        catch (IOException e) { e.printStackTrace(); }

    Author Comment

    Sorry no reply for a while.

    That was enough to get me going

    Expert Comment

    I'm bring this back from the dead in hopes that mingbaden or OBCT could help me out with a problem i posted a few days ago?

    Thanks for your time!


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
    This video teaches viewers about errors in exception handling.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now