Skip to content

Commit

Permalink
feat: Add loadWifiList meaningfull Exceptions (#81)
Browse files Browse the repository at this point in the history
Co-authored-by: Elias Lecomte <[email protected]>
  • Loading branch information
eliaslecomte and Elias Lecomte authored May 27, 2020
1 parent 1ba6e99 commit cc8f7c1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ Returns a list of nearby WiFI networks.
* `level`: The detected signal level in dBm, also known as the RSSI.
* `timestamp`: timestamp in microseconds (since boot) when this result was last seen.

#### Errors:
* `locationPermissionMissing`: Starting android 6, location permission needs to be granted for wifi
* `locationServicesOff`: Starting Android 6, location services needs to be on to scan for wifi networks.
* `jsonParsingException`: Json parsing exception while parsing the result.
* `illegalViewOperationException`: An exception caused by JS requesting the UI manager to perform an illegal view operation.

### `reScanAndLoadWifiList(): Promise<Array<string>>`
Similar to `loadWifiList` but it forcefully starts a new WiFi scan and only passes the results when the scan is done.

Expand Down Expand Up @@ -283,4 +289,4 @@ If you are connected to that network, it will disconnect.

## Conventions

* Anuglar JS Git Commit conventions are used, read more: https://gist.github.com/stephenparish/9941e89d80e2bc58a153#recognizing-unimportant-commits
* Anuglar JS Git Commit conventions are used, read more: https://gist.github.com/stephenparish/9941e89d80e2bc58a153#recognizing-unimportant-commits
18 changes: 16 additions & 2 deletions android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.reactlibrary.rnwifi.errors.LoadWifiListErrorCodes;
import com.reactlibrary.rnwifi.receivers.WifiScanResultReceiver;
import com.reactlibrary.utils.LocationUtils;
import com.reactlibrary.utils.PermissionUtils;
Expand Down Expand Up @@ -45,6 +46,7 @@ public class RNWifiModule extends ReactContextBaseJavaModule {
this.context = context;
}

@NonNull
@Override
public String getName() {
return "WifiManager";
Expand All @@ -55,6 +57,18 @@ 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 is not granted");
return;
}

final boolean isLocationOn = LocationUtils.isLocationOn(context);
if (!isLocationOn) {
promise.reject(LoadWifiListErrorCodes.locationServicesOff.toString(), "Location service is turned off");
return;
}

try {
final List<ScanResult> results = wifi.getScanResults();
final JSONArray wifiArray = new JSONArray();
Expand All @@ -70,7 +84,7 @@ public void loadWifiList(final Promise promise) {
wifiObject.put("level", result.level);
wifiObject.put("timestamp", result.timestamp);
} catch (final JSONException jsonException) {
promise.reject("jsonException", jsonException.getMessage());
promise.reject(LoadWifiListErrorCodes.jsonParsingException.toString(), jsonException.getMessage());
return;
}
wifiArray.put(wifiObject);
Expand All @@ -79,7 +93,7 @@ public void loadWifiList(final Promise promise) {

promise.resolve(wifiArray.toString());
} catch (final IllegalViewOperationException illegalViewOperationException) {
promise.reject("exception", illegalViewOperationException.getMessage());
promise.reject(LoadWifiListErrorCodes.illegalViewOperationException.toString(), illegalViewOperationException.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.reactlibrary.rnwifi.errors;

public enum LoadWifiListErrorCodes {
/**
* Starting android 6, location permission needs to be granted for wifi scanning.
*/
locationPermissionMissing,
/**
* Starting Android 6, location services needs to be on to scan for wifi networks.
*/
locationServicesOff,
/**
* Json parsing exception while parsing the result.
*/
jsonParsingException,
/**
* An exception caused by JS requesting the UI manager to perform an illegal view operation.
*/
illegalViewOperationException,
}
19 changes: 19 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ declare module 'react-native-wifi-reborn' {
timestamp: number;
}

export enum LOAD_WIFI_LIST_ERRORS {
/**
* Starting android 6, location permission needs to be granted for wifi scanning.
*/
locationPermissionMissing = 'locationPermissionMissing',
/**
* Starting Android 6, location services needs to be on to scan for wifi networks.
*/
locationServicesOff = 'locationServicesOff',
/**
* Json parsing exception while parsing the result.
*/
jsonParsingException = 'jsonParsingException',
/**
* An exception caused by JS requesting the UI manager to perform an illegal view operation.
*/
illegalViewOperationException = 'illegalViewOperationException',
}

/**
* Returns a list of nearby WiFI networks.
*
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,11 @@ import { NativeModules } from 'react-native';

const { WifiManager } = NativeModules;

export const LOAD_WIFI_LIST_ERRORS = {
locationPermissionMissing: 'locationPermissionMissing',
locationServicesOff: 'locationServicesOff',
jsonParsingException: 'jsonParsingException',
illegalViewOperationException: 'illegalViewOperationException',
};

export default WifiManager;

0 comments on commit cc8f7c1

Please sign in to comment.