随着更多数据的出现,我将如何继续进行计算

时间:2015-03-15 23:02:20

标签: android dynamic bluetooth real-time runnable

我正在尝试创建一个应用程序,该应用程序根据使用蓝牙接收并发送到应用程序的心电图信号计算心率。我已经能够创建将检测心电图信号中峰值的代码(这将是500个值的数组),计算心率并将其输出到屏幕。到目前为止,阵列是硬编码的。但我想确保每次覆盖数组时,再次执行计算并将新值输出到屏幕。所以它基本上需要不断重复。我尝试在以下循环中插入我的整个代码:

while(true) { }

如下所示:

private class ProcessECG extends Thread {
    public ProcessECG() {

    }

        public void run() {

        heartRateValues = (TextView) findViewById(R.id.heartRateValues);

        double rawHeartData[] = {0.024, -0.016, -0.032, 0.016, -0.008, 0.024, 0.016, -0.016, 0.016, -0.016, 0.0, 0.112, 0.096, -0.264, -0.272, 0.136, 0.272, 0.096, 0.088, -0.016, 0.024, -0.024, -0.008, -0.04, -0.032, 0.008, -0.024, 0.008, -0.016, 0.016, 0.024, -0.024, -0.016, 0.016, -0.016, 0.016, -0.016, 0.016, 0.024, -0.008, 0.008, 0.056, 0.056, 0.008, 0.016, -0.032, -0.032, 0.0, -0.024, 0.008, -0.016, 0.024, -0.016, 0.024, -0.016, 0.016, -0.016, 0.016, 0.016, -0.016, 0.024, -0.016, -0.008, 0.024, -0.016, 0.024, 0.024, -0.016, -0.008, 0.016, 0.024, -0.016, 0.016, -0.016, -0.016, 0.024, -0.016, 0.024, -0.008, 0.024, -0.016, 0.024, 0.024, -0.016, -0.016, 0.024, -0.016, 0.016, 0.016, -0.016, -0.016, 0.024, 0.016, -0.024, -0.016, 0.024, 0.024, -0.016, 0.016, -0.016, -0.016, 0.048, 0.08, -0.064, -0.08, -0.216, -0.144, 0.2, 0.208, 0.024, 0.024, -0.008, 0.008, -0.032, -0.032, 0.008, -0.024, 0.016, 0.016, -0.024, -0.024, 0.016, 0.016, -0.024, -0.016, 0.016, -0.024, 0.016, -0.008, 0.016, -0.008, 0.032, 0.008, 0.048, 0.032, -0.016, 0.0, -0.032, -0.032, 0.0, -0.024, 0.024, 0.024, -0.016, -0.024, 0.024, -0.016, 0.016, 0.024, -0.016, -0.016, 0.024, 0.024, -0.016, -0.016, 0.024, 0.024, -0.016, -0.016, 0.016, 0.024, -0.016, -0.016, 0.024, -0.016, 0.024, -0.016, 0.024, -0.008, 0.024, 0.016, -0.016, -0.016, 0.024, -0.016, 0.016, -0.016, 0.016, -0.008, 0.024, 0.016, -0.016, -0.008, 0.024, -0.016, 0.024, 0.016, -0.024, 0.008, -0.024, -0.008, 0.024, 0.024, -0.016, -0.008, 0.016, -0.008, 0.08, 0.064, -0.264, -0.232, 0.192, 0.224, 0.072, 0.056, -0.032, 0.016, -0.032, -0.032, -0.008, -0.032, 0.008, 0.016, -0.016, -0.024, 0.016, -0.016, 0.016, 0.024, -0.016, 0.016, -0.024, 0.016, -0.016, -0.008, 0.024, 0.0, 0.04, 0.056, 0.0, 0.024, -0.024, 0.0, -0.032, -0.032, 0.016, -0.024, 0.016, -0.016, 0.016, 0.024, -0.016, 0.024, -0.008, -0.016, 0.016, -0.016, 0.016, 0.016, -0.016, 0.024, -0.016, 0.024, -0.016, -0.016, 0.024, -0.016, 0.016, -0.016, 0.024, 0.024, -0.016, 0.024, -0.016, -0.016, 0.016, -0.016, 0.024, 0.024, -0.024, 0.016, -0.016, 0.024, -0.016, 0.016, -0.016, -0.016, 0.016, -0.016, 0.024, 0.024, -0.016, -0.008, 0.024, -0.008, 0.104, 0.08, -0.28, -0.28, 0.12, 0.256, 0.128, 0.12, -0.032, 0.008, -0.016, -0.04, -0.008, -0.032, 0.008, 0.0, -0.024, -0.016, 0.024, -0.016, 0.016, 0.024, -0.024, -0.024, 0.016, 0.016, -0.016, -0.016, 0.024, 0.0, 0.056, 0.056, 0.0, 0.016, -0.032, 0.0, -0.032, -0.032, 0.016, -0.024, 0.016, -0.024, 0.016, -0.016, 0.016, 0.016, -0.016, -0.016, 0.024, 0.016, -0.016, 0.016, -0.016, -0.016, 0.024, -0.008, 0.024, -0.016, 0.024, -0.016, 0.024, -0.016, 0.024, -0.016, 0.024, 0.024, -0.016, 0.024, -0.016, 0.016, -0.008, -0.016, 0.016, -0.016, 0.016, 0.016, -0.016, 0.024, -0.016, -0.016, 0.024, -0.016, 0.024, 0.024, -0.032, 0.008, -0.032, -0.008, 0.024, 0.024, -0.008, 0.024, -0.016, -0.016, 0.024, 0.112, -0.072, -0.088, -0.272, -0.128, 0.272, 0.248, -0.008, 0.024, -0.024, 0.0, -0.048, -0.032, 0.0, -0.032, 0.008, -0.032, 0.016, -0.024, 0.016, 0.016, -0.016, 0.024, -0.016, 0.016, -0.024, -0.016, 0.016, -0.016, 0.024, 0.008, 0.056, 0.04, -0.008, 0.016, -0.032, -0.04, 0.0, -0.024, 0.016, 0.016, -0.024, 0.016, -0.016, 0.024, -0.016, -0.016, 0.016, -0.016, 0.024, -0.016, 0.016, 0.024, -0.008, 0.024, -0.016, -0.016, 0.024, -0.016, 0.016, 0.016, -0.016, -0.016, 0.024, 0.024, -0.016, -0.008, 0.024, 0.016, -0.024, -0.008, 0.024, 0.024, -0.032, 0.024, -0.016, -0.008, 0.016, -0.016, 0.024, -0.016, 0.024, 0.024, -0.016, 0.024, -0.016, -0.016, 0.016, -0.024, 0.008, -0.008, 0.024, 0.024, -0.016, 0.024, -0.016, -0.016, 0.04, 0.096, -0.152, -0.256, -0.112, -0.088, 0.264, 0.248, 0.008, -0.024, 0.008, 0.0, -0.04, -0.032, 0.0, -0.032, 0.008, -0.024, 0.016, -0.024, 0.016, -0.016, 0.024, -0.024, 0.016, 0.016, -0.024, -0.016, 0.024, -0.008, 0.032, 0.008, 0.056, 0.04, -0.016, 0.008, -0.032, 0.0, -0.032, -0.032, 0.016, -0.016, 0.016, 0.016, -0.016, 0.024, -0.016, -0.016, 0.024, -0.016, 0.024, -0.016, 0.016, 0.024, -0.016, 0.024, -0.016, -0.024, 0.024, -0.016, 0.024, -0.016, 0.016, -0.016, 0.016, 0.024, -0.016, 0.024, -0.008, -0.016, 0.024, -0.024, 0.016, -0.016, 0.016, -0.016, 0.024, 0.024, -0.016, -0.008, 0.024, 0.016, -0.024, -0.032, 0.008, -0.008, 0.024, 0.024, -0.016, 0.016, -0.016, -0.016, 0.064, 0.104, -0.224, -0.264, 0.024, 0.04, 0.248, 0.176, -0.024, -0.032, 0.016, -0.032, 0.0, 0.0, -0.032, 0.016, -0.024, -0.032, 0.008, -0.016, 0.016, 0.016, -0.032, 0.016, -0.024, 0.016, -0.016, -0.016, 0.024, -0.008, 0.04};


        double max = 0;
        for (int i = 0; i <rawHeartData.length; i++) {
            if (rawHeartData[i] > max) {
                max = rawHeartData[i];
            }
        }
        double threshold = 0.7*max;



        while (true) {

            //Calculate Heart Rate from list "Raw Data"
            int upflag = 0;
            int last = 1;
            int p = 0;
            int t = 0;
            int count = 0;
            //List<Double> heartRate2 = new ArrayList<Double>();
            int heartRate[] = new int[7];

            for (int i = 0; i < rawHeartData.length; i++) {
                if (rawHeartData[i]> threshold){
                    if (upflag == 0){
                        if (last > 0){
                            t = i - last;
                            p = 100*60/t;
                            //100 is the sampling rate

                            heartRate[count]= p;
                            count = count + 1;
                            //int k = (int) p;
                            //heartRate2.add(p);
                        }
                        last = i;
                    }
                    upflag = 50;
                }

                else {
                    if (upflag > 0){
                        upflag = upflag -1;
                    }
                }

            }
            //Change array to String and display last value
            String heartRate3 = Arrays.toString(heartRate);
            String[] bits = heartRate3.split(",");
            String lastValue = bits[bits.length-1];

            heartRateValues.setText(lastValue + "bpm");

        }

        }


}

然而,这只是导致我的应用程序崩溃,我收到以下错误:

03-15 14:22:50.809  10094-10300/es.pymasde.blueterm E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-1165
Process: es.pymasde.blueterm, PID: 10094
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6247)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:867)
        at android.view.View.requestLayout(View.java:17364)
        at android.view.View.requestLayout(View.java:17364)
        at android.view.View.requestLayout(View.java:17364)
        at android.view.View.requestLayout(View.java:17364)
        at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
        at android.view.View.requestLayout(View.java:17364)
        at android.widget.TextView.checkForRelayout(TextView.java:6855)
        at android.widget.TextView.setText(TextView.java:4047)
        at android.widget.TextView.setText(TextView.java:3905)
        at android.widget.TextView.setText(TextView.java:3880)
        at es.pymasde.blueterm.ProcessActivity$ProcessECG.run(ProcessActivity.java:164)

当涉及到setText时,它会出错,但当代码在while循环之外时,它会完美地运行。我发现以下链接以回应我的错误: Android "Only the original thread that created a view hierarchy can touch its views." error in Fragment 但是,我仍然不确定如何使用runOnUIThread。这是最好的解决方案还是有任何关于如何确保在新数据进入时实时执行我的计算的想法?

1 个答案:

答案 0 :(得分:-1)

嗯,无论怎样,你都需要在ui线程上setText

但是,根据您处理的数据量和频率,您可能希望以一定的间隔执行此操作,而这段时间不会对UI线程造成太多影响。