react-native-sensors 所有三个传感器读数
react-native-sensors All three sensors reading
这不是问题,而是问题。有没有办法一次从所有三个传感器(加速度计、陀螺仪、磁力计)获取数据,或者是否足以为所有三个相同的值设置更新间隔。像这样
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 }) );
是的,这些是 RxJS Observables,它们允许组合。
假设您希望收到这样的回复:
{
accelerometer: {x,y,z,timestamp},
gyroscope: {x,y,z,timestamp},
magnetometer: {x,y,z,timestamp}
}
如果您拥有所有数据,而不是部分数据,您希望只在这个可观察对象上发射。
实现如下所示:
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
}))
)
这不是问题,而是问题。有没有办法一次从所有三个传感器(加速度计、陀螺仪、磁力计)获取数据,或者是否足以为所有三个相同的值设置更新间隔。像这样
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 }) );
是的,这些是 RxJS Observables,它们允许组合。
假设您希望收到这样的回复:
{
accelerometer: {x,y,z,timestamp},
gyroscope: {x,y,z,timestamp},
magnetometer: {x,y,z,timestamp}
}
如果您拥有所有数据,而不是部分数据,您希望只在这个可观察对象上发射。
实现如下所示:
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
}))
)