Get and show live data from cordova plugin in ionic 4 page

0

Issue

im trying to code a simple step counter app, im using this plugin (https://github.com/leecrossley/cordova-plugin-pedometer) in android to get the step counter data, so i want to get this data and show it on my ionic page (front end view), i tryed to use ngZone to refresh the object while the method subscribe to the startPedometerUpdates function, but it takes many seconds to work and sometimes stucks few seconds and then start to show the counter again…

this is my code:

steps: any  = []

  constructor(private ngZone: NgZone, private pedometer: Pedometer) { }

  ngOnInit() {
  }


  getSteps(){
    this.pedometer.startPedometerUpdates()
     .subscribe((data: IPedometerData) => {
     this.ngZone.run(() =>  this.steps.push(data))
     });
  }

my html is simple for debug the data:

<ion-content>
    {{this.steps | json}}
    <ion-button (click)="getSteps()">Show steps</ion-button>
</ion-content>

so i want to show the data in “real time” with something simple as posible…

thanks in advance

Solution

you can’t push data in subscribe even if you using NgZone bcoz push is a function ngZone doesn’t work in the function

    steps: any  = []

      constructor(private ngZone: NgZone, private pedometer: Pedometer) { 
        this.getSteps();
        setInterval(()=>{
           console.log('read in a sec')
        },1000)
      }

      ngOnInit() {
      }


      getSteps(){
        this.pedometer.startPedometerUpdates()
         .subscribe((data: IPedometerData) => {
         this.ngZone.run(() =>  this.steps.push(data))
         });
      }

Answered By – Aashay karekar

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More