Skip to content

Commit

Permalink
fix(Android): getCurrentWifiSSID return null if '<unknown ssid>' (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaslecomte committed Jan 10, 2021
1 parent 5403479 commit 336668a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ Used on iOS. If true, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 pe

### `getCurrentWifiSSID(): Promise`

Returns the SSID of the current WiFi network.

#### Errors:
* `CouldNotDetectSSID`: Not connected or connecting.

## Only iOS

The following methods work only on iOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.reactlibrary.rnwifi.errors.DisconnectErrorCodes;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.reactlibrary.rnwifi.errors.GetCurrentWifiSSIDErrorCodes;
import com.reactlibrary.rnwifi.errors.IsRemoveWifiNetworkErrorCodes;
import com.reactlibrary.rnwifi.errors.LoadWifiListErrorCodes;
import com.reactlibrary.rnwifi.receivers.WifiScanResultReceiver;
Expand Down Expand Up @@ -304,6 +305,12 @@ public void getCurrentWifiSSID(final Promise promise) {
ssid = ssid.substring(1, ssid.length() - 1);
}

// Android returns `<unknown ssid>` when it is not connected or still connecting
if (ssid.equals("<unknown ssid>")) {
promise.reject(GetCurrentWifiSSIDErrorCodes.CouldNotDetectSSID.toString(), "Not connected or connecting.");
return;
}

promise.resolve(ssid);
}

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

enum class GetCurrentWifiSSIDErrorCodes {
/**
* Not connected or connecting.
*/
CouldNotDetectSSID,
}
7 changes: 7 additions & 0 deletions lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ declare module 'react-native-wifi-reborn' {
isWEP: boolean
): Promise<void>;

export enum GET_CURRENT_WIFI_SSID_ERRRORS {
CouldNotDetectSSID = 'CouldNotDetectSSID',
}

/**
* Returns the name of the currently connected WiFi. When not connected, the promise will be or null when not connected.
*/
export function getCurrentWifiSSID(): Promise<string>;

//#region iOS only
Expand Down

0 comments on commit 336668a

Please sign in to comment.