2015-05-07 11 views
5

से स्ट्रीमिंग हार्ट रेट मैं वर्तमान में एक एंड्रॉइड ऐप विकसित कर रहा हूं जो माइक्रोसॉफ्ट बैंड से दिल की दर डेटा प्राप्त करता है। यहाँ मेरी गतिविधि परियोजना नमूना 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); 
    } 
    

समस्या स्ट्रीमिंग सेंसर के लिए सहमति दी मैं पता नहीं कैसे लागू करने के लिए क्या डॉक मेरी कोड पर कहते है कि है।

+1

मैं टैग इस सवाल का दूर करने के लिए [माइक्रोसॉफ्ट टैग] संपादित); यह मेटा प्रश्न के लिए यहां पूछा गया है: http://meta.stackoverflow.com/q/293754/82548। –

उत्तर

7

आपको उपयोगकर्ता के एचआर डेटा नहीं मिलेगा जब तक कि वह स्पष्ट रूप से सहमति नहीं देता (उसे केवल एक बार अपनी सहमति देने की आवश्यकता होती है)।

तो, कोड की इस पंक्ति को रखने के बजाय: client.getSensorManager().registerHeartRateEventListener(heartRateListener); आपको यह जांचना होगा कि UserConsent.GRANTED == true दस्तावेज़ीकरण के तीसरे बिंदु में कहा गया है या नहीं। यदि यह true है, तो आप एचआर सेंसर ईवेंट श्रोता को पंजीकृत कर सकते हैं, लेकिन यदि यह 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, अब आप एचआर सेंसर ईवेंट श्रोता पंजीकृत कर सकते हैं, यदि यह false है जो आप चाहते हैं कि उपयोगकर्ता को सूचित करने के लिए एचआर अधिग्रहण काम नहीं करेगा। दस्तावेज के दूसरे बिंदु में कहा गया है कि यह सब 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); 
     } 
    } 
संबंधित मुद्दे