J2ME - send and receive on serial connection

Hi there,

I am trying to send and recieve serial commands from my phone (N6230) to what is essentially an RS-232 device over Bluetooth.
Basically I need to send the device a single character (e.g "y") and then receive the response.

I already have a basic program which sets up the connection and I know this works to open the connection so far - it is just this next bit i am stuck on.


import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.util.*;

public class BTMIDlet extends MIDlet {

    public BTMIDlet() { }
    public void startApp() {

   String ServiceURL = "btspp://0800171C3666:1;master=false;encrypt=false;authenticate=false";
    try {

    StreamConnection connection = (StreamConnection) Connector.open(ServiceURL);
    InputStream is = connection.openInputStream();
    OutputStream os = connection.openOutputStream();


   catch(IOException e) {
    protected void destroyApp(boolean unconditional) { }
    protected void pauseApp() {}

Any help anyone can give will be greatly received.
Thanks in advance.

Who is Participating?
KnightleyConnect With a Mentor Commented:
to send data:

int output_data=2;
 os .write(output_data);
//use some ASCI code to convert your character into integer

to read data:
put the codes inside a runnable.

int input_data;
while ( ( input_data= is.readByte()) != -1) {
//put some code here to convert the received char into int

hope this helps

Can you share the whole source code with me? I am trying to build up a simple bluetooto demo to send charaters from Pocket PC to a laptop through serial port. When I try to run your sample, I got the service address error. How did you know the service URL is "btspp://0800171C3666:1"? Is it the client service URL or server service URL?


Well, you can any another midlet, called BtDiscovery.java to got know MAC adress of BT devices nearby, append them to form and choose any of them for btspp-address.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.