代码之家  ›  专栏  ›  技术社区  ›  davidesp

传感器的本机模块不可用。本机链接是否成功运行?

  •  0
  • davidesp  · 技术社区  · 6 年前

    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?
    

    如下图所示:

    enter image description here

    你知道我该怎么做吗?

    谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   Andrew    6 年前

    这可能是因为您正在使用 博览会 react-native-sensors 自然反应

    使用 反应本机传感器

    有关Expo和React Native之间差异的更多详细信息,请查看以下答案: What is the difference between Expo and React Native?

    不过,Expo确实可以访问一些传感器信息,您可以在此处阅读更多有关使用加速度计的信息 https://docs.expo.io/versions/latest/sdk/accelerometer

    世博会还可以进入 gyroscope magnetometer pedometer docs

        2
  •  1
  •   James Nicholson    5 年前

    我有一个类似的问题,我解决的是使用npx

    npx反应本机链路反应本机传感器;

    https://blog.npmjs.org/post/162869356040/introducing-npx-an-npm-package-runner

    然后我清除了应用程序数据并从手机中删除了应用程序,然后重新构建!