How to update GPS status all of the time?


#1

I want to know about the status of the GPS during the use of the app.This means that if the user deactivates the phone GPS, I warn him to turn on the GPS. At the moment, I can only check the status of the GPS at the start of run the app.

import React, { Component } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import { Constants, Location, Permissions } from 'expo';
import { connect } from 'react-redux';
import { setGps } from '../actions'

 class GpsSensor extends Component {
  state = {
    location: null,
    errorMessage: null,
    gps:false,
  };

  componentWillMount() {
    if (Platform.OS === 'android' && !Constants.isDevice) {
      this.setState({
        errorMessage: 'Oops, this will not work on Sketch in an Android emulator. Try it on your device!',
      });
    } else {
      this.getLocation();
    }
  }
  componentDidMount() {
    this._getGpsStatus();
  }

  _getLocationAsync = async () => {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    if (status !== 'granted') {
      this.setState({
        errorMessage: 'Permission to access location was denied',
      });
    }

    let location = await Location.getCurrentPositionAsync({});
    this.setState({ location });
  };

  getLocation = () => {
    
    this.mlocation = Expo.Location.watchPositionAsync({
        enableHighAccuracy: true,
        timeInterval: 1,
        distanceInterval: 1
    }, (loc) => {
        if (loc.timestamp) {
           this.props.dispatch(setGps(loc));
           this.setState({location:loc});
        } else {
          this.setState({errorMessage:'Problems on update location'})
        }
    });
}

_getGpsStatus= async () => {
  let { status } = await Permissions.askAsync(Permissions.LOCATION);
  if (status !== 'granted') {
    this.setState({
      errorMessage: 'Permission to access location was denied',
    });
  }

  let gps = await Location.getProviderStatusAsync();
  this.setState({ gps: gps.gpsAvailable });
  console.log(gps.gpsAvailable)
};
  render() {
    let text = 'Waiting..';
    if (this.state.errorMessage) {
      text = this.state.errorMessage;
    } else if (this.state.location) {
      text = JSON.stringify(this.state.location);
    }

    let mGps;
    if (this.state.gps) {
      mGps="true"
    }
    else mGps="false"

    return (
      <View style={styles.container}>
        <Text> Gps Sensor : </Text>
        <Text style={styles.paragraph}>{text}</Text>
        <Text> "this.state.gps": {mGps} </Text>
      </View>
    );
  }
}

GpsSensor = connect()(GpsSensor);

export default GpsSensor

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    textAlign: 'center',
  },
});

#2

Currently you cannot observe changes of Permissions.

A standard way of observing a users permissions at “all times” is to check whenever the AppState changes. A user would have to leave the app to turn the permission off, when they open the app back up then you check again.

Here is some pseudo code that demonstrates listening to AppState changing… :smiley:

import {
  AppState,
} from 'react-native';

  state = {
    appState: AppState.currentState
  }

  componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
  }

  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
  }

  _handleAppStateChange = (nextAppState) => {
    if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
      console.log('App has come to the foreground!')
    } else {
      console.log('App has left foreground :/')
    }
  }

#3

tnx.I will try it and give the result


#4

Were you able to make it work? Facing the same issue.


#5

The solution I proposed will work! :slight_smile: I noticed it’s a little short on details so here is a snack and an experience (sometimes the snack doesn’t work for whatever reason (nothing demo related :grin:) )

Let me know if this is more clear :heart:


#6

It doesn’t work for Android with quick setting panel feature(all new ones has it).You can turn on/off the location service at the panel. The appStatus doesn’t change when the quick setting is showing.