1

This isn't a problem, rather it is a question. Is there a way to get data from all three sensors at once (accelerometer, gyro, magnetometer) or is it just enough to set update interval for all three the same value. Like this

setUpdateIntervalForType(SensorTypes.accelerometer, 100); setUpdateIntervalForType(SensorTypes.magenetometer, 100); setUpdateIntervalForType(SensorTypes.gyroscope, 100);

const subscription = accelerometer.subscribe(({ x, y, z, timestamp }) => console.log({ x, y, z, timestamp }) );

const subscription1 = gyroscope.subscribe(({ x, y, z, timestamp }) => console.log({ x, y, z, timestamp }) );

const subscription2 = magenetometer.subscribe(({ x, y, z, timestamp }) => console.log({ x, y, z, timestamp }) );
kenobi91
  • 75
  • 1
  • 4

1 Answers1

3

Yes, these are RxJS Observables, those allow to be combined.

Let's say you would like to have a response like this:

{ 
  accelerometer: {x,y,z,timestamp}, 
  gyroscope: {x,y,z,timestamp},
  magnetometer: {x,y,z,timestamp}
}

and you would like to only emit on this observable if you have all data, not partial data.

An implementation looks like this:

import {
  combineLatest
} from "rxjs";
import {
  map
} from 'rxjs/operators';

import {
  accelerometer,
  magnetometer,
  gyroscope
} from "react-native-sensors";


const combinedStream = combineLatest(
  accelerometer,
  magnetometer,
  gyroscope
).pipe(
  map(([accelerometerValue, magnetometerValue, gyroscopeValue]) => ({
    accelerometer: accelerometerValue,
    magnetometer: magnetometerValue,
    gyroscope: gyroscopeValue
  }))
)
Daniel Schmidt
  • 11,605
  • 5
  • 38
  • 70
  • Haven't work with RxJS Observables before, i'm going to check them up :) Thank you so much for your answer :) – kenobi91 Jun 05 '19 at 18:09
  • hope it helps, could you approve the answer? – Daniel Schmidt Jun 06 '19 at 16:38
  • Yap it works, than you very much. Since this is new account i cant vote up for answer but yea it works. I did add a little more code to it to execute subscribe so i will add that code also whem my rep gets bigger :) – kenobi91 Jun 06 '19 at 18:50