views:

57

answers:

1

I am trying to build a bluetooth chat application using j2me. I created a thread which is used for connecting to other devices. Two devices can be connected now. I opened the input and output stream. I want to read and write data simultaneously from input and output stream. I dont have any idea how to achieve this?? Should i create a new thread which only reads and writes data??

Advice and please be descriptive as i m a novice in this field.

A: 

To get input data you can use a thread like this:

public class MyReceiver extends Thread {   
...
public void run() {
    try {
        Message msgIn = inputStream.read();
        msgIn.processMessage();
        // Create a class Message with a method like this for generic data handling
        // so if you have different types of message (text, comands, ...) each one becomes a class extending
        // Message and implementing this method for its own purpose
    } catch {
        // Error handling, like disconections...
    }
}
...
}

To send data you can just open an outputStream, write on it and flush:

OutputStream msgSender;
//Initialize and open you output stream
msgSender.write("Some cool message");
msgSender.flush();
eMgz
thanx implemented the same...and its working.
RishiPatel