Skip to content

Commit

Permalink
feat(Android): added feature to connect to hidden wifi on android 10 (#…
Browse files Browse the repository at this point in the history
…287)

* added feature to connect to hidden wifi on android 10

* added isHidden to ts types
  • Loading branch information
gitisaac authored May 26, 2023
1 parent 15a5ad3 commit 32b5db2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 7 additions & 6 deletions android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,11 @@ public void setEnabled(final boolean enabled) {
* @param SSID name of the network to connect with
* @param password password of the network to connect with
* @param isWep only for iOS
* @param isHidden only for android, use if WiFi is hidden
* @param promise to send success/error feedback
*/
@ReactMethod
public void connectToProtectedSSID(@NonNull final String SSID, @NonNull final String password, final boolean isWep, final Promise promise) {
public void connectToProtectedSSID(@NonNull final String SSID, @NonNull final String password, final boolean isWep, final boolean isHidden, final Promise promise) {
if(!assertLocationPermissionGranted(promise)) {
return;
}
Expand All @@ -210,9 +211,8 @@ public void connectToProtectedSSID(@NonNull final String SSID, @NonNull final St
return;
}


this.removeWifiNetwork(SSID, promise, () -> {
connectToWifiDirectly(SSID, password, promise);
connectToWifiDirectly(SSID, password, isHidden, promise);
});
}

Expand Down Expand Up @@ -390,9 +390,9 @@ public void reScanAndLoadWifiList(final Promise promise) {
wifi.startScan();
}

private void connectToWifiDirectly(@NonNull final String SSID, @NonNull final String password, final Promise promise) {
private void connectToWifiDirectly(@NonNull final String SSID, @NonNull final String password, final boolean isHidden, final Promise promise) {
if (isAndroidTenOrLater()) {
connectAndroidQ(SSID, password, promise);
connectAndroidQ(SSID, password, isHidden, promise);
} else {
connectPreAndroidQ(SSID, password, promise);
}
Expand Down Expand Up @@ -429,8 +429,9 @@ private void connectPreAndroidQ(@NonNull final String SSID, @NonNull final Strin
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private void connectAndroidQ(@NonNull final String SSID, @NonNull final String password, final Promise promise) {
private void connectAndroidQ(@NonNull final String SSID, @NonNull final String password, final boolean isHidden, final Promise promise) {
WifiNetworkSpecifier.Builder wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder()
.setIsHiddenSsid(isHidden)
.setSsid(SSID);

if (!isNullOrEmpty(password)) {
Expand Down
4 changes: 3 additions & 1 deletion lib/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ declare module 'react-native-wifi-reborn' {
* @param SSID Wifi name.
* @param password `null` for open networks.
* @param isWep Used on iOS. If `true`, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 personal Wi-Fi network.
* @param isHidden only for android, use if WiFi is hidden
*/
export function connectToProtectedSSID(
SSID: string,
password: string | null,
isWEP: boolean
isWEP: boolean,
isHidden: boolean
): Promise<void>;

export enum GET_CURRENT_WIFI_SSID_ERRRORS {
Expand Down

0 comments on commit 32b5db2

Please sign in to comment.