Get Heart Rate from "Sensor" Samsung Gear Live

Here is a gist that shows how to read the heart rate sensor.

The meat of it is:

SensorManager mSensorManager = ((SensorManager)getSystemService(SENSOR_SERVICE));
Sensor mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
Sensor mStepCountSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
Sensor mStepDetectSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);

mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mStepCountSensor, SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this, mStepDetectSensor, SensorManager.SENSOR_DELAY_NORMAL);

You will also need the following entry in the AndroidManifest.xml

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

A little example: (full gist here:https://gist.github.com/gabrielemariotti/d23bfe583e900a4f9276)

public class MyActivity extends Activity implements SensorEventListener {

        //Sensor and SensorManager
        Sensor mHeartRateSensor;
        SensorManager mSensorManager;

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

            //Sensor and sensor manager
            mSensorManager = ((SensorManager)getSystemService(SENSOR_SERVICE));
            mHeartRateSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);

            //.... 
        }

        @Override
        protected void onResume() {
            super.onResume();
            //Register the listener
            if (mSensorManager != null){
                mSensorManager.registerListener(this, mHeartRateSensor, SensorManager.SENSOR_DELAY_NORMAL);
            }
        }

        @Override
        protected void onPause() {
            super.onPause();
            //Unregister the listener
            if (mSensorManager!=null)
                mSensorManager.unregisterListener(this);
        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            //Update your data. 
            if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {            
                 //heart rate = (int) event.values[0];
                }
            }
        }

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

        }
    }

Add in your Manifest

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

There is a blog post that discusses this along with the source code : http://marctan.com/blog/2014/07/08/reading-heart-rate-data-from-samsung-gear-live/

Key points to note from the blog post:

  • The article used a different value other than Sensor.TYPE_HEART_RATE , which gave an accuracy level of 1 only.
  • You will also need the following entry in the AndroidManifest.xml

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