Reading Heart Rate Data from Samsung Gear Live

July 8, 2014


Here’s a quick tutorial on how to get data from the heart rate sensor in your Samsung Gear Live watch.

First, declare this permission in your manifest:


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

then get an instance of SensorManager and an instance of the Sensor that we want to use, the HR sensor in this case.


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

I initially tried to use Sensor.TYPE_HEART_RATE which was added in API Level 20 but I always end up getting accuracy = 1 which is the lowest sensor accuracy. Then I found out that the demo app loaded in Samsung Gear Live was actually using a different value for the sensor type:


mHeartRateSensor = mSensorManager.getDefaultSensor(65562);

This produces accuracy = 3 which is what we really need. This isn’t documented elsewhere (as far as I know) so maybe in the future, they’ll update the official documentation or they will fix it in future OS updates.

Using Sensor.TYPE_HEART_RATE
Using S65562 as sensor type

After that, you can now register a listener for sensor data by calling:



    protected void onStart() {
        super.onStart();

        mSensorManager.registerListener(this, this.mHeartRateSensor, 3);
    }

In this case, we let our activity be the listener for sensor data so we must implement SensorEventListener and override these two methods:


    
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Log.d(TAG, "sensor event: " + sensorEvent.accuracy + " = " + sensorEvent.values[0]);
    }

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

Lastly, we need to make sure that we unregister our listener when we exit our application.


    
    @Override
    protected void onStop() {
        super.onStop();

        mSensorManager.unregisterListener(this);
    }

Grabe the source code of the sample app here. Enjoy!

comments powered by Disqus