0

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.

I've tried it, but it doesn't run.

My activity

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

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.giuseppeaccardo.prova" >
    <uses-permission android:name="android.permission.BODY_SENSORS" />

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <uses-permission android:name="android.permission.BODY_SENSORS" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3 Answers3

1

You could try changing this:

sMgr.registerListener(this, battito,SensorManager.SENSOR_DELAY_NORMAL);

to this:

 sMgr.registerListener(this, battito,SensorManager.SENSOR_DELAY_FASTEST);

This increases the time it reads your finger.

The last thing you need to do to make sure you have allowed the permission correctly. I see you have allowed it in the manifest but you also need to allow it at run time. See this link for help.

Otherwise as previously suggested, please add some more detail about your issue

Oli Wood
  • 46
  • 2
0

I think the problem in your case is with the permission for body sensors. I would recommend you to check if the body sensors permission is granted by the user and then initialize the heart rate sensor, if yes continue else ask the user to grant the permission. This applies even if you have added uses body sensors permission in your manifest.

if (checkSelfPermission(Manifest.permission.BODY_SENSORS)
            != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.BODY_SENSORS},
                PERMISSIONS_REQUEST_BODY_SENSORS);
} else {
        //Your code for declaring and initializing Sensor manager and Heartrate sensor
};

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                       int[] grantResults) {
    if (requestCode == PERMISSIONS_REQUEST_BODY_SENSORS
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
         //Your code for declaring and initializing Sensor manager and Heartrate sensor
    }
}
Abhishek Dhotre
  • 481
  • 4
  • 17
-1

You can't since Samsung doesnt allow it if you are not using the SDK. If you would like to use their SDK you have to request a permission. More informations at http://developer.samsung.com/galaxy#sensor-extension.

You will need the Samsung Health SDK for accessing those sensors.

Im not sure if you can access it, but this is the official documentation to use the proper Sensor. http://developer.samsung.com/health/data/guide

Emanuel
  • 8,027
  • 2
  • 37
  • 56