From cc8f7c13438a1782258c9500135e8a2edb17928c Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Wed, 27 May 2020 10:35:01 +0200 Subject: [PATCH] feat: Add loadWifiList meaningfull Exceptions (#81) Co-authored-by: Elias Lecomte --- README.md | 8 +++++++- .../com/reactlibrary/rnwifi/RNWifiModule.java | 18 +++++++++++++++-- .../rnwifi/errors/LoadWifiListErrorCodes.java | 20 +++++++++++++++++++ lib/types/index.d.ts | 19 ++++++++++++++++++ src/index.js | 7 +++++++ 5 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 android/src/main/java/com/reactlibrary/rnwifi/errors/LoadWifiListErrorCodes.java diff --git a/README.md b/README.md index a44ee89..0935a29 100644 --- a/README.md +++ b/README.md @@ -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>` Similar to `loadWifiList` but it forcefully starts a new WiFi scan and only passes the results when the scan is done. @@ -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 \ No newline at end of file +* Anuglar JS Git Commit conventions are used, read more: https://gist.github.com/stephenparish/9941e89d80e2bc58a153#recognizing-unimportant-commits diff --git a/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java b/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java index aeca6e2..680c1a1 100644 --- a/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java +++ b/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java @@ -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; @@ -45,6 +46,7 @@ public class RNWifiModule extends ReactContextBaseJavaModule { this.context = context; } + @NonNull @Override public String getName() { return "WifiManager"; @@ -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 results = wifi.getScanResults(); final JSONArray wifiArray = new JSONArray(); @@ -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); @@ -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()); } } diff --git a/android/src/main/java/com/reactlibrary/rnwifi/errors/LoadWifiListErrorCodes.java b/android/src/main/java/com/reactlibrary/rnwifi/errors/LoadWifiListErrorCodes.java new file mode 100644 index 0000000..447316b --- /dev/null +++ b/android/src/main/java/com/reactlibrary/rnwifi/errors/LoadWifiListErrorCodes.java @@ -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, +} diff --git a/lib/types/index.d.ts b/lib/types/index.d.ts index c665012..9972b6a 100644 --- a/lib/types/index.d.ts +++ b/lib/types/index.d.ts @@ -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. * diff --git a/src/index.js b/src/index.js index 418080f..02c9ca6 100644 --- a/src/index.js +++ b/src/index.js @@ -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;