// タイムアウト型にしないとデータがうまくひろえない場合がある private int recv(byte[] buf, int length) { int retval = 0; int totalRetval = 0; long endTime; long startTime = System.currentTimeMillis(); byte[] tmpbuf = new byte[length]; while (true) { retval = mComm.read(tmpbuf, length); // retval = mSerial.read(tmpbuf); if (retval > 0) { System.arraycopy(tmpbuf, 0, buf, totalRetval, retval); totalRetval += retval; startTime = System.currentTimeMillis(); if (DEBUG_SHOW_RECV) { Log.d(TAG, "recv(" + retval + ") : " + toHexStr(buf, totalRetval)); } } if (totalRetval >= length) { break; } endTime = System.currentTimeMillis(); if ((endTime - startTime) > 250) { Log.e(TAG, "recv timeout."); break; } } return retval; }
// リードバッファをカラにする private int drain() { byte[] buf = new byte[1]; int retval = 0; long endTime; long startTime = System.currentTimeMillis(); while (true) { retval = mComm.read(buf, 1); if (retval > 0) { // startTime = System.currentTimeMillis(); if (DEBUG_SHOW_DRAIN) { Log.d(TAG, "drain(" + retval + ") : " + toHexStr(buf[0])); } } endTime = System.currentTimeMillis(); if ((endTime - startTime) > 1000) { break; } } return retval; }