我目前正在开发一个从 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
然后我检查文档,它说:
实现 HeartRateConsentListener 接口
@Override public void userAccepted(boolean consentGiven) { // handle user's heart rate consent decision };
确保用户已同意心率传感器流式传输
// 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); }
问题是我不知道如何实现文档在我的代码上所说的内容。