Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 470 Mobile App Development

Similar presentations


Presentation on theme: "CIS 470 Mobile App Development"— Presentation transcript:

1 CIS 470 Mobile App Development
Lecture 24 Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University 11/17/2018 CIS 470: Mobile App Development

2 Step Counting (Pedometer)
Taken from Android Sensor Programming By Example, Packt Publishing, 2016 Step detector and step counter sensors A custom algorithm using the accelerometer sensor’s data to detect walking, jogging, and fast running activities 11/17/2018 CIS 470: Mobile App Development

3 CIS 470: Mobile App Development
Step Counter Sensor The step counter sensor is used to get the total number of steps taken by the user since the last reboot of the phone The value of the step counter sensor is reset to zero on reboot In the onSensorChanged() method, the number of steps is given by event.value[0] The event timestamp represents the time at which the last step was taken This sensor is especially useful for those applications that don't want to run in the background and maintain the history of steps themselves This sensor works in the batch and continuous modes. If we specify 0 or no latency in the SensorManager.registerListener() method, then it works in continuous mode, otherwise, if we specify any latency, then it groups the events in batches and reports them at a specified latency 11/17/2018 CIS 470: Mobile App Development

4 CIS 470: Mobile App Development
Step Counter APIs protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.stepcounter_layout); mStepsSinceReboot = (TextView)findViewById(R.id.stepssincereboot); mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); if(mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) != null) { mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); isSensorPresent = true; } else { isSensorPresent = false; } } public void onSensorChanged(SensorEvent event) { mStepsSinceReboot.setText("Steps since reboot:" + String.valueOf(event.values[0])); } 11/17/2018 CIS 470: Mobile App Development

5 CIS 470: Mobile App Development
Step Detector Sensor The step detector sensor triggers an event each time a step is taken by the user The value reported in the onSensorChanged() method is always one, the fractional part is always zero, and the event timestamp is the moment that the user's foot hits the ground The step detector sensor has lower accuracy and produces more false positives compared to the step counter sensor The step counter sensor is more accurate, but it has more latency in reporting the steps, as it uses extra time after each step to remove any false positive values The step detector sensor is recommended for those applications that want to track the steps in real time and maintain their own history of each and every step with their timestamp 11/17/2018 CIS 470: Mobile App Development

6 CIS 470: Mobile App Development
Step Detector APIs public void onCreate() { super.onCreate(); mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE); if(mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR) != null) { mStepDetectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR); mSensorManager.registerListener(this, mStepDetectorSensor, SensorManager.SENSOR_DELAY_NORMAL); mStepsDBHelper = new StepsDBHelper(getApplicationContext()); } } public void onSensorChanged(SensorEvent event) { mStepsDBHelper.createStepsEntry(); } onSensorChanged() is called for every step 11/17/2018 CIS 470: Mobile App Development

7 Custom Step Counting and Activity Detection
Step counting: count number of peaks/valleys Types of activities: highest peak values private static final int WALKINGPEAK = 18; private static final int JOGGINGPEAK = 25; private static final int RUNNINGPEAK = 32; Green circle: true peak Red circle: valley Yellow circle: false peak (to be filtered out) 11/17/2018 CIS 470: Mobile App Development

8 CIS 470: Mobile App Development
Detect Peaks public void findHighPeaks() { //Calculating the High Peaks boolean isAboveMeanLastValueTrue = false; int dataSize = mRawDataList.size(); for (int i = 0; i < dataSize; i++) { if(mRawDataList.get(i).value > WALKINGPEAK) { // add all continuous data points that are bigger than WALKINGPEAK to the array // One of these points include both the true peak and the false peaks, and many other points that are not peaks mAboveThresholdValuesList.add(mRawDataList.get(i)); isAboveMeanLastValueTrue = false; } else { // Once we see a point that is below the threshold, we are falling out of the peak region // Now time to find the peaks if(!isAboveMeanLastValueTrue && mAboveThresholdValuesList.size()>0) { // Sort the temporary data array. The last one in the array has the biggest value, which is the peak // which could be true or false peak Collections.sort(mAboveThresholdValuesList,new DataSorter()); mHighestPeakList.add(mAboveThresholdValuesList.get(mAboveThresholdValuesList.size()-1)); mAboveThresholdValuesList.clear(); } isAboveMeanLastValueTrue = true; } } } 11/17/2018 CIS 470: Mobile App Development

9 CIS 470: Mobile App Development
public void removeClosePeaks() { for (int i = 0; i < mHighestPeakList.size()-1; i++) { if(mHighestPeakList.get(i).isRealPeak) { if(mHighestPeakList.get(i+1).time - mHighestPeakList.get(i).time < 400) { if(mHighestPeakList.get(i+1).value > mHighestPeakList.get(i).value) { mHighestPeakList.get(i).isRealPeak = false; } else { mHighestPeakList.get(i+1).isRealPeak = false; } } } } } Observation: false peaks are very close to the true peak. Use this fact to filter them out 11/17/2018 CIS 470: Mobile App Development

10 CIS 470: Mobile App Development
The Pedometer app Create an app and name it MyPedometer Replace existing manifests, activity_main.xml, styles.xml, strings.xml, MainActivity.java with the source code provided in the course webpage Add the following java files: AccelerometerData.java, CustomAlgoResultsActivity.java, DateStepsModel.java, ListAdapter.java, StepsCounterActivity.java, StepsDBHelper.java, StepsHistoryActivity.java, StepsService.java, StepsTrackerDBHelper.java, StepsTrackerService.java Add the following xml files: capability_layout.java, list_rows.xml, pedomesterlist_layout.xml, stepcounter_layout.xml, list_raw_divider_gradient.xml 11/17/2018 CIS 470: Mobile App Development

11 CIS 470: Mobile App Development
The Pedometer app 11/17/2018 CIS 470: Mobile App Development

12 CIS 470: Mobile App Development
Exercise If you compare the step count using the API and that from the custom algorithm, you will see they are different depending on where you place the phone Make the three parameters for activity detection for walking, jogging, and running (18, 25, 32) customizable in the app itself dynamically, e.g., by adding three TextEdits and a button, so that the result from the custom algorithm is similar to result from the APIs 11/17/2018 CIS 470: Mobile App Development


Download ppt "CIS 470 Mobile App Development"

Similar presentations


Ads by Google