Search Unity

Question Input System - Unresponsive Android Accelerometer and StepCounter

Discussion in 'Input System' started by TheBen67, Dec 28, 2022.

  1. TheBen67

    TheBen67

    Joined:
    Dec 28, 2022
    Posts:
    3
    I've been stumped on implimenting the Accelerometer and StepCounter devices on my Android Phone and Tablet to my project.

    Here's my code:
    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3. using UnityEngine.Android;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class WalkScript : MonoBehaviour
    7. {
    8.  
    9.     [SerializeField]
    10.     public int count = 0;
    11.     [SerializeField]
    12.     public TextMeshProUGUI textObject;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         if (!Permission.HasUserAuthorizedPermission("android.permission.ACTIVITY_RECOGNITION"))
    17.         {
    18.             Permission.RequestUserPermission("android.permission.ACTIVITY_RECOGNITION");
    19.         }
    20.     }
    21.  
    22.     private void OnEnable()
    23.     {
    24.         InputSystem.AddDevice<Accelerometer>();
    25.         if (Accelerometer.current != null)
    26.         {
    27.             Debug.Log("Found Accelerometer Device");
    28.             InputSystem.EnableDevice(Accelerometer.current);
    29.             Accelerometer.current.MakeCurrent();
    30.         }
    31.  
    32.         InputSystem.AddDevice<StepCounter>();
    33.         if (StepCounter.current != null)
    34.         {
    35.             Debug.Log("Found StepCounter Device");
    36.             InputSystem.EnableDevice(StepCounter.current);
    37.             StepCounter.current.MakeCurrent();
    38.         }
    39.  
    40.         InputSystem.AddDevice<GravitySensor>();
    41.         if(GravitySensor.current != null)
    42.         {
    43.             Debug.Log("Found GravitySensor Device");
    44.             InputSystem.EnableDevice(GravitySensor.current);
    45.             GravitySensor.current.MakeCurrent();
    46.         }
    47.     }
    48.  
    49.     private void OnDisable()
    50.     {
    51.         if (Accelerometer.current != null)
    52.         {
    53.             InputSystem.RemoveDevice(Accelerometer.current);
    54.         }
    55.  
    56.         if (StepCounter.current != null)
    57.         {
    58.             InputSystem.RemoveDevice(StepCounter.current);
    59.         }
    60.  
    61.         if (GravitySensor.current != null)
    62.         {
    63.             InputSystem.RemoveDevice(GravitySensor.current);
    64.         }
    65.     }
    66.  
    67.     // Update is called once per frame
    68.     void FixedUpdate()
    69.     {
    70.         if (StepCounter.current != null && StepCounter.current.enabled)
    71.         {
    72.             textObject.text = StepCounter.current.stepCounter.ReadValue().ToString() + ", " + Accelerometer.current.acceleration.ReadValue().ToString() + ", " + GravitySensor.current.gravity.ReadValue().ToString();
    73.             Debug.Log("StepCounter - Last Update Time: " + StepCounter.current.lastUpdateTime.ToString());
    74.             Debug.Log("StepCoutner - Actuated: " + StepCounter.current.stepCounter.IsActuated());
    75.         }
    76.         else
    77.         {
    78.             textObject.text = "No StepCounter found";
    79.         }
    80.  
    81.         if (Accelerometer.current != null && Accelerometer.current.enabled)
    82.         {
    83.  
    84.  
    85.  
    86.         }
    87.         else
    88.         {
    89.             textObject.text += " No Accelerometer found";
    90.         }
    91.  
    92.         if(GravitySensor.current == null && !GravitySensor.current.enabled)
    93.         {
    94.             textObject.text += " No Gravity Sensor found";
    95.         }
    96.     }
    97. }
    When testing via the unity editor and testing the build version it seems as if only the accelerometer works on the unity editor version of the project but the build version displays no change just 0's all around. This is after I implemented permission into the custom manifest
    upload_2022-12-28_11-18-17.png


    In Unity Editor:
    (StepCounter, Accelerometer, Gyroscope)
    upload_2022-12-28_11-16-26.png

    Built and exported onto devices: LG K31 [phone] && Samsung Tab S8 [tablet]
    upload_2022-12-28_11-20-37.png

    I understand both the Accelerometer and the Gyroscope are built-in devices while the StepCounter is sorta calculated via the accelerometer, I just don't understand why the Gyroscope or the StepCounter aren't updating. I'm really stumped as to the accelerometer is not working on the build version of the project. There's not a lot of information to go off on the documentation to help me does anyone know what I could do or where I could visit for more information?
     
    wmadwand likes this.
  2. TheBen67

    TheBen67

    Joined:
    Dec 28, 2022
    Posts:
    3
    Hey Guys I have returned, I think I figured out a solution i'm not sure if its the optimal method of actually implimenting the input system but I think its working on my device. So basically I just do within Update()
    Code (CSharp):
    1. if(!StepCounter.current.enabled){
    2.   InputSystem.enable(StepCounter.current);
    3. }
    I also check in the Start() function if the sensor is missing and I add a new one using InputSystem.AddDevice<StepCounter>(); -- not sure if this actually does anything tho I haven't checked.

    I do it this way because if I were to enable everything via the Start or OnEnable method unity will say there's no current device so make a new one and then then I enable it, but the 'current' device I want enabled won't show up until after the start and OnEnable method have already run and i'm in the Update function - so I need a way to enable the 'current' device when Unity switches to the most recent device so that it can read input.

    I think that's what is going on but i'm still not sure. I think the proper method involves using a callback somewhere within the UnityInput system to fire when devices change or are updated but idk how to do that. If there's anyone out there who knows the actual method of implementing the sensors please let me know because this method sorta works but I wouldn't be surprised if its very very bad and taxing on the system. Thx you!

    Oh i'll also actually upload my code later this evening in case anyone would like to see it.

    Code (CSharp):
    1. public class WalkScript : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField]
    5.     public int count = 0;
    6.     [SerializeField]
    7.     public TextMeshProUGUI stepsText;
    8.     [SerializeField]
    9.     public TextMeshProUGUI accelText;
    10.     [SerializeField]
    11.     public TextMeshProUGUI infoText;
    12.     [SerializeField]
    13.     public AndroidStepCounter asc;
    14.  
    15.     // Start is called before the first frame update
    16.     void OnEnable()
    17.     {
    18.         if (!Permission.HasUserAuthorizedPermission("android.permission.ACTIVITY_RECOGNITION"))
    19.         {
    20.             Permission.RequestUserPermission("android.permission.ACTIVITY_RECOGNITION");
    21.         }
    22.     }
    23.  
    24.     private void Start()
    25.     {
    26.         if (StepCounter.current == null)
    27.         {
    28.             InputSystem.AddDevice<StepCounter>();
    29.         }
    30.  
    31.         if (!LinearAccelerationSensor.current.enabled)
    32.         {
    33.             InputSystem.EnableDevice(LinearAccelerationSensor.current);
    34.         }
    35.  
    36.         Debug.Log("Started LAS: " + LinearAccelerationSensor.current.ToString());
    37.     }
    38.  
    39.  
    40.     // Update is called once per frame
    41.     void FixedUpdate()
    42.     {
    43.         if (!LinearAccelerationSensor.current.enabled)
    44.         {
    45.             accelText.SetText("The Device is not enabled");
    46.             InputSystem.EnableDevice(LinearAccelerationSensor.current);
    47.             Debug.Log("Linear Acceleration was re-enabled!!");
    48.         }
    49.         else
    50.         {
    51.             Debug.Log("Currently Enabled: " + LinearAccelerationSensor.current.ToString());
    52.             accelText.SetText(LinearAccelerationSensor.current.acceleration.ReadValue().ToString());
    53.         }
    54.  
    55.         if (!StepCounter.current.enabled)
    56.         {
    57.             infoText.SetText("Stepcounter is Paused");
    58.             InputSystem.EnableDevice(StepCounter.current);
    59.             Debug.Log("Linear Acceleration was re-enabled!!");
    60.         }
    61.         else
    62.         {
    63.             Debug.Log("Currently Enabled: " + StepCounter.current.ToString());
    64.             stepsText.SetText("Steps: " + StepCounter.current.stepCounter.ReadValue().ToString());
    65.         }
    66.     }
    67. }
     
    Last edited: Jan 23, 2023
    tantx and wmadwand like this.
  3. wmadwand

    wmadwand

    Joined:
    Jul 19, 2014
    Posts:
    14
    Thanks for sharing buddy! I've been looking for any examples of StepCounter usage. Despite being optimized your solution actually does the job. As for my input I can only attach UnityRemoteTestScript from InputSystem samples :)
     

    Attached Files: