Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

[pedometer 4.0.1] stepCountStream listened in a Foreground Service does not trigger onData when phone is locked and screen is closed. Triggers when lock screen is shown again.

Open
#952 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
android, dart, flutter
Domain
mobile

Research direction

Start from the Android implementation behind Pedometer.stepCountStream and compare sensor event delivery while the device is unlocked versus locked with the screen off. Reproduce the issue on the Huawei P9 Lite running Android 7.0, using the onStart foreground-service entry point and the step listener; done means step data is delivered while the screen remains locked and black.

Written by the indexing model from the issue text.

Description

bugfix pedometer
Device / Emulator and OS
  • Device: Huawei P9 Lite
  • OS: Android 7.0

I'm developing a Flutter app using pedometer package to track steps of the user. I want to count steps when app is closed as well, so I used flutter_background_service package as well in order to use foreground service on Android.

Basically this is my setup Background Service initialization logic (which is basically configured to use onStart method mentioned below):

Future<void> initializeBackgroundService() async {
  final service = FlutterBackgroundService();

  await service.configure(
    androidConfiguration: AndroidConfiguration(
      // this will be executed when app is in foreground or background in separated isolate
      onStart: onStart,
      // auto start service
      autoStart: true,
      isForegroundMode: true,
      notificationChannelId: 'my_foreground',
      initialNotificationTitle: 'AWESOME SERVICE',
      initialNotificationContent: 'Initializing',
      foregroundServiceNotificationId: 888,
    ),
    iosConfiguration: IosConfiguration(
      // auto start service
      autoStart: true,
      // this will be executed when app is in foreground in separated isolate
      onForeground: onStart,
      // you have to enable background fetch capability on xcode project
      onBackground: onIosBackground,
    ),
  );
}

onStart method (method called when foreground service is started):

@pragma('vm:entry-point')
Future<void> onStart(ServiceInstance service) async {
  DartPluginRegistrant.ensureInitialized();

  if (service is AndroidServiceInstance) {
    service.on('setAsForeground').listen((event) {
      service.setAsForegroundService();
    });

    service.on('setAsBackground').listen((event) {
      service.setAsBackgroundService();
    });
  }

  service.on('stopService').listen((event) {
    service.stopSelf();
  });
  
  await activatePedometer(service);
}

And the content of activatePedometer method:

Future<void> activatePedometer(ServiceInstance service) async {
  Stream<StepCount> pedometerStream = Pedometer.stepCountStream;
  var subscription = pedometerStream.listen((stepCount) async {
    print("STEP DATA LISTENED!!!");
  }
   
  Timer.periodic(const Duration(seconds: 1), (timer) async {
    print('TIMER: ${DateTime.now()}!!!');
  });
}

listen callback of stepCountStream is called in the following scenarios:

  • When app is open
  • When app is minimized
  • When app is closed
  • When phone lock screen is shown

And as you can see in the code, "STEP DATA LISTENED!!!" is printed on debug console.

But when phone is locked and screen goes black, this callback is not triggered even when user takes steps. When user uses lock button to show lock screen, it is triggered again. I want to be able to read step data even when phone is sleeping (locked and screen is black).

I wanted to see if this is a foreground service issue, that's why I added the following:

Timer.periodic(const Duration(seconds: 1), (timer) async {
  print('TIMER: ${DateTime.now()}!!!');
});

"TIMER: 2024-04-27 21:58:00.657213!!!" is printed every second even when phone is locked and screen goes black. So it's clearly not a foreground service issue.

Has anyone encountered a problem similar to this? Is there anything I can do to ensure listen callback is triggered even when phone is sleeping (locked and screen is black)?

Dominant language
Dart
Stars
610
Forks
733
Avg merge
33m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from carp-dk/flutter-plugins

All issues in carp-dk/flutter-plugins

Similar issues

More Dart issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.