简体   繁体   中英

Java/Android - Writing to Outputstream after reading from Inputstream

On my PC i start a Server to send Bytes over Bluetooth to an Android Phone. I am sending Every 10 seconds different Bytes using the same Outputstream.

outputStream.write(buffer);
outputStream.flush();
thisThread.sleep(10000);

buffer being a bytearray which changes every loop

Now i want to read the buffer on the Android Phone:

while((bytes = inputStream.read(buffer)) != -1) {
  //do something
}
//do more

My problem is now that read(buffer) doesnt return -1. How can i notify the Android Phone that the bytes have been transfered so i can do the things i want after the while loop?

Have you tried closing connection on PC side? From your example it looks like you have not.

[edit] if you do not want to close connection on PC side, then send known byte sequence which will indicate to andorid app that PC side finished sending data and ie. is waiting for response

try this...

while((bytes = inputStream.read(buffer)) > 0)
{
//
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM