Breathing Rate - A4100 Sample Health App PPG Sensor

Breathing Rate

For breathing rate reading we need to declare Device Specific Sensor Type. Custom Sensor Base starts with 33171000 and adding 5 will be used for Respiratory.

// Device Specific Sensor Types
private static final int qSensorTypeBase = 33171000; private static final int sensorTypeRespiratory = qSensorTypeBase + 5;<br>

The below code path is executed overall for the use case defined above for measuring the Breathing rate data and for showcasing error scenarios as above.

@Override
 
public void onAccuracyChanged(Sensor sensor, int accuracy) {
   Log.d("TAG", "Accuracy has changed to $accuracyLevel." + accuracy);
}
@Override
public void onSensorChanged(SensorEvent event) {
   switch (type) {
       case BREATHING_RATE: {
           if (event.sensor.getType() == Sensor.TYPE_LOW_LATENCY_OFFBODY_DETECT) {
               Log.d("Breathing Rate  off body", "TYPE_LOW_LATENCY_OFF_BODY_DETECT event has changed" + event.values[0]);
               if (event.values[0] == 0) {
                  // Show an error message
                  showErrorDialog(true);
               }
               break;
           }
           if (event.sensor.getType() == sensorTypeRespiratory) {
              Log.d("Breathing Rate", "Breathing Rate event has changed     to event." + event.values[1]);
            float breathingRateFloat = event.values[1];
               int values = Math.round(breathingRateFloat/4);
               if (values > 0) {
                   // if the sensor give raw value than launch the new activity with the value provided by sensor 
                   invokeActivity(values);
               }
           } else {
               showErrorDialog(false);
           }
           break;
       }
   }
}<br>

Note: To get the final value divide event.values[1] by 4 the result will provide the accurate Breathing rate

int values = Math.round(breathingRateFloat/4);

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 that the user can check the value again, the app uses shared preference

Save to Shared preference

// Store Breathing rate value
SharedPrefs.save(this, SharedPrefs.KEY_SHARED_BREATHING_RATE, value);
// Store Time in milliseconds
SharedPrefs.save(this, SharedPrefs.KEY_SHARED_BREATHING_RATE_TIME, System.currentTimeMillis());

Get From Shared preference

// Get Breathing rate value
long heartRate = SharedPrefs.getLong(this, SharedPrefs.KEY_SHARED_BREATHING_RATE);
// Get Time in milliseconds
long past = SharedPrefs.getLong(this,SharedPrefs.KEY_SHARED_BREATHING_RATE_TIME);

Breathing Rate Measured Screen with timestamp:

Next Up:

Blood Oxygen - A4100 Sample Health app PPG Sensor

Previous:

Heart Rate - A4100 Sample Health App PPG Sensor

Still need help? Contact Us Contact Us