Heart Rate - A4100 Sample Health App PPG Sensor

Heart Rate

Upon clicking the heart rate option, the user can view either of the following screens based on first-time user experience vs a returning user to this screen.

Once the user clicks the Measure button, It will open a new screen where the sensor will be registered and start the measurement activity. 

In case the user doesn’t have permission for sensor data it will ask for permission first and once the user provides the permission the sensor will be active to send data. This is one-time permission until we disable the permission.

To Enable permission:

Use this code in Oncreate() inside Activity, since this line of code executes once Activity is created,  it will first check for permission. If it is not enabled, you need to open up the Allow permission screen so the user can provide by checking on the checkmark.

protected void onCreate(Bundle savedInstanceState) {
   requestPermissions(new String[]{Manifest.permission.BODY_SENSORS}, 1);

If Permission is already provided then the user will land to:

At this point the watch should be tied to the wrist otherwise the app will throw an error.

Below error scenario will execute if: 


The below code path is executed overall for the use case defined above for measuring the data and for showcasing error scenarios. The measurement takes roughly 10-15 seconds.

public void onAccuracyChanged(Sensor sensor, int accuracy) {
   Log.d("TAG", "Accuracy has changed to $accuracyLevel." + accuracy);
public void onSensorChanged(SensorEvent event) {
   switch (type) {
       case HEART_RATE: {
           if (event.sensor.getType() == Sensor.TYPE_LOW_LATENCY_OFFBODY_DETECT) {
               Log.d("Heart Rate off body", "TYPE_LOW_LATENCY_OFF_BODY_DETECT event has changed" + event.values[0]);
               if (event.values[0] == 0) {
                  // Show an error message
           if (event.sensor.getType() == Sensor.TYPE_HEART_RATE) {
               Log.d("HeartRate", "Heart Rate event has changed to." + event.values[0]);
               float heartRateFloat = event.values[0];
               int values = Math.round(heartRateFloat);
               if (values > 0) {
        // if the sensor give raw value than launch the new activity with the value provided by sensor 
           } else {

The raw value reading from the sensor is displayed on the watch screen and is categorized based on the following standard range limits as “Very Light, Light, Moderate or Hard“.

Category  Range
Normal Resting Zone Less than 104 
Very Light  104-114
Light 114-133
Moderate 133-152
Hard 152-172

Once the user clicks the done button, it will take the user to the launch screen where they can start the measurement once again.

  • In order to save the value so we can check later or offline, the App uses shared preference

Save to Shared preference

// Store Heart rate value
SharedPrefs.save(this, SharedPrefs.KEY_SHARED_HEART_RATE, value);
// Store Time in milliseconds
SharedPrefs.save(this, SharedPrefs.KEY_SHARED_HEART_RATE_TIME, System.currentTimeMillis());

Get From Shared preference

// Get Heart rate value
long heartRate = SharedPrefs.getLong(this, SharedPrefs.KEY_SHARED_HEART_RATE);
// Get Time in milliseconds
long past = SharedPrefs.getLong(this,SharedPrefs.KEY_SHARED_HEART_RATE_TIME);
Next Up:

Breathing Rate - A4100 Sample Health App PPG Sensor


Intro - A4100 Sample Health App Using PPG Sensor

Still need help? Contact Us Contact Us