我目前正在Microsoft Studio中为Microsoft乐队编写一个Android应用程序,该应用程序将记录来自GSR,HR和Skin Temp的数据。

我有GSR和“皮肤温度”的数据。当前正在读取该应用程序,但其更新速度非常慢,尤其是对于“皮肤温度”而言。我想知道是否有一种方法可以使这些传感器更频繁地发送数据,因为我现在拥有的数据间隔太长,无法满足我的用途。这是我的MainPage.java文件。

public class MainPage extends Activity {

    private BandClient client = null;
    TextView tvGSR;
    TextView tvHeartRate;
    TextView tvTemperature;

    Button updateTest;

    private BandGsrEventListener mGsrEventListener = new BandGsrEventListener() {
        @Override
        public void onBandGsrChanged(final BandGsrEvent event) {
            if (event != null) {
                appendGSRToUI(String.format("%d kΩ\n", event.getResistance()));
            }
        }
    };

    private BandSkinTemperatureEventListener tempEventListener = new BandSkinTemperatureEventListener() {
        @Override
        public void onBandSkinTemperatureChanged(final BandSkinTemperatureEvent event) {
            if (event != null) {
                appendTempToUI(String.format("%.2f ºF\n", event.getTemperature()*1.800 + 32.00));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);

        tvGSR = (TextView) findViewById(R.id.tvGSR);
        tvTemperature = (TextView) findViewById(R.id.tvTemperature);

        updateTest = (Button) findViewById(R.id.updateTest);
        updateTest.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tvGSR.setText("");
                tvTemperature.setText("");
                new GsrSubscriptionTask().execute(); // Put first (runs connection)
                new TempSubscriptionTask().execute();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        tvGSR.setText("");
        tvTemperature.setText("");
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (client != null) {
            try {
                client.getSensorManager().unregisterGsrEventListener(mGsrEventListener);
                client.getSensorManager().unregisterSkinTemperatureEventListener(tempEventListener);
            } catch (BandIOException e) {
                appendGSRToUI(e.getMessage());
                appendTempToUI(e.getMessage());
            }
        }
    }

    @Override
    protected void onDestroy() {
        if (client != null) {
            try {
                client.disconnect().await();
            } catch (InterruptedException e) {
                // Do nothing as this is happening during destroy
            } catch (BandException e) {
                // Do nothing as this is happening during destroy
            }
        }
        super.onDestroy();
    }

        private class GsrSubscriptionTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                if (getConnectedBandClient()) {
                    int hardwareVersion = Integer.parseInt(client.getHardwareVersion().await());
                    if (hardwareVersion >= 20) {
                        appendGSRToUI("Band is connected.\n");
                        client.getSensorManager().registerGsrEventListener(mGsrEventListener);
                    } else {
                        appendGSRToUI("The Gsr sensor is not supported with your Band version. Microsoft Band 2 is required.\n");
                    }
                } else {
                    appendGSRToUI("Band isn't connected. Check Bluetooth.\n");
                }
            } catch (BandException e) {
                String exceptionMessage="";
                switch (e.getErrorType()) {
                    case UNSUPPORTED_SDK_VERSION_ERROR:
                        exceptionMessage = "SDK Version Outdated.\n";
                        break;
                    case SERVICE_ERROR:
                        exceptionMessage = "GSR Not Supported\n";
                        break;
                    default:
                        exceptionMessage = "Unknown error occured: " + e.getMessage() + "\n";
                        break;
                }
                appendGSRToUI(exceptionMessage);

            } catch (Exception e) {
                appendGSRToUI(e.getMessage());
            }
            return null;
        }
    }

    private class TempSubscriptionTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                if (true) {
                    int hardwareVersion = Integer.parseInt(client.getHardwareVersion().await());
                    if (hardwareVersion >= 20) {
                        appendTempToUI("Band is connected.\n");
                        client.getSensorManager().registerSkinTemperatureEventListener(tempEventListener);
                    } else {
                        appendTempToUI("Temperature Not Supported.\n");
                    }
                } else {
                    appendTempToUI("Band isn't connected. Check Bluetooth\n");
                }
            } catch (BandException e) {
                String exceptionMessage="";
                switch (e.getErrorType()) {
                    case UNSUPPORTED_SDK_VERSION_ERROR:
                        exceptionMessage = "SDK Version Outdated\n";
                        break;
                    case SERVICE_ERROR:
                        exceptionMessage = "Microsoft Health App Error\n";
                        break;
                    default:
                        exceptionMessage = "Unknown error occured: " + e.getMessage() + "\n";
                        break;
                }
                appendTempToUI(exceptionMessage);

            } catch (Exception e) {
                appendTempToUI(e.getMessage());
            }
            return null;
        }
    }

    private void appendGSRToUI(final String string) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvGSR.setText(string);
            }
        });
    }

    private void appendTempToUI(final String string) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvTemperature.setText(string);
            }
        });
    }

    private boolean getConnectedBandClient() throws InterruptedException, BandException {
        if (client == null) {
            BandInfo[] devices = BandClientManager.getInstance().getPairedBands();
            if (devices.length == 0) {
                appendGSRToUI("Band isn't paired with your phone.\n");
                return false;
            }
            client = BandClientManager.getInstance().create(getBaseContext(), devices[0]);
        } else if (ConnectionState.CONNECTED == client.getConnectionState()) {
            return true;
        }

        appendGSRToUI("Band is connecting...\n");
        return ConnectionState.CONNECTED == client.connect().await();
    }
}

最佳答案

当前没有任何一种方法可以以比Microsoft Band SDK提供的更快的速率获取数据。

另外,取决于您想要的数据,皮肤温度数据可能对您没有用。查看您要订阅的传感器,看来您的应用程序可能与健康有关。但是皮肤温度数据包含带内几个温度计之一的原始值。而且,表带本身会在内部产生一些热量,因此数据不太可能准确地代表佩戴者的皮肤温度。

关于android - 微软乐队-传感器更新率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35002259/

10-13 04:13