Put the Below in Expo Playground and try to see it from a Device.
This is not working,
kyriakosmichail@gmail.com
Thank you
import React, { Component } from ‘react’;
import { Text, View, StyleSheet, Button, Alert } from ‘react-native’;
import { Constants, Accelerometer } from ‘expo’;
export default class App extends Component {
state = {
accelerometerData: { x: 0, y: 0, z: 0 }
};
componentWillUnmount() {
this._unsubscribeFromAccelerometer();
}
componentDidMount() {
this._subscribeToAccelerometer();
}
_handleButtonPress = () => {
Alert.alert(
<Text>
Accelerometer:
x = {this.state.accelerometerData.x.toFixed(2)}{', '}
y = {this.state.accelerometerData.y.toFixed(2)}{', '}
z = {this.state.accelerometerData.z.toFixed(2)}
</Text>
);
};
_subscribeToAccelerometer = () => {
this._acceleroMeterSubscription = Accelerometer.addListener(accelerometerData =>
this.setState({ accelerometerData })
);
};
_unsubscribeFromAccelerometer = () => {
this._acceleroMeterSubscription && this._acceleroMeterSubscription.remove();
this._acceleroMeterSubscription = null;
};
render() {
return (
See Accelerometer Values
<Button
title="Show me!"
onPress={this._handleButtonPress}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: ‘center’,
justifyContent: ‘center’,
paddingTop: Constants.statusBarHeight,
backgroundColor: ‘#ecf0f1’,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: ‘bold’,
textAlign: ‘center’,
color: ‘#34495e’,
},
});