0

我遇到了一个奇怪的错误,我从串行连接接收到错误的值。我之前创建了一个帖子,但还没有人回复。[其他问题]:蓝牙SPP(串行)故障(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);

有人可以帮我实现一个过滤器,以字符串形式输出保留值吗?谢谢。

4

1 回答 1

0

这是我的解决方案:

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

                        }
                        else {
                            sensorReading = sensorReadingOld;
                        }
于 2013-03-22T03:03:19.993 回答