From 32b5db2e1ebcdc757a4dec39f1441a4c9586a827 Mon Sep 17 00:00:00 2001 From: Isaac Gonzalez Date: Fri, 26 May 2023 13:37:15 +0200 Subject: [PATCH] feat(Android): added feature to connect to hidden wifi on android 10 (#287) * added feature to connect to hidden wifi on android 10 * added isHidden to ts types --- .../java/com/reactlibrary/rnwifi/RNWifiModule.java | 13 +++++++------ lib/types/index.d.ts | 4 +++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java b/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java index 8ff42a4..87c7588 100644 --- a/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java +++ b/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java @@ -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; } @@ -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); }); } @@ -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); } @@ -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)) { diff --git a/lib/types/index.d.ts b/lib/types/index.d.ts index 34092d4..95c933d 100644 --- a/lib/types/index.d.ts +++ b/lib/types/index.d.ts @@ -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; export enum GET_CURRENT_WIFI_SSID_ERRRORS {