我试图从输入流中读取数据,但如果程序没有接收到X数据量的时间,我想终止尝试并返回-1
。我以前使用Thread.sleep(X)
,但后来意识到这是一个完全不正确的做法。如果有人有任何想法,请让我知道。这里是我的代码从输入流中读取...等待只输入X时间的输入
try {
// Read from the InputStream
bytes = mmInStream.read(buffer, 0, length);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MainMenu.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothService.this.start();
//break;
}
我不确定这会起作用,它似乎与使用'Thread.sleep()'类似。我想调用'mmInStream.read()',如果在'InStream'获得一个字节之前经过了'X时间量',我想返回'-1'的值。 – JuiCe 2012-08-09 16:43:44