?
Solved

J2ME - Help me organise my code

Posted on 2005-04-18
4
Medium Priority
?
1,253 Views
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.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Image;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;


public final class BTMIDlet {

// set the port number
BCC.setPortNumber("COM12");
// set the baud rate
BCC.setBaudRate(9600);
// set the connectable mode
BCC.setConnectable(true);
// set the discovery mode to Limited Inquiry Access Code
BCC.setDiscoverable(DiscoveryAgent.LIAC);

// 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(
        javax.microedition.io.Connection 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 =
    "btspp://localhost:10203040607040A1B1C1DE100;name=SPP
        Server1";

try {
    // create a server connection
    StreamConnectionNotifier notifier =
       (StreamConnectionNotifier) Connector.open(serviceURL);
    // 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
    os.write(msg.getBytes());
    // read data from client
    is.read(buffer);
    connection.close();
}
catch(IOException e) {
  e.printStackTrace();
}

    public BTMIDlet() { }

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

Many thanks in advance and good luck!

C
0
Comment
Question by:mingbaden
  • 2
4 Comments
 
LVL 9

Expert Comment

by:OBCT
ID: 13811955
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. :-)
0
 
LVL 9

Accepted Solution

by:
OBCT earned 1500 total points
ID: 13811973
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.

E.g.

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

try
{
  // Open streams, read and write data
}
catch (IOException e)
{
    e.printStackTrace();
}
finally
{
    try
    {
        if (out != null)
        {
            out.close();
        }

        if (in != null)
        {
            in.close();
        }

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

Author Comment

by:mingbaden
ID: 13890709
Sorry no reply for a while.

That was enough to get me going
cheers
0
 

Expert Comment

by:Stealthrt
ID: 23165607
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?

http://www.experts-exchange.com/Programming/Languages/Java/J2ME/Q_23981472.html

Thanks for your time!

David
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 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