[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3636
  • Last Modified:

Java - J2ME - Send serial over Bluetooth

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
 
objectsCommented:
Looks like you should be able to read/write bytes using the standard stream methods

0
 
objectsCommented:
something like:

os.write((int)'y');

int response = is.read();

Or if dealing with text then wrap your streams in a reader/writer.

Writer out = new InputStreamWriter(os);
0
 
KnightleyCommented:
i have seen this also in the blueooth section, so i posted my anwser there.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mingbadenAuthor Commented:
worked a charm - cheers
0
 
objectsCommented:
no worries :)
0
 
StealthrtCommented:
I'm bring this back from the dead in hopes that mingbaden or objects could help me out with a simualr 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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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