How to read Heart rate from Android Wear
Asked Answered
D

4

11

I need to read the heart rate of the user at that instant. I tried using this code located in my main activity in the Wear project.

public class MainActivity extends WearableActivity implements SensorEventListener {

    private static final String TAG = "MainActivity";
    private TextView mTextViewHeart;
    SensorManager mSensorManager;
    Sensor mHeartRateSensor;
    SensorEventListener sensorEventListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextViewHeart = (TextView) findViewById(R.id.heart);
        mSensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE));
        mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
        mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
        Log.i(TAG, "LISTENER REGISTERED.");
        mTextViewHeart.setText("Something here");


        mSensorManager.registerListener(sensorEventListener, mHeartRateSensor, mSensorManager.SENSOR_DELAY_FASTEST);
    }


    public void onResume(){
        super.onResume();
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.d(TAG, "onAccuracyChanged - accuracy: " + accuracy);
    }

    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
            String msg = "" + (int)event.values[0];
            mTextViewHeart.setText(msg);
            Log.d(TAG, msg);
        }
        else
            Log.d(TAG, "Unknown sensor type");
    }

}

This code is simply not working, giving me an error saying:

E/SensorManager﹕ sensor or listener is null

It works fine if I use anything else but the heart rate sensor.

I am using:

  • LG G4 with android API level 23
  • LG Urbane watch (Which I know has a heart rate sensor) with Wear API level 23

Thank you for your help.

Doom answered 21/4, 2016 at 5:24 Comment(1)
try this question ...#26489781Hoof
T
6

I faced the same problem as you can see in this question. We have the same wear devices, so I'm almost sure that you have missed this on your wear AndroidManifest.xml:

<uses-permission android:name="android.permission.BODY_SENSORS" />

Also, your should go to Permissions in Settings in order to check if that permission is enabled.

You can check my project on GitHub, which is working for now.

Transformer answered 21/4, 2016 at 8:12 Comment(3)
Hello! I actually do have the permission in my Wear manifest and it doesn't work... I will check your project. Thank you!Doom
do you know how to pass data to Sensor from adbSyrup
@prathamkesarkar Hi! Sorry for the late reply. I haven't tried that, so I can't help you.Transformer
F
3

Actually you are implementing the SensorEventListener interface and created an instance of SensorEventListener without initializing it.

Either initialize the sensor:

SensorEventListener sensorEventListener = new SensorEventListener(){ ... };

or remove it and use "this":

mSensorManager.registerListener(sensorEventListener, mHeartRateSensor, mSensorManager.SENSOR_DELAY_FASTEST);

change it to:

mSensorManager.registerListener(this, mHeartRateSensor, mSensorManager.SENSOR_DELAY_FASTEST);
Furlani answered 31/10, 2016 at 23:30 Comment(1)
The same solution for other smartwatches: Huawei, Sony and ASUS. Use only mSensorManager.registerListener(this, mHeartRateSensor, mSensorManager.SENSOR_DELAY_FASTEST);Mcmullin
R
1

Don't forget, from SDK 23 onwards you have to ask for permissions separately on the wear device and the phone. The Wear app no longer inherits permissions from the phone app.

https://developer.android.com/training/articles/wear-permissions.html

Realistic answered 14/11, 2016 at 10:59 Comment(0)
C
1

The permissions thing has caught me out a few times now. While I'm yet to implement code to request permission to use the sensors, a simple workaround is to go into settings on the wearable, then select permissions and locate your application. When you click on your application you'll notice sensors is disabled, so just click on it to enable them. Should work then!

Calle answered 17/1, 2017 at 10:4 Comment(1)
OMG! I literally spent two hours fighting against this. I'm not a java developer so I assumed I was doing something wrong. Nope, the app just needed permissions in settings. I did add permissions in the AndroidManifest.xml and thought that would be enough.Downtrend

© 2022 - 2024 — McMap. All rights reserved.