微软带 - 传感器更新率

时间:2016-01-25 20:59:44

标签: android sensor microsoft-band

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

我有GSR和Skin Temp的数据。目前正在阅读应用程序,但它更新的速度非常慢,特别是对于皮肤温度。我想知道是否有办法让这些传感器更频繁地发送数据,因为我现在拥有的数据的间隔时间太长,我正在使用它。这是我的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();
    }
}

1 个答案:

答案 0 :(得分:1)

目前无法以比Microsoft Band SDK提供的更快的速度获取数据。

此外,根据您想要的数据,皮肤温度数据可能对您没有用。查看您要订阅的传感器,看起来您的应用程序可能与健康相关。但是皮肤温度数据包含了乐队内部几个温度计之一的原始值。并且乐队本身会在内部产生一些热量,因此数据不太可能完全代表佩戴者的皮肤温度。