传感器的本机模块不可用.react-native 链接是否成功运行? [英] Native modules for sensors not available. Did react-native link run successfully?
问题描述
我在这里遵循了 React Native
教程的第一步:
有什么想法可以让我完成这项工作吗?
谢谢!
这可能是因为您使用的是 Expo 并且 react-native-sensors
需要React Native 的完整版.
要使用 react-native-sensors
,您可能必须弹出您的应用,然后安装依赖项.如果您弹出应用程序,则需要在开发机器上安装 Xcode(适用于 iOS)和 Android Studio(适用于 Android).
有关 Expo 和 React-Native 之间差异的更多详细信息,请查看此 SO 答案:Expo 和 React Native 有什么区别?
但是,Expo 确实可以访问一些传感器信息,您可以在此处阅读有关使用加速度计的更多信息 https://docs.expo.io/versions/latest/sdk/accelerometer
Expo 还可以使用 陀螺仪,
I followed the first steps of the React Native
tutorial here:
https://facebook.github.io/react-native/docs/getting-started.html
Then I want to read information from the device sensors.
For that I also followed this tutorial:
https://medium.com/react-native-training/using-sensors-in-react-native-b194d0ad9167
and ended up with this code (just copy/pasted from there):
// Reference:
// https://medium.com/react-native-training/using-sensors-in-react-native-b194d0ad9167
// https://react-native-sensors.github.io
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
import { Accelerometer } from "react-native-sensors";
const Value = ({name, value}) => (
<View style={styles.valueContainer}>
<Text style={styles.valueName}>{name}:</Text>
<Text style={styles.valueValue}>{new String(value).substr(0, 8)}</Text>
</View>
)
export default class App extends Component {
constructor(props) {
super(props);
new Accelerometer({
updateInterval: 400 // defaults to 100ms
})
.then(observable => {
observable.subscribe(({x,y,z}) => this.setState({x,y,z}));
})
.catch(error => {
console.log("The sensor is not available");
});
this.state = {x: 0, y: 0, z: 0};
}
render() {
return (
<View style={styles.container}>
<Text style={styles.headline}>
Accelerometer values
</Text>
<Value name="x" value={this.state.x} />
<Value name="y" value={this.state.y} />
<Value name="z" value={this.state.z} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
headline: {
fontSize: 30,
textAlign: 'center',
margin: 10,
},
valueContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
},
valueValue: {
width: 200,
fontSize: 20
},
valueName: {
width: 50,
fontSize: 20,
fontWeight: 'bold'
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
Here is the full repository you can download and try right away:
$ git clone https://github.com/napolev/react-native-app
$ cd react-native-app
$ npm i
$ expo start
My problem is that after I do: $ expo start
I get the following error:
Native modules for sensors not available. Did react-native link run successfully?
as you can see on the following image:
Any idea about how can I make this work?
Thanks!
It's probably due to the fact that you are using Expo and that react-native-sensors
requires the full version of React Native.
To use react-native-sensors
you will probably have to eject your app and then install the dependency. If you eject your app it will require you to have Xcode (for iOS) and Android Studio (for Android) installed on your development machine.
For more details on the differences between Expo and React-Native check out this SO answer: What is the difference between Expo and React Native?
However, Expo does have access to some sensor info, you can read more about using the accelerometer here https://docs.expo.io/versions/latest/sdk/accelerometer
Expo also has access to the gyroscope, the magnetometer and a pedometer, though no barometer that I can see in the docs
这篇关于传感器的本机模块不可用.react-native 链接是否成功运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!