2013-03-21 56 views
0

我正在運行一個好奇的錯誤,我從串行連接收到錯誤的值。我之前創建了一篇文章,但沒有人回覆。 [其他問題]:Bluetooth SPP (serial) glitchs (Android)緩衝區和值過濾器

因爲我想盡快完成這個項目,我想我將不得不實施一個「補丁」來過濾所有的值。如果新值大約爲我以前的值的+/- 15%,我會保留它,如果超過此截止範圍,我會放棄它。這裏是我收到的值的代碼:

hBluetooth = new Handler() { 
    public void handleMessage(android.os.Message msg) { 
     switch (msg.what) { 
     case RECEIVE_MESSAGE:             // If we receive a message 
      byte[] readBuf = (byte[]) msg.obj; 
      String stringIncome = new String(readBuf, 0, msg.arg1);    // Create string from byte array 
      stringBuilder.append(stringIncome);            
      int endOfLineIndex = stringBuilder.indexOf("\r\n");     // Determine the end-of-line 
      Log.e(TAG, "Line"+endOfLineIndex); 
      if (endOfLineIndex > 0) {           // If we are at the end-of-line we parsed all the data that was sent 
       rmsgBluetooth = stringBuilder.substring(0, endOfLineIndex);  // The string is extracted in a string object rmsgBluetooth 
       stringBuilder.setLength(0); 

有人可以幫我實現一個過濾器,將輸出保留值的字符串?謝謝。

回答

0

這裏是我的解決方案:

sensorReading = Float.parseFloat(rmsgBluetooth); 
         if(Math.abs(sensorReadingOld - sensorReading)<10 || sensorReadingOld == 0){ 
          sensorReadingOld = sensorReading;  

         } 
         else { 
          sensorReading = sensorReadingOld; 
         }