I don't think I fully understand the code behind heart rate monitoring. I created my project and this is the code I used.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enables Always-on
setAmbientEnabled();
mMobvoiApiClient = new MobvoiApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mSensorManager =
(SensorManager)getSystemService(SENSOR_SERVICE);
mHeartRate = mSensorManager.getDefaultSensor(
Sensor.TYPE_HEART_RATE);
}
I connect to the client here and get the default sensor here, declare it as TYPE_HEART_RATE.
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mHeartRate,
SensorManager.SENSOR_DELAY_NORMAL);
}
In the onResume method I register the listener.
public void onSensorChanged(SensorEvent sensorEvent) {
Log.d("Test", "Got the heart rate (beats per minute) : " +
String.valueOf(sensorEvent.values[0]));
}
So this method will generate log everytime the sensor data changes right? Correct me please, if I am wrong.
My main question is what method should I call or what listener should I use listener to activate my heart rate sensor on my watch. I am obviously missing out on something important but can't figure out what. For start I just want to activate the heart rate monitor to start getting data from it.
Thank you for answering.
EDIT: I already had the uses permission in the AndroidManifest file.
<uses-permission android:name="android.permission.BODY_SENSORS" />
EDIT 2:
I tried adding two button one to register the listener and one to unregister it. When I try to register it with the startMeasure()
method the boolean always returns Sensor Status:: Sensor registered: no
public void OnClickBtn(View v){
startMeasure();
}
public void OnClickBtnStop (View v){
stopMeasure();
}
private void startMeasure() {
boolean sensorRegistered = mSensorManager.registerListener(this, mHeartRate, SensorManager.SENSOR_DELAY_FASTEST);
Log.d("Sensor Status:", " Sensor registered: " + (sensorRegistered ? "yes" : "no"));
}
private void stopMeasure() {
mSensorManager.unregisterListener(this);
}
It is connected with my layout like this
<Button
android:id="@+id/utrip"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="60dp"
android:background="@color/blue"
android:onClick="OnClickBtn"
android:text="@string/start" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:background="@color/dark_red"
android:text="@string/stop"
android:onClick="OnClickBtnStop"/>
I also get this from logcat.
sphal namespace is not configured for this process.
I think this has to do something with the API. This occurs when I press the Stop Button.