Hi,
I make an application in java ME (client) that connects to a PC application (server) using OBEX protocol. Connection goes well but I don't understand why I can use the stream to send JUST one message
frmo the client and after that I must close the stream to see the message at the server.
I have these codes:
SERVER (PC)
public void startServer() throws IOException{
LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
serverConnection = (SessionNotifier) Connector.open("btgoep://localhost:"
+ serverUUID + ";name=ObexExample");
handler = new RequestHandler();
System.out.println("waiting for clients...");
serverConnection.acceptAndOpen(handler);
System.out.println("Received OBEX connection ");
}
String serverUUID = "123";
Operation operation;
DataInputStream dis;
InputStream is;
private class RequestHandler extends ServerRequestHandler {
@Override
public int onPut(Operation op) {
try {
HeaderSet hs = op.getReceivedHeaders();
String name = (String) hs.getHeader(HeaderSet.NAME);
if (name != null) {
System.out.println("put name:" + name);
}
dis = op.openDataInputStream();
int a = 0;
//just show me incoming mesages as they arrive
while((a=dis.readInt())!=-1000);// I RECEIVE THE MESSAGE HERE ONLY ONE TIME
System.out.println(a);
return ResponseCodes.OBEX_HTTP_OK;
} catch (IOException e) {
e.printStackTrace();
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
}
}
}
CLIENT (Mobile Phone)
public void startObexClient(){
if (serverURL == null) {
String[] searchArgs = null;
try {
searchArgs = new String[] { "123" };
System.out.println(LocalDevice.getLocalDevice().getBluetoothAddress());
ServicesSearch.main(searchArgs);
} catch (IOException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
if (ServicesSearch.serviceFound.size() == 0) {
System.out.println("OBEX service not found");
return;
}
// Select the first service found
serverURL = (String)ServicesSearch.serviceFound.elementAt(0);
}
System.out.println("Connecting to " + serverURL);
try {
clientSession = (ClientSession) Connector.open(serverURL);
} catch (IOException ex) {
ex.printStackTrace();
}
HeaderSet hsConnectReply = null;
try {
hsConnectReply = clientSession.connect(null);
} catch (IOException ex) {
ex.printStackTrace();
}
try {
if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
System.out.println("Failed to connect");
return;
}
} catch (IOException ex) {
ex.printStackTrace();
}
HeaderSet hsOperation = clientSession.createHeaderSet();
hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
hsOperation.setHeader(HeaderSet.TYPE, "text");
//Create PUT Operation
//Operation putOperation = null;
try {
putOperation = clientSession.put(hsOperation);
} catch (IOException ex) {
ex.printStackTrace();
}
}
Now... from the client I send messages like this:
try {
DataOutputStream das = putOperation.openDataOutputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
try {
das.writeInt(12);
das.close();
} catch (IOException ex) {
ex.printStackTrace();
}
it only works ONE time :(. Why? I also tried to open the stream just one time and after that just do:
das.writeInt(12);
das.flush();//just as a Socket object...
but nothing happens until I close the stream :(...
my question is: HOW CAN I SEND MESSAGES CONTINOUSLY? (using the same stream)