5

我目前正在开发一个从 Microsoft Band 接收心率数据的 Android 应用程序。这里我的 Activity 从项目示例 Accelerometer 修改:

    package com.microsoft.band.sdk.sampleapp;

import com.microsoft.band.BandClient;
import com.microsoft.band.BandClientManager;
import com.microsoft.band.BandException;
import com.microsoft.band.BandInfo;
import com.microsoft.band.BandIOException;
import com.microsoft.band.ConnectionState;
import com.microsoft.band.UserConsent;
import com.microsoft.band.sdk.sampleapp.streaming.R;
import com.microsoft.band.sensors.SampleRate;

import com.microsoft.band.sensors.BandHeartRateEvent;
import com.microsoft.band.sensors.BandHeartRateEventListener;
import com.microsoft.band.sensors.HeartRateConsentListener;



import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.os.AsyncTask;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class BandStreamingAppActivity extends Activity {

    private BandClient client = null;
    private Button btnStart;
    private TextView txtStatus;

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

        txtStatus = (TextView) findViewById(R.id.txtStatus);
        btnStart = (Button) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                txtStatus.setText("");
                new appTask().execute();
            }
        });
    }

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

    @Override
    protected void onPause() {
        super.onPause();
        if (client != null) {
            try {
                client.getSensorManager().unregisterAccelerometerEventListeners();
            } catch (BandIOException e) {
                appendToUI(e.getMessage());
            }
        }
    }

    private class appTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                if (getConnectedBandClient()) {
                    appendToUI("Band is connected.\n");
                    // client.getSensorManager().registerAccelerometerEventListener(mAccelerometerEventListener, SampleRate.MS128);
                    client.getSensorManager().registerHeartRateEventListener(heartRateListener);

                } else {
                    appendToUI("Band isn't connected. Please make sure bluetooth is on and the band is in range.\n");
                }
            } catch (BandException e) {
                String exceptionMessage="";
                switch (e.getErrorType()) {
                case UNSUPPORTED_SDK_VERSION_ERROR:
                    exceptionMessage = "Microsoft Health BandService doesn't support your SDK Version. Please update to latest SDK.";
                    break;
                case SERVICE_ERROR:
                    exceptionMessage = "Microsoft Health BandService is not available. Please make sure Microsoft Health is installed and that you have the correct permissions.";
                    break;
                default:
                    exceptionMessage = "Unknown error occured: " + e.getMessage();
                    break;
                }
                appendToUI(exceptionMessage);

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

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



    private BandHeartRateEventListener heartRateListener = new BandHeartRateEventListener() {
        @Override
        public void onBandHeartRateChanged(final BandHeartRateEvent event) {
            if (event != null) {
                appendToUI(String.format(" HR = %i", event.getHeartRate()));
            }
        }

    };


    private boolean getConnectedBandClient() throws InterruptedException, BandException {
        if (client == null) {
            BandInfo[] devices = BandClientManager.getInstance().getPairedBands();
            if (devices.length == 0) {
                appendToUI("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;
        }

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

但是,当应用程序运行时出现此错误:

Unknown Error occured : User has not given consent for use of heart rate data 

然后我检查文档,它说:

  1. 实现 HeartRateConsentListener 接口

    @Override
        public void userAccepted(boolean consentGiven) {
        // handle user's heart rate consent decision
        };
    
  2. 确保用户已同意心率传感器流式传输

    // check current user heart rate consent
    if(client.getSensorManager().getCurrentHeartRateConsent() !=
    UserConsent.GRANTED) {
    // user has not consented, request it
    // the calling class is both an Activity and implements
    // HeartRateConsentListener
    bandClient.getSensorManager().requestHeartRateConsent(this, this);
    }
    

问题是我不知道如何实现文档在我的代码上所说的内容。

4

1 回答 1

7

在用户明确同意之前,您不会获得用户的 HR 数据(他只需要同意一次)。

因此,不要使用这行代码:client.getSensorManager().registerHeartRateEventListener(heartRateListener);您必须检查UserConsent.GRANTED == true文档第三点中所说的是否。如果是true,您可以像以前一样注册 HR 传感器事件侦听器,但如果是,false您必须调用requestHeartRateConsent.

if(client.getSensorManager().getCurrentHeartRateConsent() == UserConsent.GRANTED) {
    startHRListener();
    } else {
// user has not consented yet, request it
client.getSensorManager().requestHeartRateConsent(BandStreamingAppActivity.this, mHeartRateConsentListener);
}

屏幕上将出现一个紫色对话框。用户可以选择是/否。他的选择将是 的值,b也将保存在UserConsent.GRANTED。如果b == true,现在您可以注册 HR 传感器事件侦听器,如果它是false做任何您想做的事情,以通知用户 HR 采集将无法工作。如文档第二点所述,所有这些都在 HeartRateConsentListener 接口中处理。您需要的代码是:

private HeartRateConsentListener mHeartRateConsentListener = new HeartRateConsentListener() {
        @Override
        public void userAccepted(boolean b) {
            // handle user's heart rate consent decision
            if (b == true) {
                // Consent has been given, start HR sensor event listener
                startHRListener();
            } else {
                // Consent hasn't been given
                appendToUI(String.valueOf(b));
            }
        }
    };

public void startHRListener() {
        try {
            // register HR sensor event listener
            client.getSensorManager().registerHeartRateEventListener(mHeartRateEventListener);
        } catch (BandIOException ex) {
            appendToUI(ex.getMessage(), band);
        } catch (BandException e) {
            String exceptionMessage="";
            switch (e.getErrorType()) {
                case UNSUPPORTED_SDK_VERSION_ERROR:
                    exceptionMessage = "Microsoft Health BandService doesn't support your SDK Version. Please update to latest SDK.";
                    break;
                case SERVICE_ERROR:
                    exceptionMessage = "Microsoft Health BandService is not available. Please make sure Microsoft Health is installed and that you have the correct permissions.";
                    break;
                default:
                    exceptionMessage = "Unknown error occurred: " + e.getMessage();
                    break;
            }
            appendToUI(exceptionMessage, band);

        } catch (Exception e) {
            appendToUI(e.getMessage(), band);
        }
    }
于 2015-05-07T17:18:42.523 回答