我試圖從輸入流中讀取數據,但如果程序沒有接收到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