4

I know this can be quite simple a question and many people have asked the same question but still I have problem in getting the heart rate. Of course I have done so much research for the solution.

Generally, for using a heart rate sensor, we need get the permission and use sensormanager and need registerListener for the sensor in mainActivity.

Some have said here for SDK 23 or higher, we need declare the permission separately but till present I develop only the part of Android Wear and there is nothing about the phone.

And I have also tried reinstall and rerun the application. All these methods don't work for me.

Here is my code, all I want is showing the heart rate on the watch.

AWHeartRateSensor\wear\src\main\java\com\example\android\awheartratesensor\MainActivity.java

package com.example.android.awheartratesensor;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.TextView;

import static com.google.android.gms.wearable.DataMap.TAG;

public class MainActivity extends Activity {

private SensorManager mSensorManager;
private Sensor mHeartSensor;
private TextView mTextView;
WatchViewStub mStub;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
        mStub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    mStub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
            mTextView.setText("I am here");
        }
    });

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mHeartSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);


    mStub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
            mTextView.setText("I have the sensor manager");
        }
    });

}
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(mSensorEventListener, mHeartSensor, SensorManager.SENSOR_DELAY_NORMAL);
}

protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(mSensorEventListener);
}
private SensorEventListener mSensorEventListener = new SensorEventListener() {


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        final SensorEvent event1=event;

        mStub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mTextView = (TextView) stub.findViewById(R.id.text);
                mTextView.setText(Float.toString(event1.values[0]));
            }
        });
    }


    };
}

Here is the manifest.xml

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.awheartratesensor">

    <uses-feature android:name="android.hardware.type.watch" />
    <uses-permission android:name="android.permission.BODY_SENSORS" />

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">
        <uses-sdk android:minSdkVersion="23"
            android:targetSdkVersion="23"
            />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
halfer
  • 19,824
  • 17
  • 99
  • 186
Xinxin
  • 135
  • 2
  • 7
  • Try their code like this [one](https://github.com/alejandrocq/HeartRateTest). This will help you see what your code is lacking. Also remember to set the min and target SDK version to match the watch. Lastly, try setting up a new project with all the suggested solutions provided by the related SO post you've linked. Hope this helps. – Mr.Rebot Jun 03 '17 at 15:25
  • Thanks a lot for your answer, it's pretty cool but it doesn't work for me. In fact, it keeps showing "please wait..." without the bpm number. However, your code is more logique. – Xinxin Jun 06 '17 at 09:55
  • @Xinxin are you able to get heart rate using bluetooth? – Neal Feb 26 '19 at 11:41
  • @Neal, It worked but I forgot how I fixed this issue(it has been 2 years....). You can check my [github](https://github.com/keweixinfr/HealthMonitor.git) to see the final version. – Xinxin Feb 27 '19 at 16:13

1 Answers1

5

There are two ways to solve this problem.

For a quick fix

Go to the app (or 'App Info') section of settings in your wearable device and see if the permission for body sensor's is checked/enabled for your app. The permission for the app is always unchecked by default, even if you include the permission in your manifest. When you enable the permission in settings, the app will start using the heart rate sensor properly.

Proper fix

In addition to adding the permission in the manifest, Android requires you to implement a runtime permission for 'Body Sensors'. This runtime permission is only required once. After that it will always work as intended.

adonayresom
  • 280
  • 3
  • 15