I am using bluetooth chat in order to connect and recieve data from a bluetooth device.
I use the following code for reading data:
public void run() {
byte[] buffer = new byte[1024];
int bytes;
Log.v("MR", "start listening....");
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
Log.d("MR", "buffer in try");
bytes = mmInStream.read(buffer);
Log.d("MR", "input stream :"+(new String(buffer)));
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Conn.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
Log.d("MR", "buffer after");
} catch (Exception e) {
Log.e("MR", "Error :"+e.getMessage());
//
connectionLost();
// break;
}
Log.d("MR", "buffer after while");
}
}
The device is sending data all the time without stopping.
With the above code I get the message of:
Log.d("MR", "buffer in try");
then it goes to the next line:
bytes=mmInStream.read(buffer);
and never returns from that call. I guess this is because it starts reading data from the device and doesn't stop until it disconnects. How can I read a certain amount of bytes at a time?
EDIT
Unless it stay to the bytes = mmInStream.read(buffer);
code due to that it don;t get any data back on from the device?