Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Service to collect sensor data only when user is moving

I wanted to track sensor data (accelerometer, gyro) when user is moving/ phone is not stationary.

Things I was able to do :

  1. Listen to sensor data using Sensor Listener
        sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
        
        val accelerometerSensor = 
        sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
    
        sensorManager.registerListener(
                accSensor,
                accelerometerSensor,
                SensorManager.SENSOR_DELAY_NORMAL
            )
  1. Run a foreground service that always keeps running in the background to track the sensors even when the app is swiped off from recent apps
serviceIntent = Intent(context, SensorService::class.java)
context.startForegroundService(serviceIntent)
  1. Start the service as soon as the app is rebooted using broadcast listeners which listen to Boot completed event.

But I was not able to:

  1. Stop sensor service that collects sensor data when the device stops moving.
  2. Start sensor (which is not running) when the user's device starts to move and collect sensor data.

What might be the way to receive the Motion start and Motion end notifications/callback from the system so that we can decide to start/end foreground services.

like image 421
erluxman Avatar asked Oct 04 '21 14:10

erluxman


People also ask

How do you collect data from a sensor?

The more common way of getting data out of smart sensors is to use a bridging device known as a gateway in each room. A gateway receives data from the sensors and makes it usable. Data is transmitted from the sensors to the gateway wirelessly.

How can I get sensor data in Android?

You can access sensors available on the device and acquire raw sensor data by using the Android sensor framework. The sensor framework provides several classes and interfaces that help you perform a wide variety of sensor-related tasks.

Is a sensor hardware or software?

Sensors can be hardware- or software-based. Hardware-based sensors are physical components built into a handset or tablet device. Hardware sensors derive their data by directly measuring specific environmental properties, such as acceleration, geomagnetic field strength, or angular change.

What is phone sensor data?

Smartphone sensor data provides organizations with a chance to learn about their employees' work patterns and behavior. IT could apply this data to the user authorization process to more accurately identify the users logging in to the device.


1 Answers

Too big for a comment, so I'll post it as an answer. And it is in some way an answer, but without proofs, just my experience.

I'm in no way an expert in the Android. But I know a thing or two about sensors in general. And the main thing is that they are always active (if not turned off entirely), and that there is always some noise. So

  1. One can't say with 100% certainty is there any movement or not. Instead you need to collect all events, choose your thresholds and decide it yourself. (Probably you'd want to aggregate the measurements from several sensors to make that decision, and even to consider the history of measurements - e.g. if a user is in a car on a bumpy road the noise will be higher for a prolonged period).
  2. There is virtually no difference in energy consumption if the corresponding code will be executed by the OS, or by your service. But there is a lot more opportunities to customize the thresholds, the aggregation and other logic with your code. That's why I believe that OS keeps API to the bare minimum, and there is no such events as moving starts/moving stops.
  3. The only thing that drains energy above the minimum is your code. And no one else, but your code decides to annoy users with a constant stream of notifications or not.

So, think of aggregation strategy and thresholds, don't show notifications if thresholds are not passed, and make your code fast (so that it won't use too much energy). And your are done... more or less... because these decisions are not easy to make. A lot of apps from big companies fail to make them right in all the cases. Just install a couple of pedometer-apps and you'll see that they all will show different counts of steps.

like image 191
x00 Avatar answered Sep 23 '22 07:09

x00