diff --git a/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java b/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java index 8313938..8ff42a4 100644 --- a/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java +++ b/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java @@ -71,15 +71,7 @@ public String getName() { */ @ReactMethod public void loadWifiList(final Promise promise) { - final boolean locationPermissionGranted = PermissionUtils.isLocationPermissionGranted(context); - if (!locationPermissionGranted) { - promise.reject(LoadWifiListErrorCodes.locationPermissionMissing.toString(), "Location permission (ACCESS_FINE_LOCATION) is not granted"); - return; - } - - final boolean isLocationOn = LocationUtils.isLocationOn(context); - if (!isLocationOn) { - promise.reject(LoadWifiListErrorCodes.locationServicesOff.toString(), "Location service is turned off"); + if(!assertLocationPermissionGranted(promise)){ return; } @@ -209,15 +201,7 @@ public void setEnabled(final boolean enabled) { */ @ReactMethod public void connectToProtectedSSID(@NonNull final String SSID, @NonNull final String password, final boolean isWep, final Promise promise) { - final boolean locationPermissionGranted = PermissionUtils.isLocationPermissionGranted(context); - if (!locationPermissionGranted) { - promise.reject(ConnectErrorCodes.locationPermissionMissing.toString(), "Location permission (ACCESS_FINE_LOCATION) is not granted"); - return; - } - - final boolean isLocationOn = LocationUtils.isLocationOn(context); - if (!isLocationOn) { - promise.reject(ConnectErrorCodes.locationServicesOff.toString(), "Location service is turned off"); + if(!assertLocationPermissionGranted(promise)) { return; } @@ -289,6 +273,10 @@ public void failed(@NonNull DisconnectionErrorCode errorCode) { */ @ReactMethod public void getCurrentWifiSSID(final Promise promise) { + if(!assertLocationPermissionGranted(promise)){ + return; + } + String ssid = getWifiSSID(); if (ssid == null) { promise.reject(GetCurrentWifiSSIDErrorCodes.couldNotDetectSSID.toString(), "Not connected or connecting."); @@ -393,6 +381,10 @@ public void failed(@NonNull RemoveErrorCode errorCode) { */ @ReactMethod public void reScanAndLoadWifiList(final Promise promise) { + if(!assertLocationPermissionGranted(promise)) { + return; + } + final WifiScanResultReceiver wifiScanResultReceiver = new WifiScanResultReceiver(wifi, promise); getReactApplicationContext().registerReceiver(wifiScanResultReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); wifi.startScan(); @@ -470,7 +462,7 @@ public void onAvailable(Network network) { @Override public void onUnavailable() { super.onUnavailable(); - promise.reject(ConnectErrorCodes.userDenied.toString(), "On Android 10, the user cancelled connecting (via System UI)."); + promise.reject(ConnectErrorCodes.didNotFindNetwork.toString(), "Network not found or network request cannot be fulfilled."); } @Override @@ -576,4 +568,20 @@ private void stuffWifiConfigurationWithoutEncryption(final WifiConfiguration wif private String formatWithBackslashes(final String value) { return String.format("\"%s\"", value); } + + private boolean assertLocationPermissionGranted(final Promise promise) { + final boolean locationPermissionGranted = PermissionUtils.isLocationPermissionGranted(context); + if (!locationPermissionGranted) { + promise.reject(ConnectErrorCodes.locationPermissionMissing.toString(), "Location permission (ACCESS_FINE_LOCATION) is not granted"); + return false; + } + + final boolean isLocationOn = LocationUtils.isLocationOn(context); + if (!isLocationOn) { + promise.reject(ConnectErrorCodes.locationServicesOff.toString(), "Location service is turned off"); + return false; + } + + return true; + } }