Skip to content

Commit

Permalink
fix(Android): * add location permission check for getCurrentWifiSSID …
Browse files Browse the repository at this point in the history
…and reScanAndLoadWifiList and change error message for clarity (#264)

* add location permission check for getCurrentWifiSSID and reScanAndLoadWifiList and change error message for clarity

* update method name
  • Loading branch information
JohnWilliamForcier-Spiria authored May 26, 2023
1 parent 98828a0 commit c3ba4e5
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c3ba4e5

Please sign in to comment.