Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Pedometer API - Native Step Counter

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

  1. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Hello guys. I recently worked on a project that required granular distance tracking, so I decided to implement an API that would provide pedometer sensor data to Unity.

    The Pedometer API is a lightweight pedometer API that provides step count and distance covered (in meters) to Unity. It has a fallback to using GPS if a pedometer sensor is not available on the device. The current beta, 1.0b1, is being hosted on GitHub. It is an open source project, so feel free to contribute to it (and open issues). Happy coding!
    Code (CSharp):
    1. public class StepCounter : MonoBehaviour {
    2.  
    3.     public Text stepText;
    4.     private Pedometer pedometer;
    5.  
    6.     private void Start () {
    7.         // Create a new pedometer
    8.         pedometer = new Pedometer(OnStep);
    9.     }
    10.  
    11.     private void OnStep (int steps, double distance) {
    12.         // Display the step count
    13.         stepText.text = steps.ToString();
    14.     }
    15. }
    Pedometer on GitHub
     
    mgear likes this.
  2. ActionVillain

    ActionVillain

    Joined:
    Apr 23, 2014
    Posts:
    3
    Great! This could really save me right now ;)

    Do you think it will be possible to get pedometer updates when the app is in the background?
     
  3. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    No, because your app is suspended. But typically, the sensor accumulates step data and forwards the accumulated data to the app once the app is resumed.
     
  4. earok3d

    earok3d

    Joined:
    Jan 24, 2014
    Posts:
    7
    I simply can't get this to work at all. I've tried cloning the repository, opening it in Unity, creating an Android build (v5.6.3p1), in the example scene it remains at 0 steps and 0 feet despite walking with the Android device (Samsung Galaxy Tab S2) and shaking it repeatedly. Any ideas what I might be doing wrong? Thanks in advance.

    Edit: Whoops - I assume it's only compatible with devices that have a hardware pedometer, as opposed to simply using an accelerometer?
     
    Last edited: Jan 21, 2018
    Lanre likes this.
  5. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Pedometer isn't designed to be a software-accelerated pedometer, which is why it doesn't have a backend that uses the accelerometer.
     
  6. earok3d

    earok3d

    Joined:
    Jan 24, 2014
    Posts:
    7
    Thanks!
     
    Lanre likes this.
  7. daisyducke

    daisyducke

    Joined:
    Jun 6, 2018
    Posts:
    7
    How can you determine direction moved?
     
  8. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    This isn't possible. You can manually check the compass to estimate it.
     
  9. pringmore

    pringmore

    Joined:
    Aug 23, 2017
    Posts:
    3
    @Lanre or any memeber experienced with Pedometer Api, can you please explain if it is possible to get the Pedometer Api working using a simple smart phone or is there a need to a hardware pedometer as I am getting always a 0 output for both steps and distance counter?

    thanks
     
  10. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Your device needs a hardware pedometer for step counting to work.
     
    pringmore likes this.
  11. pringmore

    pringmore

    Joined:
    Aug 23, 2017
    Posts:
    3
    @Lanre Thanks for your answer! I am trying to make a mobile phone able to deduce the steps number from the fusion of the Gyroscope / Accelerometer sensors, can you give me orientations, please, where is located the main algorithm-logic behind the steps counter mechanism in your package?
     
  12. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    We don't perform computation based on motion data, we simply pull the step data from the hardware pedometer. Check out the code in the GitHub repository.
     
    pringmore likes this.
  13. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    Hi, I was trying to make a build on the example scene but they're giving me this error:

    Unity: AndroidJavaException: java.lang.NoSuchMethodError: no non-static method with name='isSupported' signature='()Z' in class Ljava.lang.Object;
    java.lang.NoSuchMethodError: no non-static method with name='isSupported' signature='()Z' in class Ljava.lang.Object;
    at com.unity3d.player.ReflectionHelper.getMethodID(Unknown Source)
    at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    at com.unity3d.player.UnityPlayer.c(Unknown Source)
    at com.unity3d.player.UnityPlayer$e$2.queueIdle(Unknown Source)
    at android.os.MessageQueue.next(MessageQueue.java:211)
    at android.os.Looper.loop(Looper.java:122)
    at com.unity3d.player.UnityPlayer$e.run(Unknown Source)

    when I tried to run the build on Android. Can anyone please help me with this.
     
  14. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Did you modify the sources? Try deleting and redownloading Pedometer API into your project. The method certainly exists.
     
  15. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    Nope, I didn't touch your code,

    But I used pedometer before, it was about 3 months ago, at it works on my android device.

    So I go to the version history of your project and download the older version of this API.

    Now it's working.
     
  16. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    I'll update the project to make sure that issues like this don't exist. Sorry for the inconvenience.
     
  17. pringmore

    pringmore

    Joined:
    Aug 23, 2017
    Posts:
    3
    thanx
     
  18. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    Thank you very much!
     
    Lanre likes this.
  19. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    Hey mate, I'm currently using your new release of pedometer API, and it's working fine on iPhone but on iPad, it's not working. Can you please help me with this.
     
  20. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    This is likely because the iPad does't have a hardware pedometer. You can check this like so:
    Code (CSharp):
    1. Debug.Log(Pedometer.Implementation.IsSupported);
     
  21. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    But, whenever I tried to run the app on iPhone, there's a 'fitness activity' permission needs to accept. But the fitness activity is available on the iPad. Also this App: https://itunes.apple.com/us/app/pedometer-α/id1027151285?mt=8
    Can detect steps in iPad.
    Please help me out.
     
  22. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Did you try logging `Pedometer.Implementation.IsSupported`? Was it true or false?
     
  23. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    Very sorry for the late reply
    I just checked and it's returning true.
     
  24. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    In this case, the hardware pedometer should be reporting steps. Can you try testing for longer, or testing on a different device?
     
  25. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    Yes, I send it to my friend for testing on his iPad, but the same issue, And I walked for like continuously 3 min. But still, the step count is set to zero.
     
  26. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    This could be an issue with the iPad's pedometer. You can look at Pedometer's iOS sources. There isn't anything I can do rectify this issue.
     
  27. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    I can tell you one thing, i.e. whenever I tried to run it on iPhone, it's asking for the permission of accessing motion and fitness. And in the case of iPad, It's asking for the permission of accessing location.

    By the way, Thank you very much for your reply.
     
  28. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    It sounds like you are using an older version of Pedometer. Upgrade to the latest version on GitHub.
     
  29. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    I tried but no use :(, Also I'm using unity version 2018.1.1f1. Is that okay?
     
  30. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Try logging Pedometer.Implementation.IsSupported. It should return false now.
     
  31. rahull1616dass

    rahull1616dass

    Joined:
    Oct 3, 2017
    Posts:
    24
    I did it before and it is returning true.
     
  32. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Check it again. In Pedometer 1.1, we removed the GPS backend (so the API will only use a hardware pedometer). So the check will tell you if the iPad really has a hardware pedometer.
     
  33. Sal1mus

    Sal1mus

    Joined:
    Feb 8, 2019
    Posts:
    8
    Hi! After starting I have receive an error “ "Pedometer Error: Step counting is not supported on this platform" then app is shut down. iPhone 7.

    Can somebody help?
     
  34. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    You might need to add a motion permission key to your Info.plist.
     
  35. Sal1mus

    Sal1mus

    Joined:
    Feb 8, 2019
    Posts:
    8
    Hi! Can you pls explain step by step, how you import plugin and setup it in unity to get Workable result on IPhone?
     
  36. Sal1mus

    Sal1mus

    Joined:
    Feb 8, 2019
    Posts:
    8
    NSMotionUsageDescription This one? I did. I’m already copy all folders in my project, then add a c# code in my script. Then add motion permission key.

    May be the error is due to the fact that I didn’t add pedometer.dispose()?
     
  37. Sal1mus

    Sal1mus

    Joined:
    Feb 8, 2019
    Posts:
    8
    Hi! Have some idea to resolve problem?
     
  38. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Did you modify the Pedometer sources in any way? This line is where the error is thrown, and this shouldn't happen on an iOS device because of this line. Can you share a screenshot of the Xcode window when the app crashes?
     
  39. Sal1mus

    Sal1mus

    Joined:
    Feb 8, 2019
    Posts:
    8
    My build on device sdk not on simulate sdk, on this situation can I make screenshot for you?and how?
     
  40. Sal1mus

    Sal1mus

    Joined:
    Feb 8, 2019
    Posts:
    8
    F68C80B6-51EA-4DDF-8DD4-D02A5F061596.jpeg
    Screenshot
     
  41. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Your ConvertScript.Start function has a null reference exception. This doesn't have anything to do with Pedometer.
     
  42. Sal1mus

    Sal1mus

    Joined:
    Feb 8, 2019
    Posts:
    8
    So I need to add in ConvertScript in Start method code from your Readme file?
     
  43. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Pedometer doesn't have anything to do with that script. It's yours. Check it to make sure there isn't any null reference exception you are overlooking.
     
  44. Sal1mus

    Sal1mus

    Joined:
    Feb 8, 2019
    Posts:
    8
    Ready. It’s work on IPhone.
    But API get steps only if I walking with iPhone, and not get steps data from synchronized Apple Watch
     
  45. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    Pedometer doesn't have any ability to interface with Apple Watch. You can manually add this functionality because the Pedometer sources are open.
     
  46. Tentonoc

    Tentonoc

    Joined:
    Apr 7, 2016
    Posts:
    2
    Hello, I have encountered an interesting issue with the pedometer. I have been testing it on different devices with different Android versions. It is working completely fine on 2 out of 4 devices. The working devices have Android version 5.0.1 and 9 (both Samsung) and the 2 which are not working are Android version 5.1.1 and 6.0.1 (Huawei and Asus Nexus tablet). Any idea why this might have happend?
     
  47. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    2,606
    The logs will pretty much always contain clues as to why something isn't working properly. Chances are that the devices that aren't working don't have hardware pedometers.
     
  48. Tentonoc

    Tentonoc

    Joined:
    Apr 7, 2016
    Posts:
    2
    Thank you very much for such a quick reply. I will have a better look into that hopefuly this evening or tomorrow. In case of the hardware pedometers, I am not 100% sure about the Nexus tablet, but the Huawei device definitely has pedometer as I tried the Samsung Health app and it count sthe steps correctly.
     
    Lanre likes this.
  49. spaceowlpro

    spaceowlpro

    Joined:
    Nov 8, 2017
    Posts:
    6
    Hello!! I am currently testing your pedometer api. I'm mostly interested in latency between actual steps and step counts in the api. Right now, I have to take about 5 or more steps for the counter to change, and then it changes them all at once. Is this because you have set the On Step function to check at a slower rate, or is this due to the actual speed of the physical pedometer itself? I'm just wondering if I could possibly get the latency to be even within half a second or so for my own purposes. What are your thoughts?

    Thank you!!
     
  50. spaceowlpro

    spaceowlpro

    Joined:
    Nov 8, 2017
    Posts:
    6
    Sorry, I just got into your actual pedometer code and saw your updateCount int, so I'll use that to check latency.