Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Pedometer API - Native Step Counter

Discussion in 'Assets and Asset Store' started by Lanre, Jul 14, 2017.

  1. spaceowlpro

    spaceowlpro

    Joined:
    Nov 8, 2017
    Posts:
    12
    So it seems my pedometer only updates the step count after 5 or 6 steps, but it kinda fluctuates. What does everyone else here get and on what kind of phone?
     
  2. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    The native pedometer determines this. On some devices, the pedometer reports steps as they come in (in realtime); on other devices, the pedometer batches steps and sends updates with the aggregate count of steps. Pedometer API itself doesn't have any latency-inducing operations.
     
  3. spaceowlpro

    spaceowlpro

    Joined:
    Nov 8, 2017
    Posts:
    12
    Okay, thanks! Maybe I'll figure out how to use the accelerometer to pick up device shaking that happens when someone takes a step instead.
     
  4. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    This isn't an easy task, but you can try it.
     
  5. spaceowlpro

    spaceowlpro

    Joined:
    Nov 8, 2017
    Posts:
    12
    Right? If I get anything going, I'll say something here. If you don't hear from me in a week... you'll know. :p
     
    Lanre likes this.
  6. avcjeewantha

    avcjeewantha

    Joined:
    Feb 19, 2019
    Posts:
    1
    Hi, I have the same problem. Do you know how to do this?.
     
  7. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9
    upload_2019-5-14_14-30-17.png

    i need use this api , but send this error
    i want build in android apk
     
  8. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Find libPedometer.a in your project, and in the inspector, make sure that it only applies to iOS.
     
  9. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9
    download the api, do not modify its code, I only want to use it for android but it throws me the previous error, what can it be?
     
  10. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Do what I described above. Unity thinks it should build the iOS library for Android which is incorrect.
     
  11. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9
    I managed to generate the apk, but it does not tell me the steps in my cell phone, I tried with other api and if it works, I have a samsung j5 pro
     
  12. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Check the logs for clues.
     
  13. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9
    I've this ,the cell phone does not count the steps
    upload_2019-5-15_0-28-30.png
     
    Last edited: May 15, 2019
  14. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    I mean the logs from adb logcat.
     
  15. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9
    i have this
    need to use this api
    I feel lost and it does not work
    help me pls
    upload_2019-5-16_0-56-36.png
     
  16. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I noticed a slight error on the sample code on GitHub. This line:
    Code (CSharp):
    1. distanceText.text = (distance * 3.28084).ToString("F2") + " ft";
    should be this:
    Code (CSharp):
    1. distanceText.text = (distance / 3.28084).ToString("F2") + " ft";
    Multiplying is saying that feet are bigger than meters, when in fact feet are smaller than meters.
     
  17. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
  18. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Not quite.
     
  19. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9
  20. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    It doesn't look like a permissions issue. Other apps might fall back to different methods to detect steps, like GPS.
     
  21. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9

    Do you have any version with gps?
    I need this urgently
    I'd appreciate it a lot
     
  22. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9
    upload_2019-5-16_13-37-32.png
    change the line of code and the above mentioned error does not appear
    but do not count the steps
     
  23. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    The old 1.0 version falls back to GPS.
     
  24. locosagpro

    locosagpro

    Joined:
    May 14, 2019
    Posts:
    9
    It has many errors :(
     
  25. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    I don't maintain it anymore. You can create a custom GPS implementation based on the code in that branch.
     
  26. ArmanUnity

    ArmanUnity

    Joined:
    Nov 29, 2015
    Posts:
    22
    Do we have a function/code to check whether user has denied permission for motion usage???? @Lanre
     
  27. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    There currently isn't a function for this. You can create an issue for this on GitHub.
     
  28. Shadoka

    Shadoka

    Joined:
    Jun 6, 2017
    Posts:
    4
    So any movement represents a step? I mean, if I try move the phone up and down it will count as steps?
     
  29. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    This highly depends on the device's hardware sensor, but most sensors are smart enough to only detect footsteps.
     
  30. Shadoka

    Shadoka

    Joined:
    Jun 6, 2017
    Posts:
    4
    I see, really cool API you have here. Thanks for the contribution.
     
    Lanre likes this.
  31. Ankit_JeGames

    Ankit_JeGames

    Joined:
    May 28, 2019
    Posts:
    11
    Thanks for sharing this it is so helpful for my vurrent project
     
    Lanre likes this.
  32. w1jvq

    w1jvq

    Joined:
    Feb 16, 2020
    Posts:
    2
    Hello,
    I am still learning Unity and this is my first attempt at an iOS application. How do I use your content on GitHub? Do I need the Build iOS Source Project?
     
  33. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    See the README.
     
  34. PartrickCraig

    PartrickCraig

    Joined:
    Nov 1, 2019
    Posts:
    1
    Hello, I now have this working on an Android Galaxy S6. Is there any way to have steps recorded in the background and have the step counter update with total steps when the application is open?
     
  35. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    If the app gets suspended for a short time, the operating system usually accumulate steps and reports them to the app when it is resumed. If the app is suspended for a long time, or if the app is closed, then this is not possible.
     
  36. vandel212

    vandel212

    Joined:
    May 10, 2020
    Posts:
    24
    Sorry, I'm kind of new to Unity Development. I'm trying to get this to work, but I have no idea how to reference the API in my project. Can someone explain it, or possibly direct me to a tutorial that explains how I'd get an API like this to work in a Unity project? The readme seems to only explain on how to utilize the API once it's installed in the project. Thank you!
     
  37. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Import the unitypackage into your project then start writing code.
     
  38. SteenPetersen

    SteenPetersen

    Joined:
    Mar 13, 2016
    Posts:
    103
    Hi, thanks for the contribution, I'm wondering if anyone has an idea on how one would use this API to determine the amount of steps for a given day? Since I only get the steps once the app opens again, I cannot know how many of those steps where from which day.
     
  39. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    I don't think this is exposed by the native API's. It would require the pedometer sensor running constantly.
     
  40. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    can I save the counted steps in a playerPref and load this on Start or by awakening the app?

    Like:
    Code (CSharp):
    1. /*
    2. *   Pedometer
    3. *   Copyright (c) 2018 Yusuf Olokoba
    4. */
    5.  
    6. namespace PedometerU.Tests {
    7.  
    8.     using UnityEngine;
    9.     using UnityEngine.UI;
    10.  
    11.     public class StepCounter : MonoBehaviour {
    12.  
    13.         public Text stepText, distanceText;
    14.         private Pedometer pedometer;
    15.         public int stepsave;
    16.         public int steps;
    17.  
    18.         private void Awake()
    19.         {
    20.             stepsave = PlayerPrefs.GetInt("25km", 0);
    21.         }
    22.         private void Start () {
    23.          
    24.             // Create a new pedometer
    25.             pedometer = new Pedometer(OnStep);
    26.             // Reset UI
    27.             OnStep(stepsave, 0);
    28.         }
    29.  
    30.         void OnStep (int steps, double distance) {
    31.             // Display the values // Distance in feet
    32.             stepText.text = steps.ToString();
    33.             //distanceText.text = (distance * 3.28084).ToString("F2") + " ft";
    34.          
    35.             distanceText.text = (distance * 0.65).ToString("F2") + " m";
    36.             PlayerPrefs.SetInt("25km", steps); PlayerPrefs.Save();
    37.         }
    38.  
    39.         private void OnDisable () {
    40.             // Wert speichern
    41.             PlayerPrefs.SetInt("25km", steps); PlayerPrefs.Save();
    42.             // Release the pedometer
    43.             pedometer.Dispose();
    44.             pedometer = null;
    45.                    
    46.         }
    47.         private void OnApplicationQuit()
    48.         {
    49.             PlayerPrefs.SetInt("25km", steps); PlayerPrefs.Save();
    50.         }
    51.     }
    52. }
     
  41. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    This should work with no issues.
     
  42. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    Hy Lanre or all the other
    I completely confused.. I not sure how and where I can add the past steps with the actualy steps.
    First, I check my playerPrefs of is something saved:
    Code (CSharp):
    1. public void Awake()
    2.         {
    3.             stepsave = PlayerPrefs.GetInt("25km", 0);
    4. }
    There is no probelm.
    Then on void Start:
    Code (CSharp):
    1.         private void Start () {
    2.             stepkalc = ((int)(stepsave * 0.65));
    3.             distanceText.text = stepkalc.ToString("F2") + " m";
    4.          
    5.             // Create a new pedometer
    6.             pedometer = new Pedometer(OnStep);
    7.             // Reset UI
    8.             OnStep(0, 0);
    9.         }
    I calculate playerprefs save value with my step distance and then I make it visible in an UI.Text field.
    !!QUESTION!!
    when I call OnStep (0,0), its defined as OneStep(steps,distance), its reset the count of steps?
    Here I add the actualy steps with the value of my PlayerPrefs:
    Code (CSharp):
    1.  public void OnStep (int steps, double distance) {
    2.             // Display the values // Distance in feet
    3.             stepadd = steps + stepsave;
    4.             stepText.text = steps.ToString();
    5.             //berechnet
    6.             distanceText.text = (stepadd * 0.65).ToString("F2") + " m";
    To see the change, I have added update():

    Code (CSharp):
    1.  public void Update()
    2.         {
    3.             stepsave = PlayerPrefs.GetInt("25km");
    4.             gespeichertText.text = "Stepsave: " + stepsave.ToString() +"\n\r Stepadd: " + stepadd.ToString();
    5.             //PlayerPrefs.SetInt("25km", stepadd); PlayerPrefs.Save();
    6.         }
    The normal way, the pedometer was reset OnDisable():
    Code (CSharp):
    1.      private void OnDisable () {
    2.             // Save PlayerPrefs
    3.            PlayerPrefs.SetInt("25km", stepadd); PlayerPrefs.Save();
    4.             // Release the pedometer
    5.             pedometer.Dispose();
    6.             pedometer = null;
    7.                        
    8.         }
    I try to save the stepadd (actually steps added with the saved value on awake)
    BUT, it will not work. it count sometimes the value double or not. I dont know how it works, when my Android phone are OnApplicationFocus or OnApplicationPause or OnDisable..So I save the stepadd by Quit():
    Code (CSharp):
    1.    public void OnApplicationQuit()
    2.         {
    3.             PlayerPrefs.SetInt("25km", stepadd); PlayerPrefs.Save();
    4.             pedometer.Dispose();
    5.             pedometer = null;
    6.         }
    I only will to be sure, that the steps are counting when the app is runing and the screen blocked. I want, allways the past steps that I gone, save and actualy added with my going steps now..

    Please, can anybody help me to solve my problem? Thanks and sry for my bad english..
     
  43. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    I'm not entirely sure what your question is. But something to note is that the pedometer is only active when the app is running (whether in the foreground or background). If the app closes, the pedometer won't give you any steps (and your code will not run).

    If you are asking why it is displaying 0 steps when you call OnStep manually, it is because you are displaying `step` instead of `stepadd`.
     
  44. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    ..sure..because step is the actually value and stepadd the PlayerPrefs. When the app wil quit, I want to add this both value and save it to PlayerPrefs..

    My question is: when and how I save the actually steps as PlayerPrefs, so that the value in PlayerPrefs are the total of all steps...When I save the value of steps ? In update(), OneStep(), sometimes on other function?

    And whas happen, when I use the PlayerPrefs value to OneStep like: OnStep(PlayerPref,0)?
     
  45. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    You should be able to save to player prefs when the app is closing, like in OnApplicationQuit.
     
  46. freddyready

    freddyready

    Joined:
    Nov 2, 2019
    Posts:
    16
    @Lanre Can you help me with this?
    i had it configured and it is returning true as the compatibility.
    but still there is no Counter increment.
    in the Logs, there is one error i think might be the reason "SensorWakeUp:false"
     
  47. freddyready

    freddyready

    Joined:
    Nov 2, 2019
    Posts:
    16
    09-01 14:57:56.447: I/Unity(3787): (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
    09-01 14:57:56.450: E/SensorManager(3787): registerListenerImpl sensorName:pedometer Non-wakeup,isWakeUpSensor:false,callingApp: com.yusufolokoba.pedometer10,callingPid:3787,callingUid:10539
    09-01 14:57:56.451: D/Unity(3787): Pedometer: Initialized Android backend
     
  48. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Can you share the full logs in a .txt attachment? If the device reports that the pedometer is supported, then the problem must be coming from the sensor itself.
     
  49. freddyready

    freddyready

    Joined:
    Nov 2, 2019
    Posts:
    16

    Attached Files:

    • log.txt
      File size:
      7.4 KB
      Views:
      281
  50. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Not sure what's going on, but there is a log that states "Unable to find Pedometer". Beyond that, there is an error raised when trying to register for pedometer events. There is no explanation given, so without it I have no clue why it isn't working, given that the device apparently has the sensor.
     
    freddyready likes this.