从“传感器”三星Gear Live获取心率

时间:2014-07-09 21:39:10

标签: android wear-os

如何从Samsung Gear Live

上的附加传感器获得心率

我只是试图按

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

但我只得到这些传感器:

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

他们如何测量心率? Android W Apps的某些部分是开源的,所以我可以查看它们吗?

谢谢和问候。

3 个答案:

答案 0 :(得分:14)

Here是一个要点,展示如何阅读心率传感器。

它的主要内容是:

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);

您还需要AndroidManifest.xml中的以下条目

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

答案 1 :(得分:8)

一个小例子:(全部要点: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) {

        }
    }

添加您的清单

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

答案 2 :(得分:5)

有一篇博客文章与源代码讨论了这个问题:http://marctan.com/blog/2014/07/08/reading-heart-rate-data-from-samsung-gear-live/

博客文章中要注意的要点:

  • 该文章使用了除Sensor.TYPE_HEART_RATE之外的其他值,其精度级别仅为1。
  • 您还需要AndroidManifest.xml

    中的以下条目

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