Need help with native API "cordova-plugin-stepcounter"

Hello everyone,

currently I implementing an ionic app for Android, with the feature to count steps. For this I used the native api of Cordova: cordova-plugin-stepcounter.

When I install the app on a Samsung S9, Samsung S7, Samsung Note or another Smartphones the steps are counted after the first start of the app. I can close the app and the steps will still be counted. Everything runs smoothly until a restart crashes the app. After restarting the phone, the app crashes and no more steps are counted. The value of the step is the same as before the phone restart. After a restart, the steps are no longer counted.

Running the method “start(startingOffset)” to get the stepcounter running again is unfortunately not enough. In order for the Stepcounter to work again, you have to uninstall and reinstall the App.

Thanks for any advice :slight_smile:

import { Injectable, NgModule, ChangeDetectorRef  } from '@angular/core';
import { AlertController } from 'ionic-angular';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { Stepcounter } from '@ionic-native/stepcounter';

@IonicPage()
@Injectable()
@NgModule()
@Component({
  selector: 'page-tab1',
  templateUrl: 'tab1.html'
})
export class Tab1Page {

  canCount: any;
  steps: any = 0;

  constructor(public navCtrl: NavController, public navParams: NavParams, public statusBar: StatusBar, 
              public stepcounter: Stepcounter, public plt: Platform, public alertCtrl: AlertController,
              private ref: ChangeDetectorRef) {
    this.plt.ready()
      .then(() => {
          this.stepcounter.deviceCanCountSteps().then((res: any) => {
            this.canCount = res;

            if (this.canCount == false) {
              let alert = this.alertCtrl.create({
                title: 'Alert',
                subTitle: 'Oops!! Your device is not able to count your steps !!',
                buttons: ['OK']
              });
              alert.present();
            }
            else if (this.canCount === true){
              let alert = this.alertCtrl.create({
                title: 'Hinweis',
                subTitle: 'Stepcounter-Funktion verfügbar',
                buttons: ['OK']
              });
              alert.present();
              this.startCounter();
            }
          })
          .catch((err) => {alert("ALARM ALARM ALARM: " + err); console.log("ALARM ALARM ALARM: " + err);});
        })
      .catch((errr) => alert("LOOOOOOOG :" + errr));

}

  startCounter(){
    try{
      let startOffset:any = 0;
      
      this.stepcounter.start(startOffset).then((res: any) => { 
        this.steps= res; 
        alert("Starten erfolgreich");
      }); 
    }
    catch{
      alert("Starten fehlgeschlagen");
    }
  }


 fetchSteps(){
    this.stepcounter.getTodayStepCount().then((res:any) => {
        this.steps= res;  
        this.ref.detectChanges();
      });
  }
}

Would this statement be equivalent to what you are say, “Everything runs smoothly until after a restart which crashes the app.”?

Reading your code and being ignorant on how apps continue running after a restart, perhaps upon restart the app creates another instance of stepcounter and/or process which conflict with the previous instance and therefore crashes.

Hopefully that helps.

Below you can find the relevant part of the bugreport of my Samsung mobile:

------ SYSTEM LOG (logcat -v threadtime -v printable -v uid -d *:v) ------
--------- beginning of crash
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: FATAL EXCEPTION: main
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: Process: io.ionic.healthyapp, PID: 12405
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver net.texh.cordovapluginstepcounter.StepCounterBootReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=io.ionic.healthyapp/net.texh.cordovapluginstepcounter.StepCounterService }: app is in background uid UidRecord{8ed6a8c u0a243 RCVR idle procs:1 seq(0,0,0)}
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3397)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.app.ActivityThread.-wrap18(Unknown Source:0)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:105)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6938)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=io.ionic.healthyapp/net.texh.cordovapluginstepcounter.StepCounterService }: app is in background uid UidRecord{8ed6a8c u0a243 RCVR idle procs:1 seq(0,0,0)}
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1538)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.app.ContextImpl.startService(ContextImpl.java:1484)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:663)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.content.ContextWrapper.startService(ContextWrapper.java:663)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at net.texh.cordovapluginstepcounter.StepCounterBootReceiver.onReceive(StepCounterBootReceiver.java:17)
12-30 04:12:35.680 10243 12405 12405 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3390)