Obecnie tworzę aplikację na Androida, która odbiera dane o tętnie z zespołu Microsoft Band. Oto mój aktywny modyfikowany przykładowy projekt Akcelerometr:Android: Streaming Heart Rate z zespołu Microsoft Band
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();
}
}
Ale ja dostaję ten błąd podczas uruchamiania aplikacji:
Unknown Error occured : User has not given consent for use of heart rate data
Potem sprawdzić dokumentację, to mówi:
Realizacja interfejsu HeartRateConsentListener
@Override public void userAccepted(boolean consentGiven) { // handle user's heart rate consent decision };
Zapewnienie użytkownik wyraził zgodę na czujnik tętna strumieniowe
// 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); }
Problemem jest to, że nie mam pojęcia, jak wdrożyć to, co mówi doc na mojego kodu.
Edytowałem znaczniki do tego pytania, aby usunąć znacznik [tag: microsoft], który nie powinien być używany, jak podano w tagu wiki (chociaż jego dalsze istnienie jest problematyczne samo w sobie); jest to związane z pytaniem o Metę, o które prosimy tutaj: http://meta.stackoverflow.com/q/293754/82548. –