cph-cachet / carp.sensing-flutter

CARP Mobile Sensing for Flutter, including mobile sensing framework, data backend support, and the CARP mobile sensing app.
MIT License
79 stars 28 forks source link

passive sensing problem #97

Closed sc00n closed 3 years ago

sc00n commented 3 years ago

I tried to use the package for passive sensing, but I can't get it to work.

I applied the best practice tips (https://github.com/cph-cachet/carp.sensing-flutter/wiki/5.-Best-Practice):

image

I used carp_mobile_sensing: 0.9.2. To test the package I simply created a new flutter project and added the following to _MyHomePageState

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initCarp();
  }

  void initCarp() async {
    // Create a study using a local file to store data
    Study study = Study("2", 'user@cachet.dk',
        name: 'A study collecting ..',
        dataEndPoint: FileDataEndPoint()
          ..bufferSize = 500 * 1000
          ..zip = true
          ..encrypt = false);

    // but delay the sampling by 10 seconds
    study.addTriggerTask(
        ImmediateTrigger(),
        AutomaticTask(name: 'Sensor Task')
          ..addMeasure(PeriodicMeasure(MeasureType(NameSpace.CARP, SensorSamplingPackage.LIGHT),
              frequency: const Duration(seconds: 10), duration: const Duration(milliseconds: 100))));

    // Create a Study Controller that can manage this study.
    StudyController controller = StudyController(study);

    // await initialization before starting/resuming
    await controller.initialize();
    controller.resume();

    // listening and print all data events from the study
    controller.events.forEach(print);
  }

I also adapted the AndroidManifest as mentioned in the Readme.

When I read the generated files (e.g. carp/data/2/carp-data-2020-09-24-17-35-33-471360.json) I see that the app clearly logs every 10 second when I am in the app. When the app is open but the screen is closed I barely get any log and when the app is closed I get no logs at all.

I tested with Android emulator (Pixel 2, API 28) and my phone (Samsung Galaxy A40, Android 9).

Am I doing something wrong/can I change something, or is passive sensing just an illusion with current Android battery restrictions?

Thank you for your help!

Merijn

bardram commented 3 years ago

Hi @sc00n - we've just release a version 0.9.3. From version 0.9.3 the CAMS carp_context_package make use of the carp_background_location. If you include the context package also, and follow the instruction on how to modify the Android Manifest.xml file, this will help keep the app running in the background.

But you're right - keeping passive sensing running on Android is getting more and more restricted. We will continue to look into this.

bardram commented 3 years ago

When the app is open but the screen is closed I barely get any log

Regarding the background sampling - if you look at Figure 6 in the CAMS paper

you can see that there is close to 100% sampling coverage both on Android and iOS. Also when the app is in the background. However, these tests were done on a Samsung Galaxy A3, Android OS level 8.0.

I'm planning to rerun these test on Android 9.0 soon to investigate.

and when the app is closed I get no logs at all.

No - the app has to run for sampling to run. CAMS is linked into the app (this is the whole idea of CAMS). If you want background sampling independent of your app, you should look for other (native) approaches, like AWARE or mCerebrum.

bardram commented 3 years ago

I've now looked into this. It appear that the light sensor is "stopped" on Android 10.

Here are the same test as in Figure 6 in the paper, done on

2020 10 13 SM-A320FL coverage

2020 10 13 SM-G970F coverage

This tests shows that on Android 10/API 29, the light sensor:

I did several tests on this, and it's pretty consistent. According to the Android API docs: "most device manufacturers use [the light sensor] to control screen brightness". Hence, I expect that a reason is that the light sensor is turned off, when the screen is turned off, since there is no need for it in terms of controlling the scree brightness. I don't know if there is a way around this.

However, all the other sensors are running as expected - memory, noise, wifi. The location probe seems "unstable", and this has a triggering effect on weather and air_quality, which uses current location. We have recently changed to using our own carp_background_location, which might explain this behaviour.

This shows that also on Android 10, CAMS keeps running in the background. However, some sensors -- like the light sensor -- behave differently.

bardram commented 3 years ago

Also found this these Best Practices for Accessing and Using Sensors in Android 9+. Here is says:

On devices running Android 9 (API level 28) or higher, apps running in the background have the following restrictions:

  • Sensors that use the continuous reporting mode, such as accelerometers and gyroscopes, don't receive events.
koenniem commented 3 years ago

The docs also say

Given these restrictions, it's best to detect sensor events either when your app is in the foreground or as part of a foreground service.

So what if the entire sensing class would run on a foreground service? That way, the sensing would stay active even if the app were closed and the sensors would get a higher priority. I wonder if and how this impacts battery life though.

bardram commented 3 years ago

I've made this blog post

https://carp.cachet.dk/sampling-coverage-in-mobile-sensing-on-android-8-vs-10/

giving a status on this. Still testing it on iOS, though.

Will also consider to add foreground service to CAMS core later -- issue #101

bardram commented 3 years ago

Finally - after release of version 0.9.5 - I managed to run the coverage test on iOS. With good results, as the graph below shows.

2020 11 02 iPhone 11 Pro coverage

Doing a blog post on this too....