Skip to content

Commit 4f00b4a

Browse files
enhanced example
1 parent 9f4e1b9 commit 4f00b4a

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

SensorExample.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import {
77

88
import RNSensors from 'react-native-sensors';
99
const { Accelerometer, Gyroscope } = RNSensors;
10-
const accelerationObservable = new Gyroscope({
10+
const accelerationObservable = new Accelerometer({
1111
updateInterval: 100, // defaults to 100ms
1212
});
1313

14+
const gyroscopeObservable = new Gyroscope({
15+
updateInterval: 2000, // defaults to 100ms
16+
});
17+
1418
export default class SensorExample extends Component {
1519
constructor(props) {
1620
super(props);
@@ -20,33 +24,53 @@ export default class SensorExample extends Component {
2024
y: 'unknown',
2125
z: 'unknown',
2226
},
27+
gyroscope: {
28+
x: 'unknown',
29+
y: 'unknown',
30+
z: 'unknown',
31+
}
2332
};
2433
}
34+
2535
componentWillMount() {
2636
accelerationObservable
2737
.subscribe(acceleration => this.setState({
2838
acceleration,
2939
}));
40+
41+
gyroscopeObservable
42+
.subscribe(gyroscope => this.setState({
43+
gyroscope,
44+
}));
3045
}
46+
3147
render() {
3248
const {
3349
acceleration,
50+
gyroscope,
3451
} = this.state;
3552

3653
return (
3754
<View style={styles.container}>
3855
<Text style={styles.welcome}>
39-
Acceleration
56+
Acceleration:
4057
</Text>
4158
<Text style={styles.instructions}>
4259
{acceleration.x + '/' + acceleration.y + '/' + acceleration.z}
4360
</Text>
61+
<Text style={styles.welcome}>
62+
Gyroscope:
63+
</Text>
64+
<Text style={styles.instructions}>
65+
{gyroscope.x + '/' + gyroscope.y + '/' + gyroscope.z}
66+
</Text>
4467
</View>
4568
);
4669
}
4770

4871
componentWillUnmount() {
4972
accelerationObservable.stop();
73+
gyroscopeObservable.stop();
5074
}
5175
}
5276

0 commit comments

Comments
 (0)