Blood Oxygen - A4100 Sample Health app PPG Sensor

Blood Oxygen

For Blood oxygen reading we need to declare Device-Specific Sensor Type. Device-Specific Sensor Base starts with 33171000 and adding 6 will be used for the SPO2 sensor.

// Device-Specfic Sensor Types
private static final int qSensorTypeBase = 33171000; 
private static final int sensorTypeSpo2 = qSensorTypeBase + 6;

The below code path is executed overall for the use case defined above for measuring the blood oxygen 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 BLOOD_OXYGEN: {
           if (event.sensor.getType() == Sensor.TYPE_LOW_LATENCY_OFFBODY_DETECT) {
               Log.d("Blood oxygen 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() == sensorTypeSpo2) {
              Log.d("Blood Oxygen", "Blood Oxygen event has changed     to event." + event.values[1]);
            float bloodOxygenFloat = event.values[1];
               int values = Math.round(bloodOxygenFloat);
               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>

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_BLOOD_OXYGEN, value);
// Store Time in milliseconds
SharedPrefs.save(this, SharedPrefs.KEY_SHARED_BLOOD_OXYGEN_TIME, System.currentTimeMillis());

Get From Shared preference

// Get Blood oxygen value
long heartRate = SharedPrefs.getLong(this, SharedPrefs.KEY_SHARED_BLOOD_OXYGEN);
// Get Time in milliseconds
long past = SharedPrefs.getLong(this,SharedPrefs.KEY_SHARED_BLOOD_OXYGEN_TIME);

Blood Oxygen Measured Screen with timestamp:

Please Note: This app performs local caching for the last measurement reading.

Next Up:

Source Code - A4100 Sample Health App PPG Sensor

Previous:

Breathing Rate - A4100 Sample Health App PPG Sensor

Still need help? Contact Us Contact Us