• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6381
  • Last Modified:

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.

Code:

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();

     //INSERT SENDS AND RECEIVES HERE

      connection.close();
      }
   catch(IOException e) {
         e.printStackTrace();
  }
 }
    protected void destroyApp(boolean unconditional) { }
    protected void pauseApp() {}
}
//END

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

Chris
0
mingbaden
Asked:
mingbaden
1 Solution
 
KnightleyCommented:
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
0
 
jjxiaoCommented:
Chris,

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?

thanks,

Jun
0
 
adeii2Commented:
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.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Tackle projects and never again get stuck behind a technical roadblock.
Join Now