Skip to content

Commit

Permalink
feat(promise): use a promise instead of callsbacks for isRemoveWifiNe…
Browse files Browse the repository at this point in the history
…twork
  • Loading branch information
Elias Lecomte committed Mar 19, 2020
1 parent 0b9349f commit abb3be2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ Called when the network status is resolved. It contains a boolean argument
### `getIP`

### `isRemoveWifiNetwork`
<details>
<summary>isRemoveWifiNetwork(ssid: String): Promise<boolean></summary>
This method will remove the wifi network configuration.
If you are connected to that network, it will disconnect.
</details>

### forceWifiUsage(useWifi: bool)

Expand Down
18 changes: 10 additions & 8 deletions android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,26 @@ public void getIP(final Callback callback) {
}

/**
* This method will remove the wifi network as per the passed SSID from the device list
* This method will remove the wifi network configuration.
* If you are connected to that network, it will disconnect.
*
* @param ssid
* @param callback
* @param SSID wifi SSID to remove configuration for
*/
@ReactMethod
public void isRemoveWifiNetwork(String ssid, final Callback callback) {
List<WifiConfiguration> mWifiConfigList = wifi.getConfiguredNetworks();
public void isRemoveWifiNetwork(final String SSID, final Promise promise) {
final List<WifiConfiguration> mWifiConfigList = wifi.getConfiguredNetworks();
final String comparableSSID = ('"' + SSID + '"'); //Add quotes because wifiConfig.SSID has them

for (WifiConfiguration wifiConfig : mWifiConfigList) {
String comparableSSID = ('"' + ssid + '"'); //Add quotes because wifiConfig.SSID has them
if (wifiConfig.SSID.equals(comparableSSID)) {
wifi.removeNetwork(wifiConfig.networkId);
wifi.saveConfiguration();
callback.invoke(true);
promise.resolve(true);
return;
}
}
callback.invoke(false);

promise.resolve(false);
}

/**
Expand Down
8 changes: 7 additions & 1 deletion lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ declare module 'react-native-wifi-reborn' {
*/
export function connectionStatus(callback: (isConnected: boolean) => void): void;
export function disconnect(): void;
export function isRemoveWifiNetwork(SSID: string): Promise<void>;
/**
* This method will remove the wifi network configuration.
* If you are connected to that network, it will disconnect.
*
* @param SSID wifi SSID to remove configuration for
*/
export function isRemoveWifiNetwork(SSID: string): Promise<boolean>;
/**
* Force wifi usage if the user needs to send requests via WiFi
* if it does not have internet connection. Useful for IoT applications, when
Expand Down

0 comments on commit abb3be2

Please sign in to comment.