1

I've got Android device acting as a client, the PC is a Bluetooth Server, using Bluecove library

Code snippet from the client:

btSocket = serverBt.createRfcommSocketToServiceRecord(myUuid);
btAdapter.cancelDiscovery();
btSocket.connect();

InputStream in = btSocket.getInputStream();
OutputStream out = btSocket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out);
InputStreamReader isr = new InputStreamReader(in);
osw.write(55);
osw.flush();
out.flush();
//osw.close();
logTheEvent("Stuff got written, now waiting for the response.");
int dummy = isr.read();
logTheEvent("Servers response: "+ new Integer(dummy).toString());

And the server:

StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString, Connector.READ_WRITE );
StreamConnection incomingConnection=streamConnNotifier.acceptAndOpen();
InputStream in = incomingConnection.openInputStream();
OutputStream out = incomingConnection.openOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(out);
InputStreamReader isr = new InputStreamReader(in);
int fromClient = isr.read();
System.out.println("Got from client " + new Integer(fromClient).toString());
osw.write(999);

When the osw.close(); at the client is uncommented, the message gets transferred to the server, however, client is then unable to receive the response, IOException with message "socket already closed" is thrown. However, when osw.close(); is commented, both client and server freeze: A. Client hangs on reading server's response of course B. Server hangs on streamConnNotifier.acceptAndOpen();

What should be done to enable two-way communication? Is my code, or PC Bluetoototh stack, or bluecove to blame?

soliloquyy
  • 355
  • 3
  • 14

1 Answers1

2

Bluetooth uses buffered output. That means there is a small memory location that contains all of the data you write to the stream. When this memory location gets full, it writes the buffer data to the socket in a packet. When you prematurely close the socket, that buffer gets wiped, and the data is gone.

In order to force the stream to write, try calling flush()

Something else you could do is set the buffer size to be very small, so data always gets written. The performance won't be very good if you do this, though.

Unfortunately, I don't have all of the code I wrote, but there's a base project here

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • That's the problem, flushing the output stream at client's side does virtually nothing – soliloquyy Oct 15 '12 at 22:00
  • I'm not sure, then, sorry. It's been a couple years since I've struggled with this. :) – Codeman Oct 15 '12 at 22:08
  • Gave it another thought and just wrote many, many integers into the socket, and voila!, they got transmitted :) So seems like indeed, reaching the buffer size is needed to push the data without closing the stream. – soliloquyy Oct 18 '12 at 08:30
  • Great! :) Can you mark this is answered so other people can see? – Codeman Oct 21 '12 at 22:13