Search Unity

Pedometer API - Native Step Counter

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

  1. freddyready

    freddyready

    Joined:
    Nov 2, 2019
    Posts:
    16
    i had the boolean printed and it says "True".
    Do you need anything else to help me figure it out??
     
  2. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Nothing else comes to mind. I don't believe there is anything you can do on the current device you are testing. Have you tried testing on a different device?
     
  3. freddyready

    freddyready

    Joined:
    Nov 2, 2019
    Posts:
    16
    Yes. THis is oneplus 7 pro, I have tested on samsung and huawei too. No progress. same issue
     
  4. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    Something strange:
    a build with target SDK 28 will work of any devices.
    a build with target SDK 29 will NOT work of devices..
    The problem is, google allowed new apps ONLY with SDK 29.. :-(

    Tested an work with: Wiko, Honor 7, Samsung Tab A20, Huawai P20,Samsung, HTC U11, Nokia 7.2
    Any ideas why?
     
  5. freddyready

    freddyready

    Joined:
    Nov 2, 2019
    Posts:
    16
    i don't think this change will effect eh apps before 2nd november.
     
  6. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    If the pedometer doesn't work on Samsung and Huawei devices, can you send the logs from those devices also? Any issues common to them could be a clue in solving whatever that issue might be.
     
  7. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Do you mind creating an issue for this on GitHub? It seems that Android 29+ requires a new permission.
     
  8. freddyready

    freddyready

    Joined:
    Nov 2, 2019
    Posts:
    16
    i made a build with target 28 and it worked on all
     
  9. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    of course: yes. It will not published in the store. I check the differnet between 28 &29:
    https://developer.android.com/sdk/api_diff/29/changes
    But I dreally don't know the point of relevant packages change that deaktivate or block the pedometer ...
     
  10. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    I see, but how I can use this in your Code? Or do you change it on GitHub ?
    And yes I use the GitHub package :)

    I see on GitHub the ..\Pedometer-API-1.1-builds.zip\Pedometer-API-1.1-builds\Android\Pedometer\src\main\java\com\yusufolokoba\pedometer\Pedometer.java:
    Code (JavaScript):
    1. public class Pedometer implements SensorEventListener {
    2.  
    3.     private SensorManager manager;
    4.     private final PedometerDelegate delegate;
    5.  
    6.     //region --Client API--
    7.  
    8.     public Pedometer (PedometerDelegate delegate) {
    9.         this.delegate = delegate;
    10.         this.manager = (SensorManager) UnityPlayer.currentActivity.getSystemService(Context.SENSOR_SERVICE);
    11.     }
    12.  
    13.     public void initialize () {
    14.         final Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
    15.         if (sensor == null) {
    16.             Log.e("Unity", "Pedometer Error: Failed to acquire step counter sensor");
    17.             return;
    18.         }
    19.         manager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
    20.         Log.d("Unity", "Pedometer: Initialized Android backend");
    21.     }
    22.  
    23.     public void release () {
    24.         manager.unregisterListener(this);
    25.         Log.d("Unity", "Pedometer: Released Android backend");
    26.     }
    27.  
    28.     public boolean isSupported () {
    29.         return UnityPlayer.currentActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_STEP_COUNTER);
    30.     }
    31.     //endregion
     
    Last edited: Sep 3, 2020
  11. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I haven't read into this, but I believe it requires adding a permission to the manifest.
     
  12. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    Yes. I see and I added this to my manifest:
    <meta-data android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" android:value="true" />

    because:

    If your app targets Android 9 (API level 28) or lower, the system auto-grants the android.permission.ACTIVITY_RECOGNITION permission to your app, as needed, if your app satisfies each of the following conditions:

    • The manifest file includes the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission.
    • The manifest file doesn't include the android.permission.ACTIVITY_RECOGNITION permission.
    or is this correct:

    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/> ??

    I dont realy understud, an now I read some manifest examples (android & unity) :)
     
  13. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    I found this:
    https://developer.android.com/guide/topics/location/transitions

    To use the Transition API in your app, you must declare a dependency to the Google Location and Activity Recognition API version 12.0.0 or higher and specify the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission in the app manifest.

    1. To declare a dependency to the API, add a reference to the Google maven repository and add an implementation entry to com.google.android.gms:play-services-location:12.0.0 to the dependencies section of your app build.gradle file. For more information, see Set Up Google Play Services.

    2. To specify the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission, add a <uses-permission> element in the app manifest, as shown in the following example:
    I NEED google play services? Realy? I'm completly confused and I^m not sure to implement the google play service.
     
  14. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I haven't looked into this yet, so I don't know. Google really doesn't seem to think through their changes sometimes, because this is all so confusing for something that should be very simple (compare how simple iOS is).
     
  15. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    Do have any idea for the solution? See my message to you..:)
     
  16. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    So it will works:

    in the android manifest:
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
    3. <manifest
    4.     xmlns:android="http://schemas.android.com/apk/res/android"
    5.     package="com.unity3d.player"
    6.     xmlns:tools="http://schemas.android.com/tools">
    7.  
    8.  
    9. [B]  <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" android:protectionLevel="dangerous" />[/B]
    10.  
    11.   <!-- <uses-feature android:name="android.hardware.sensor.accelerometer" /> -->
    12.  
    13.   <application>
    14.  
    Fo the request Window:
    I use these FREE Asset Plugin:
    https://assetstore.unity.com/packages/tools/integration/android-runtime-permissions-117803

    then in my Awake Funtion in the APP:
    Code (CSharp):
    1.     private void Awake()
    2.     {
    3.         AndroidRuntimePermissions.Permission result = AndroidRuntimePermissions.RequestPermission("android.permission.ACTIVITY_RECOGNITION");
    4.         if (result == AndroidRuntimePermissions.Permission.Granted) {
    5.             Debug.Log("We have permission to access the stepcounter");
    6.         }
    7.         else {
    8.             Debug.Log("Permission state: " + result); // No permission
    9. Application.Quit();
    10. }
    And it works fine with Android 10 SDK 29
     
    Lanre likes this.
  17. Sashimiii

    Sashimiii

    Joined:
    Sep 26, 2017
    Posts:
    15
    Hi Lanre,

    I have two questions:
    1. Is it possible for this API to get the updated pedometer steps after closing and re-opening the app?
    2. It does seem to track the steps properly if the app is in the background. However, I'm not sure whether the steps are still properly being updated in this scenario:
    • The user leaves the app but keeps it in the background.
    • After a few hours, the user decides to re-open the app.
    Do you have more information on this?
     
  18. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    No, if the app is closed, you can't get any updates.
    I'm not sure how long it tracks steps if the app is in the background, but for short periods of time you will get the steps when the app resumes. I believe it is up to the OS to decide how long to keep the pedometer sensor active when the app is suspended.
     
    Sashimiii likes this.
  19. Utarastas

    Utarastas

    Joined:
    Jul 27, 2017
    Posts:
    11
    Im running Unity 2019.4.24f, with Android target of API 29. Initially, the pedometer API didnt work, it would simply not start as long as I was building for API level 29, I tested it with level 28, and it worked pretty well. Then changed the AndroidManifest (you need to enable custom android manifest in Player Settings/Publishing settings first) to this:

    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <manifest
    3.     xmlns:android="http://schemas.android.com/apk/res/android"
    4.     package="com.unity3d.player"
    5.     xmlns:tools="http://schemas.android.com/tools">
    6.     <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
    7.     <uses-feature android:name="android.hardware.sensor.accelerometer" />
    8.     <application>
    9.         <activity android:name="com.unity3d.player.UnityPlayerActivity"
    10.                   android:theme="@style/UnityThemeSelector">
    11.             <intent-filter>
    12.                 <action android:name="android.intent.action.MAIN" />
    13.                 <category android:name="android.intent.category.LAUNCHER" />
    14.             </intent-filter>
    15.             <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    16.         </activity>
    17.     </application>
    18. </manifest>
    In Unity, I would ask for the permission the same way you did:
    Code (CSharp):
    1.  
    2.  private void Awake()
    3.          {
    4.              AndroidRuntimePermissions.Permission result = AndroidRuntimePermissions.RequestPermission("android.permission.ACTIVITY_RECOGNITION");
    5.              if (result == AndroidRuntimePermissions.Permission.Granted)
    6.              {
    7.                  Debug.Log("We have permission to access the stepcounter");
    8.                 Debug.LogError("PERMISSION GRANTED");
    9.              }
    10.              else
    11.              {
    12.                
    13.                  Debug.LogError("NO PERMISSION");
    14.          
    15.                  Debug.Log("Permission state: " + result); // No permission
    16.                
    17.              }
    18.          }
    19.  
    At the time I was building using Mono scripting backend, the build would be successful, but it still wouldnt ask for permission. Then I changed scripting backend to IL2CPP, but then the build would fail with an IL2CPP error. I solved the error by deleting libPedometer.a (iOS library for this API, I imagine). Finally the build was successful, and the application asked for permission to use Activity data, and the pedometer would start tracking steps. Hopefully someone can find this useful
     
  20. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I've added the `ACTIVITY_RECOGNITION` permission to the manifest. Thanks for pointing this out.
    Ah, for this select libPedometer.a in the inspector and unselect all platforms except iOS.

    Thank you so much for this information. I'm working on pushing out an update now.
     
    muhammad_ali_safdar likes this.
  21. Utarastas

    Utarastas

    Joined:
    Jul 27, 2017
    Posts:
    11
    Thank you for taking the time to update this API, it's pretty much the only proper solution I found for step counting currently. I couldn't get Unity's native step counter to work at all. Regarding iOS library giving me some issues, I did exactly what you proposed here.
     
    Lanre likes this.
  22. kesada7

    kesada7

    Joined:
    Jun 21, 2017
    Posts:
    14
    Did you push the update to github? I tried to build an apk but it does not count any step.
     
    equiperadarfit and Lanre like this.
  23. uzabiart

    uzabiart

    Joined:
    May 18, 2018
    Posts:
    1
    Hello, I have question about working in the background. I noticed the app stops counting steps after 10~ minutes. I suppose android is turning off this background activity? In foreground and in less then 10 minutes in the background the app works perfect, but after 10 minutes in background it stops.
    If anyone else noticed this?
     
  24. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I haven't noticed this before, but it might be a power-saving measure by the OS.
     
  25. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    183
    @Lanre can you please update Github with the changes. Thanks.
     
    Lanre likes this.
  26. Balder92

    Balder92

    Joined:
    Jan 25, 2014
    Posts:
    2
    Hi @Lanre First of all thank you for this API and for making it open source really appreciate that!

    I have taken your example project and built it for android successfully, however the count is stuck at 0,0.
    I Tried logging
    Code (CSharp):
    1. Pedometer.Implementation.IsSupported
    and it returns as True. Tried this on 3 different devices, all with built in pedometers and returning true on the debug.

    Any ideas where I should look to solve this?
     
    Last edited: Jun 8, 2022
  27. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hey there, sorry for the late response on this. I didn't get an email notification. I will update the package and have you re-test it. In the mean time, I recommend joining our community on Discord to track this. We're way more active on Discord.