title: Java Android - Create a simple Heart rate monitor
body: <p>I've a Samsung galaxy S7 and I want that on touch heart sensor (back the phone near the camera), the application read heart rate and display it into a TextView. </p>
  
  <p>I've tried it, but it doesn't run.</p>
  
  <p><strong>My activity</strong></p>
  
  <pre><code>import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorManager;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.hardware.SensorEventListener;
  import android.util.Log;
  import android.widget.TextView;
  
  public class MainActivity extends AppCompatActivity implements SensorEventListener{
      public TextView testoLabel;
      private static final String TAG = "MyActivity";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Log.e(TAG, "onCreate called");
  
          testoLabel = (TextView) findViewById(R.id.testoLabel);
  
         SensorManager sMgr;
          sMgr = (SensorManager)this.getSystemService(SENSOR_SERVICE);
  
          Sensor battito = null;
          battito = sMgr.getDefaultSensor(Sensor.TYPE_HEART_RATE);
          if(battito != null)
              testoLabel.setText("load sensor");
          else
              testoLabel.setText("no load sensor");
  
          sMgr.registerListener(this, battito,SensorManager.SENSOR_DELAY_NORMAL);
  
      }
      @Override
      public void onSensorChanged(SensorEvent event) {
          if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
              String msg = " Value sensor: " + (int)event.values[0];
              testoLabel.setText(msg);
          }
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
          System.out.println("onAccuracyChanged - accuracy: " + accuracy);
      }
  }
  </code></pre>
  
  <p><strong>AndroidMainfest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.giuseppeaccardo.prova" &gt;
      &lt;uses-permission android:name="android.permission.BODY_SENSORS" /&gt;
  
      &lt;application
  
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme" &gt;
          &lt;activity android:name=".MainActivity" &gt;
              &lt;uses-permission android:name="android.permission.BODY_SENSORS" /&gt;
  
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>