Skip to content

Commit

Permalink
fix: loadWifiList and reScanAndLoadWifiList not returning a JSON arra…
Browse files Browse the repository at this point in the history
…y but a string (#104)

Co-authored-by: Elias Lecomte <elias.lecomte@inthepocket.com>
  • Loading branch information
eliaslecomte and Elias Lecomte authored Aug 25, 2020
1 parent 4f76b75 commit 9dce618
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 70 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ Returns a list of nearby WiFI networks.
#### 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.
* `exception`: Any other caught exception.

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

### `isEnabled(): Promise<boolean>`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.reactlibrary.mappers;

import android.net.wifi.ScanResult;

import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeArray;
import com.facebook.react.bridge.WritableNativeMap;

import java.util.List;

public class WifiScanResultsMapper {
private WifiScanResultsMapper() {
}

public static WritableArray mapWifiScanResults(final List<ScanResult> scanResults) {
final WritableArray wifiArray = new WritableNativeArray();

for (ScanResult result : scanResults) {
final WritableMap wifiObject = new WritableNativeMap();
if (!result.SSID.equals("")) {
wifiObject.putString("SSID", result.SSID);
wifiObject.putString("BSSID", result.BSSID);
wifiObject.putString("capabilities", result.capabilities);
wifiObject.putInt("frequency", result.frequency);
wifiObject.putInt("level", result.level);
wifiObject.putDouble("timestamp", result.timestamp);
wifiArray.pushMap(wifiObject);
}
}

return wifiArray;
}
}
39 changes: 9 additions & 30 deletions android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import android.net.wifi.WifiManager;
import android.os.Build;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.bridge.WritableArray;
import com.reactlibrary.rnwifi.errors.IsRemoveWifiNetworkErrorCodes;
import com.reactlibrary.rnwifi.errors.LoadWifiListErrorCodes;
import com.reactlibrary.rnwifi.receivers.WifiScanResultReceiver;
Expand All @@ -27,13 +29,9 @@
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionErrorCode;
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import androidx.annotation.NonNull;
import static com.reactlibrary.mappers.WifiScanResultsMapper.mapWifiScanResults;

public class RNWifiModule extends ReactContextBaseJavaModule {
private final WifiManager wifi;
Expand Down Expand Up @@ -71,30 +69,11 @@ public void loadWifiList(final Promise promise) {
}

try {
final List<ScanResult> results = wifi.getScanResults();
final JSONArray wifiArray = new JSONArray();

for (ScanResult result : results) {
final JSONObject wifiObject = new JSONObject();
if (!result.SSID.equals("")) {
try {
wifiObject.put("SSID", result.SSID);
wifiObject.put("BSSID", result.BSSID);
wifiObject.put("capabilities", result.capabilities);
wifiObject.put("frequency", result.frequency);
wifiObject.put("level", result.level);
wifiObject.put("timestamp", result.timestamp);
} catch (final JSONException jsonException) {
promise.reject(LoadWifiListErrorCodes.jsonParsingException.toString(), jsonException.getMessage());
return;
}
wifiArray.put(wifiObject);
}
}

promise.resolve(wifiArray.toString());
} catch (final IllegalViewOperationException illegalViewOperationException) {
promise.reject(LoadWifiListErrorCodes.illegalViewOperationException.toString(), illegalViewOperationException.getMessage());
final List<ScanResult> scanResults = wifi.getScanResults();
final WritableArray results = mapWifiScanResults(scanResults);
promise.resolve(results);
} catch (final Exception exception) {
promise.reject(LoadWifiListErrorCodes.exception.toString(), exception.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ public enum LoadWifiListErrorCodes {
*/
locationServicesOff,
/**
* Json parsing exception while parsing the result.
* Any other caught exception.
*/
jsonParsingException,
/**
* An exception caused by JS requesting the UI manager to perform an illegal view operation.
*/
illegalViewOperationException,
exception,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;

import com.facebook.react.bridge.Promise;
import com.facebook.react.uimanager.IllegalViewOperationException;
import androidx.annotation.NonNull;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.WritableArray;

import java.util.List;

import androidx.annotation.NonNull;
import static com.reactlibrary.mappers.WifiScanResultsMapper.mapWifiScanResults;

public class WifiScanResultReceiver extends BroadcastReceiver {
private final WifiManager wifiManager;
Expand All @@ -31,30 +29,11 @@ public WifiScanResultReceiver(@NonNull final WifiManager wifiManager, @NonNull f
public void onReceive(final Context context, final Intent intent) {
context.unregisterReceiver(this);
try {
final List<ScanResult> results = this.wifiManager.getScanResults();
final JSONArray wifiArray = new JSONArray();

for (ScanResult result : results) {
JSONObject wifiObject = new JSONObject();
if (!result.SSID.equals("")) {
try {
wifiObject.put("SSID", result.SSID);
wifiObject.put("BSSID", result.BSSID);
wifiObject.put("capabilities", result.capabilities);
wifiObject.put("frequency", result.frequency);
wifiObject.put("level", result.level);
wifiObject.put("timestamp", result.timestamp);
} catch (final JSONException jsonException) {
promise.reject("jsonException", jsonException.getMessage());
return;
}
wifiArray.put(wifiObject);
}
}
promise.resolve(wifiArray.toString());
} catch (IllegalViewOperationException illegalViewOperationException) {
promise.reject("exception", illegalViewOperationException.getMessage());
final List<ScanResult> scanResults = this.wifiManager.getScanResults();
final WritableArray results = mapWifiScanResults(scanResults);
promise.resolve(results);
} catch (Exception exception) {
promise.reject("exception", exception.getMessage());
}
}

}
}

0 comments on commit 9dce618

Please sign in to comment.