捆绑错误:语法错误使用React-native-android-wifi时出现意外令牌

时间:2018-12-23 16:37:17

标签: react-native react-native-android

我是react-native的初学者,想在我的android设备中管理wifi。基本上,我想征得许可以打开wifi,显示wifi-list并断开wifi,因此我遵循了本教程:

下面的请求许可的代码

import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';

var wifi = require('react-native-android-wifi');

export default class App extends Component {

  wifi.isEnabled((isEnabled) => {
    if (isEnabled) {
      console.log("wifi service enabled");
    } else {
      console.log("wifi service is disabled");
    }
  });

  render() {
    return (
      <View style={styles.container}>
        <Text>Welcome to React Native!</Text>
        <Text>To get started, edit App.js</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
});

我收到此错误:

有人可以帮助我吗,为什么我会收到错误消息,我该怎么办?

1 个答案:

答案 0 :(得分:1)

这是由于您需要在componentDidMount中而不是在类正文中调用wifi.isEnabled

将您的组件更新为:

export default class App extends Component{

  componentDidMount() {
    wifi.isEnabled((isEnabled)=>{
      if (isEnabled){
       console.log("wifi service enabled");
      }else{
        console.log("wifi service is disabled");
      }
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <Text>Welcome to React Native!</Text>
        <Text>To get started, edit App.js</Text>
      </View>
    );
  }
}

查看react-native-android-wifi的存储库,您仍然需要执行一些步骤。

首先,您需要请求访问该位置的权限。因此,请在componentDidMount中进行此操作,因为这是一个异步请求,我们需要确保您的componentDidMount也是异步的。

第二,我们要执行检查。我们可以将其绑在按钮上。

由于我们正在使用Button组件并希望请求Android权限,因此我们还需要从react-native导入一些内容。

import { Button, PermissionsAndroid } from 'react-native';

export default class App extends Component {

  async componentDidMount () {
    this.askForUserPermissions();
  }

  async askForUserPermissions () {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          'title': 'Wifi networks',
          'message': 'We need your permission in order to find wifi networks'
        }
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('Thank you for your permission! :)');
      } else {
        console.log('You will not able to retrieve wifi available networks list');
      }
    } catch (err) {
      console.warn(err);
    }
  }

  checkWiFi () {
    wifi.isEnabled((isEnabled) => {
      if (isEnabled) {
        console.log('wifi service enabled');
      } else{
        console.log('wifi service is disabled');
      }
    });
  }

  render () {
    return (
      <View style={styles.container}>
        <Button title={'check wifi'} onPress={this.checkWiFi.bind(this)} />
        <Text>Welcome to React Native!</Text>
        <Text>To get started, edit App.js</Text>
      </View>
    );
  }
}

有关如何进行此设置的更多详细信息,请查看存储库中的examples,它们显示了如何执行您可能想做的大多数事情。

相关问题