2013-03-21 55 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; 
         }