Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Both ACCESS_WIFI_STATE & ACCESS_FINE_LOCATION permissions are required (Android) #294

Open
tiendatnguyen678 opened this issue Jun 14, 2021 · 1 comment

Comments

@tiendatnguyen678
Copy link

tiendatnguyen678 commented Jun 14, 2021

When i use getCurrentPlace() the message error return:

Error: Both ACCESS_WIFI_STATE & ACCESS_FINE_LOCATION permissions are required
at Object.fn [as getCurrentPlace] (NativeModules.js:99)
at RNGooglePlaces.getCurrentPlace (index.js:50)
at Object.onPress (DetailInstallScreen.js:2071)
at Object.touchableHandlePress (TouchableOpacity.js:264)
at Object._performSideEffectsForTransition (Touchable.js:880)
at Object._receiveSignal (Touchable.js:779)
at Object.touchableHandleResponderRelease (Touchable.js:490)
at Object.invokeGuardedCallbackImpl (ReactNativeRenderer-dev.js:307)
at invokeGuardedCallback (ReactNativeRenderer-dev.js:531)
at invokeGuardedCallbackAndCatchFirstError (ReactNativeRenderer-dev.js:555)

AndroidManifest.xml file:

<!-- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Required -->
<uses-permission android:name="android.permission.CAMERA" />

<!-- Include this only if you are planning to use the camera roll -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Include this only if you are planning to use the microphone for video recording -->
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

<application
  android:largeHeap="true"
  android:name=".MainApplication"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:allowBackup="false"
  android:theme="@style/AppTheme"
  android:requestLegacyExternalStorage="true"
  >
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
  <meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="my_api_key"/>
  <uses-library android:name="org.apache.http.legacy" android:required="false"/>
</application>

But when i check PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION) & PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_WIFI_STATE) it's result is true.

This is my function:

RNGooglePlaces.getCurrentPlace()
.then((results) => {
console.log(results)
this.setState({
location: {
latitude: results[0].location.latitude,
longitude: results[0].location.longitude
}
})
}).catch((error) => console.log(error))

On iOS is ok. Thanks for help!

@RafikMk
Copy link

RafikMk commented Jan 11, 2024

It's possible that the "Both ACCESS_WIFI_STATE & ACCESS_FINE_LOCATION permissions are required" error you're encountering with RNGooglePlaces.getCurrentPlace() on Android may be due to a synchronization issue in how permissions are checked or requested. Although your AndroidManifest.xml appears correct and the ACCESS_FINE_LOCATION and ACCESS_WIFI_STATE permissions seem to be granted (as indicated by your permission checks returning true), there might be an issue in the sequence of permission requests or in how the RNGooglePlaces library handles these permissions.

A potential solution would be to ensure that all necessary permissions are explicitly requested and granted before calling RNGooglePlaces.getCurrentPlace(). Here is a requestLocationPermission method you can use to handle this:

async function requestLocationPermission() {
  try {
   if (Platform.OS === 'android') {
       const hasPermission = await PermissionsAndroid.check(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      );

       if (hasPermission) {
        console.log('La permission de localisation est déjà activée');
        return true;
      }

       const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          title: 'Localisation',
          message: 'Taxi Coq besoin un accès à votre localisation',
          buttonNegative: 'Annuler',
          buttonPositive: 'OK',
        },
      );

       if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('La localisation a été activée avec succès sur Android');
        return true;
      } else {
        console.log('Permission de localisation refusée sur Android');
        return false;
      }
    }
  } catch (err) {
    console.warn(err);
    return false;
  }
}

In this method, we are ensuring that the ACCESS_FINE_LOCATION permission is checked and requested on Android before performing location-dependent operations. Try calling requestLocationPermission() before RNGooglePlaces.getCurrentPlace() to see if it resolves the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants