5

How do I get the heartrate from the attached sensor on the Samsung Gear Live

I just tried to list all Sensors by

SensorManager  mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL);
for(Sensor s : deviceSensors){
    Log.i(TAG, "" + s.getName());
}

But I only get theses Sensors:

07-09 23:18:05.047    3269-3269/com.sample.soma.wapp I/MyActivity﹕ MPU6515 Acceleration Sensor
07-09 23:18:05.047    3269-3269/com.sample.soma.wapp I/MyActivity﹕ MPU6515 Gyroscope Sensor
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ AK8963C Magnetic field Sensor
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ AK8963C Magnetic Sensor UnCalibrated
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ SAMSUNG Step Detector Sensor
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ SAMSUNG Step Counter Sensor
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ SAMSUNG Significant Motion Sensor
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ SAMSUNG Game Rotation Vector
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ SAMSUNG Tilt Wake Sensor
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ MPL Rotation Vector
07-09 23:18:05.057    3269-3269/com.sample.soma.wapp I/MyActivity﹕ MPL Orientation
07-09 23:18:05.067    3269-3269/com.sample.soma.wapp I/MyActivity﹕ MPL Gravity
07-09 23:18:05.067    3269-3269/com.sample.soma.wapp I/MyActivity﹕ MPL Linear Accelration

How do they measure the heart rate? Are some parts of the Android W Apps open sourced so I can have a look at them?

Thanks and Greets.

A.S.
  • 4,574
  • 3
  • 26
  • 43

3 Answers3

14

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.S.
  • 4,574
  • 3
  • 26
  • 43
Peter Friese
  • 6,709
  • 31
  • 43
  • Thanks for this! Missed the permission. – A.S. Jul 11 '14 at 07:52
  • @Peter Friese, if I call getSystemService() inside a service, I got an NPE error on `mBase' context, do you have a solution to that? I kinda come up with something, but it's not thourough. I posted here, http://stackoverflow.com/questions/24970677/calling-getsystemservice-inside-a-service can you help me with that? Much appreciated. – EyeQ Tech Jul 28 '14 at 00:57
  • Hi @TungMaiLe - did you manage to solve the NPE issue? Usually, the reason is you're trying to access an instance before it is properly instantiated. – Peter Friese Jul 28 '14 at 18:29
  • 1
    Hi @PeterFriese, yes I did solve it with a thorough solution, the reason was I initialized the Service object and call onCreate(), onStartCommand() directly. I changed it to using the calling context to call startService(). I'm still wondering why onCreate(), onStartCommand() are left as public functions but calling them causes misbehavior. – EyeQ Tech Aug 03 '14 at 13:35
8

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" />
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Since the last 23 update on huawei watch the heart rate sensor is always null: `Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);` – drindt Mar 26 '16 at 08:56
  • I found the issue, with api 23 comes the story user granted permissions per app. So if your sensor is null then you haven't the permission granted in the settings of your watch or you forgot it in the manifest. – drindt Mar 26 '16 at 10:14
5

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" />

Romin
  • 8,708
  • 2
  • 24
  • 28