2015-05-07 6 views
5

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:

  1. Realizacja interfejsu HeartRateConsentListener

    @Override 
        public void userAccepted(boolean consentGiven) { 
        // handle user's heart rate consent decision 
        }; 
    
  2. 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.

+1

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. –

Odpowiedz

7

Nie otrzymasz danych HR użytkownika, dopóki nie wyrazi on wyraźnej zgody (musi tylko raz wyrazić zgodę).

Zamiast tego wiersza kodu: client.getSensorManager().registerHeartRateEventListener(heartRateListener); należy sprawdzić, czy UserConsent.GRANTED == true, jak wspomniano w trzecim punkcie dokumentacji. Jeśli jest to true, możesz zarejestrować detektor zdarzeń czujnika HR tak jak to robiłeś, ale jeśli jest to false, musisz zadzwonić pod numer requestHeartRateConsent.

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

Na ekranie pojawi się fioletowe okno dialogowe. Użytkownik może wybrać TAK/NIE. Jego wybór będzie miał wartość b i zostanie również zapisany pod numerem UserConsent.GRANTED. Jeśli b == true, teraz możesz zarejestrować detektor zdarzeń czujnika HR, jeśli jest to false robić, co chcesz, aby powiadomić użytkownika, że ​​pozyskanie HR nie będzie działać. Wszystko to jest obsługiwane w interfejsie HeartRateConsentListener, jak wspomniano w drugim punkcie dokumentacji. Potrzebny kod to:

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