android - 传感器的本机模块不可用。react-native 链接是否运行成功?
问题描述
React Native
我在这里遵循了教程的第一步:
https://facebook.github.io/react-native/docs/getting-started.html
然后我想从设备传感器读取信息。
为此,我还遵循了本教程:
https://medium.com/react-native-training/using-sensors-in-react-native-b194d0ad9167
并最终得到此代码(只需从那里复制/粘贴):
// 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,
},
});
这是您可以立即下载并尝试的完整存储库:
$ git clone https://github.com/napolev/react-native-app
$ cd react-native-app
$ npm i
$ expo start
我的问题是,在我这样做之后:$ expo start
我收到以下错误:
Native modules for sensors not available. Did react-native link run successfully?
如下图所示:
关于如何使这项工作的任何想法?
谢谢!
解决方案
这可能是由于您使用的是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
推荐阅读
- django - 在 Django 中从数据库中填充下拉列表
- java - 绑定一个 ObservableList
到 JavaFX 中的 HBox 的孩子? - javascript - 无法隐藏我用 javascript 和 jquery 生成的 html 元素
- node.js - 未定义:即使使用 upload.single() 中间件,req.file() 也会输出未定义
- regex - 我需要匹配一个不以特定字符集(文件扩展名)结尾的字符串
- ada - Ada 编程语言的 GUI
- html - 如何将颜色渐变应用到 .SVG 图像?
- android - 应用内结算的 Android 开发者通知
- r - 根据条件子集中的行号编辑 data.table 列
- javascript - 在数组中添加星号?