gpt4 book ai didi

安卓 : Streaming Heart Rate from Microsoft Band

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:25:44 25 4
gpt4 key购买 nike

我目前正在开发一个 Android 应用程序,它可以从 Microsoft Band 接收心率数据。这里我的 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接口(interface)

    @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);
    }

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

最佳答案

在他明确同意之前,您不会获得用户的 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 接口(interface)中处理。您需要的代码是:

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);
}
}

关于安卓 : Streaming Heart Rate from Microsoft Band,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30090668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com