@freqsc wrote:
I would like to create a simple service that listens to accelerometer data. How can I subscribe to these values from a page?
This is my page:
import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; import { AccelerometerProvider } from '../../providers/accelerometer/accelerometer'; @Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage { constructor(public navCtrl: NavController, private accelerometer: AccelerometerProvider) { } }and this is my service:
import { Injectable } from '@angular/core'; import { DeviceMotion, DeviceMotionAccelerationData } from '@ionic-native/device-motion'; import { Observable } from 'rxjs/Observable'; @Injectable() export class AccelerometerProvider { constructor(private deviceMotion: DeviceMotion) { console.log('Hello AccelerometerProvider Provider'); } start() { this.deviceMotion.watchAcceleration().subscribe((acceleration: DeviceMotionAccelerationData) => { let x = acceleration.x; let y = acceleration.y; let z = acceleration.z; }); } }What I want is just have access to x, y, z values from my page, each time a new subscription comes.
I suppose it is a n ‘observable’ thing but I am fairly new to this technology. I would appreciate any help.
Posts: 1
Participants: 1