Android Sensor Tutorial – Pedometer

Note: we just updated the Pedometer Plugin and we added lots of new features for more information on this features and changes please follow this link

http://www.gigadrillgames.com/2017/04/16/pedometer-plugin-version-1-5-for-unity3d/

After Reading that new changes and feature you can still read this but take note this is a bit old now , but don’t inside our plugin there’s a simple demo that is super easy to follow šŸ™‚

Hi Everyone, we just released an Android Sensor Plugin for Unity3d and this time it’s about sensors and the first feature is Pedometer also known as Step Counter

for more info about Step Counter follow this linkĀ https://developer.android.com/reference/android/hardware/Sensor.html#TYPE_STEP_COUNTER

for more info about step detector follow this linkĀ https://developer.android.com/reference/android/hardware/Sensor.html#TYPE_STEP_DETECTOR

 

and now we will show you on how to easily use this plugin in no time

Quick note: this plugin will only work if your android device supports this features step detector, step counter and accelerometer without this features it will not work so read this first before purchasing the plugin.

so let’s start.

First Android Manifest Requirements

you need to have this permissions on your android manifest file, found in “Assets/Plugins/Android” directory

<uses-feature android:name="android.hardware.sensor.stepcounter" android:required="false"/>
<uses-feature android:name="android.hardware.sensor.stepdetector" android:required="false"/>

After checking that you have Android Manifest file in “Assets/Plugins/Android” directory and you have the required permissions it’s to start coding in C# scripts

First you need to have access on PedometerPlugin instance

private PedometerPlugin pedometerPlugin=PedometerPlugin.GetInstance();

Then we set Debug to 1 to see toast Debug messages or 0 to hide them

pedometerPlugin.SetDebug(0);

After that we need to initialize it

pedometerPlugin.Init();

Then after that if you want to start on Step zero always pass true on SetAlwaysStartAtZero else if you want to continue step count just pass false

pedometerPlugin.SetAlwaysStartAtZero(true);

Then we need to setup listener for event callback of pedometer

pedometerPlugin.SetCallbackListener(OnStepCount,OnStepDetect);

private void OnStepCount(int count){
  UpdateStepCount(count);
  Debug.Log( demoName + "OnStepCount count " + count);
}

//step detect event is triggered
private void OnStepDetect(){
  UpdateStepDetect("Detect!");
  Debug.Log( demoName + "OnStepDetect");
}

And Then we start pedometer from listening on step sensor

pedometerPlugin.RegisterSensorListener();

if you want to reset step call ResetTotalStep

public void ResetTotalStep(){
  //reset total step to zero
  if(pedometerPlugin!=null){
   pedometerPlugin.ResetTotalStep();
  }
}

whenĀ  you are done using pedometer plugin call RemoveSensorListener method

if(pedometerPlugin!=null){
   pedometerPlugin.RemoveSensorListener();
}

To See all working codes here’s the exact code inside Pedometer Demo

Note: this code needs Text , just create 2 Text UI inside Uniy3d and drag it on this code Inspector.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PedometerDemo : MonoBehaviour {
    
    private PedometerPlugin pedometerPlugin;
    private string demoName="[PedometerDemo] ";

    public Text stepCountText;
    public Text stepDetectText;
    
    // Use this for initialization
    void Start (){
        //get the instance of pedometer plugin
        pedometerPlugin = PedometerPlugin.GetInstance();

        //set to zero to hide debug toast messages
        pedometerPlugin.SetDebug(0);

        //initialze pedometer
        pedometerPlugin.Init();

        //set this to true to always starts at zero steps, else set to false to continue steps
        pedometerPlugin.SetAlwaysStartAtZero(true);

        //set call back listener for pedometer events
        pedometerPlugin.SetCallbackListener(OnStepCount,OnStepDetect);

        //register sensor event listener
        pedometerPlugin.RegisterSensorListener();
    }

    public void ResetTotalStep(){
        //reset total step to zero
        if(pedometerPlugin!=null){
            pedometerPlugin.ResetTotalStep();
            UpdateStepCount(0);
            Debug.Log( demoName + "ResetTotalStep ");
        }
    }    
                
    private void OnApplicationPause(bool val){
        if(val){
            //game is pause
            //remove sensor event listener
            if(pedometerPlugin!=null){
                pedometerPlugin.RemoveSensorListener();
            }
        }else{
            //game is resume
            //register sensor event listener
            if(pedometerPlugin!=null){
                pedometerPlugin.RegisterSensorListener();
            }
        }
    }

    //step count event is triggered
    private void OnStepCount(int count){
        UpdateStepCount(count);
        Debug.Log( demoName + "OnStepCount count " + count);
    }

    //step detect event is triggered
    private void OnStepDetect(){
        UpdateStepDetect("Detect!");
        Debug.Log( demoName + "OnStepDetect");
    }

    //for updating step count for demo purpose
    private void UpdateStepCount(int count){
        if(stepCountText!=null){
            stepCountText.text = string.Format("Step Count: {0}",count);
        }
    }

    //for showing step is detected for demo purpose
    private void UpdateStepDetect(string val){
        if(stepDetectText!=null){
            stepDetectText.text = string.Format("Step Detect: {0}",val);
        }
    }
}

Hooray we are done , that’s it i hope i should be clear now how to use it.

For Questions or comments or suggestions or any feed back just email us at gigadrillgames@gmail.com

2 thoughts on “Android Sensor Tutorial – Pedometer

  • March 11, 2016 at 1:50 am
    Permalink

    HI Warfreakz, i try to make your code work but a get this error :
    The type or namespace name `PedometerPlugin’ could not be found. Are you missing a using directive or an assembly reference?

    I try to create an AndroidManifest.xml under Assets/Plugins/Android with :

    But i’m not sure what i’am doing.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.