From 29a2912b1390860efe0ceec1d0be06b130524433 Mon Sep 17 00:00:00 2001 From: Giuliano Comugnaro Date: Wed, 7 Feb 2024 10:54:27 +0000 Subject: [PATCH 1/6] Add connectToProtectedSSIDPrefixOnce method for iOS --- CHANGELOG.md | 6 ++++++ README.md | 2 ++ ios/RNWifi.m | 20 +++++++++++++++----- lib/types/index.d.ts | 14 ++++++++++++++ package.json | 2 +- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63e3997d..2ecc2c49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [4.10.2](https://github.com/JuanSeBestia/react-native-wifi-reborn/compare/v4.10.0...v4.10.1) (2024-02-07) + +### Features + +* **iOS:** 🌟 Add connectToProtectedSSIDPrefixOnce method + ## [4.10.1](https://github.com/JuanSeBestia/react-native-wifi-reborn/compare/v4.10.0...v4.10.1) (2023-11-06) diff --git a/README.md b/README.md index d1347681..1f999fc1 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,8 @@ The following methods work only on iOS ### `connectToProtectedSSIDPrefix(SSIDPrefix: string, password: string, isWep: boolean): Promise` +### `connectToProtectedSSIDPrefixOnce(SSIDPrefix: string, password: string, isWep: boolean, joinOnce: boolean): Promise` + Use this function when you want to match a known SSID prefix, but don’t have a full SSID. If the system finds multiple Wi-Fi networks whose SSID string matches the given prefix, it selects the network with the greatest signal strength. #### SSIDPrefix diff --git a/ios/RNWifi.m b/ios/RNWifi.m index 0b3890ae..e2c0542a 100644 --- a/ios/RNWifi.m +++ b/ios/RNWifi.m @@ -87,15 +87,25 @@ + (BOOL)requiresMainQueueSetup } } -RCT_EXPORT_METHOD(connectToProtectedSSIDPrefix:(NSString*)ssid +RCT_EXPORT_METHOD(connectToProtectedSSIDPrefix:(NSString*)ssidPrefix withPassphrase:(NSString*)passphrase isWEP:(BOOL)isWEP resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { + [self connectToProtectedSSIDPrefixOnce:ssidPrefix withPassphrase:passphrase isWEP:isWEP joinOnce:false resolver:resolve rejecter:reject]; +} + +RCT_EXPORT_METHOD(connectToProtectedSSIDPrefixOnce:(NSString*)ssidPrefix + withPassphrase:(NSString*)passphrase + isWEP:(BOOL)isWEP + joinOnce:(BOOL)joinOnce + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { + if (@available(iOS 13.0, *)) { - NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSIDPrefix:ssid passphrase:passphrase isWEP:isWEP]; - configuration.joinOnce = false; + NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc] initWithSSIDPrefix:ssidPrefix passphrase:passphrase isWEP:isWEP]; + configuration.joinOnce = joinOnce; [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) { if (error != nil) { @@ -103,10 +113,10 @@ + (BOOL)requiresMainQueueSetup } else { // Verify SSID connection [self getWifiSSID:^(NSString* result) { - if ([result hasPrefix:ssid]){ + if ([result hasPrefix:ssidPrefix]){ resolve(nil); } else { - reject([ConnectError code:UnableToConnect], [NSString stringWithFormat:@"%@/%@", @"Unable to connect to Wi-Fi with prefix ", ssid], nil); + reject([ConnectError code:UnableToConnect], [NSString stringWithFormat:@"%@/%@", @"Unable to connect to Wi-Fi with prefix ", ssidPrefix], nil); } }]; } diff --git a/lib/types/index.d.ts b/lib/types/index.d.ts index fdbc25f3..0689a079 100644 --- a/lib/types/index.d.ts +++ b/lib/types/index.d.ts @@ -137,6 +137,20 @@ declare module 'react-native-wifi-reborn' { password: string, isWEP: boolean ): Promise; + /** + * Connects to a WiFi network that start with SSIDPrefix. Rejects with an error if it couldn't connect. + * + * @param SSIDPrefix Wifi name prefix. + * @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 joinOnce Used on iOS. If `true`, restricts the lifetime of a configuration to the operating status of the app that created it. + */ + export function connectToProtectedSSIDPrefixOnce( + SSIDPrefix: string, + password: string | null, + isWEP: boolean, + joinOnce: boolean + ): Promise; //#endregion diff --git a/package.json b/package.json index 4c42d46b..99e824cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-wifi-reborn", - "version": "4.10.1", + "version": "4.10.2", "description": "A react-native implementation for viewing and connecting to Wifi networks on Android and iOS devices.", "types": "lib/types/index.d.ts", "main": "src/index.js", From 234c9eba60cbd73e91f89b34e8608402940002b0 Mon Sep 17 00:00:00 2001 From: Giuliano Comugnaro Date: Thu, 28 Mar 2024 15:59:47 +0000 Subject: [PATCH 2/6] Merge branch 'master' of github.com:notjulian/react-native-wifi-reborn --- .tool-versions | 6 +- CHANGELOG.md | 23 +- README.md | 19 +- .../com/reactlibrary/rnwifi/RNWifiModule.java | 112 +- example/RNWifiReborn/.buckconfig | 6 - example/RNWifiReborn/.bundle/config | 2 + example/RNWifiReborn/.eslintrc.js | 2 +- example/RNWifiReborn/.flowconfig | 75 - example/RNWifiReborn/.gitattributes | 1 - example/RNWifiReborn/.gitignore | 27 +- example/RNWifiReborn/.prettierrc.js | 3 +- example/RNWifiReborn/.watchmanconfig | 2 +- example/RNWifiReborn/App.js | 159 - example/RNWifiReborn/App.tsx | 40 + example/RNWifiReborn/Gemfile | 9 + example/RNWifiReborn/Gemfile.lock | 103 + example/RNWifiReborn/README.md | 79 + .../__tests__/{App-test.js => App.test.tsx} | 3 + example/RNWifiReborn/android/app/BUCK | 55 - example/RNWifiReborn/android/app/build.gradle | 129 +- .../RNWifiReborn/android/app/build_defs.bzl | 19 - .../android/app/src/debug/AndroidManifest.xml | 7 +- .../android/app/src/main/AndroidManifest.xml | 15 +- .../java/com/rnwifireborn/MainActivity.java | 15 - .../java/com/rnwifireborn/MainActivity.kt | 22 + .../com/rnwifireborn/MainApplication.java | 74 - .../java/com/rnwifireborn/MainApplication.kt | 45 + .../res/drawable/rn_edit_text_material.xml | 36 + .../app/src/main/res/values/styles.xml | 4 +- example/RNWifiReborn/android/build.gradle | 37 +- .../RNWifiReborn/android/gradle.properties | 24 +- .../android/gradle/wrapper/gradle-wrapper.jar | Bin 55616 -> 63721 bytes .../gradle/wrapper/gradle-wrapper.properties | 4 +- example/RNWifiReborn/android/gradlew | 287 +- example/RNWifiReborn/android/gradlew.bat | 40 +- example/RNWifiReborn/android/settings.gradle | 1 + example/RNWifiReborn/app.json | 2 +- example/RNWifiReborn/babel.config.js | 2 +- .../RNWifiReborn/components/ConnectToSSID.tsx | 60 + .../RNWifiReborn/components/CurrentSSID.tsx | 32 + .../RNWifiReborn/components/Disconnect.tsx | 34 + example/RNWifiReborn/components/Section.tsx | 29 + example/RNWifiReborn/ios/.xcode.env | 11 + example/RNWifiReborn/ios/Podfile | 111 +- example/RNWifiReborn/ios/Podfile.lock | 1470 +- .../ios/RNWifiReborn-tvOS/Info.plist | 53 - .../ios/RNWifiReborn-tvOSTests/Info.plist | 24 - .../RNWifiReborn.xcodeproj/project.pbxproj | 544 +- .../xcschemes/RNWifiReborn-tvOS.xcscheme | 129 - .../xcschemes/RNWifiReborn.xcscheme | 45 +- .../xcshareddata/IDEWorkspaceChecks.plist | 8 - .../ios/RNWifiReborn/AppDelegate.h | 13 +- .../ios/RNWifiReborn/AppDelegate.m | 42 - .../ios/RNWifiReborn/AppDelegate.mm | 31 + .../RNWifiReborn/Base.lproj/LaunchScreen.xib | 42 - .../AppIcon.appiconset/Contents.json | 45 +- .../RNWifiReborn/ios/RNWifiReborn/Info.plist | 12 +- .../ios/RNWifiReborn/LaunchScreen.storyboard | 47 + .../RNWifiReborn/RNWifiReborn.entitlements | 12 + example/RNWifiReborn/ios/RNWifiReborn/main.m | 10 +- .../ios/RNWifiRebornTests/RNWifiRebornTests.m | 36 +- example/RNWifiReborn/jest.config.js | 3 + example/RNWifiReborn/metro.config.js | 42 +- example/RNWifiReborn/package-lock.json | 49839 ---------------- example/RNWifiReborn/package.json | 38 +- example/RNWifiReborn/tsconfig.json | 3 + example/RNWifiReborn/yarn.lock | 6636 ++ example/with-expo/.gitignore | 28 +- example/with-expo/.prettierrc | 7 - example/with-expo/App.tsx | 1 + example/with-expo/android/app/build.gradle | 103 +- .../android/app/src/debug/AndroidManifest.xml | 2 +- .../rnwifi/withexpo/ReactNativeFlipper.java | 75 - .../android/app/src/main/AndroidManifest.xml | 10 +- .../com/rnwifi/withexpo/MainActivity.java | 68 - .../java/com/rnwifi/withexpo/MainActivity.kt | 61 + .../com/rnwifi/withexpo/MainApplication.java | 76 - .../com/rnwifi/withexpo/MainApplication.kt | 65 + .../res/drawable/rn_edit_text_material.xml | 2 +- .../rnwifi/withexpo/ReactNativeFlipper.java | 20 - example/with-expo/android/build.gradle | 22 +- example/with-expo/android/gradle.properties | 15 +- .../android/gradle/wrapper/gradle-wrapper.jar | Bin 60756 -> 63721 bytes .../gradle/wrapper/gradle-wrapper.properties | 4 +- example/with-expo/android/gradlew | 35 +- example/with-expo/android/settings.gradle | 12 +- .../with-expo/components/ConnectToSSID.tsx | 8 +- example/with-expo/components/CurrentSSID.tsx | 2 +- example/with-expo/components/Disconnect.tsx | 2 +- example/with-expo/index.js | 8 - example/with-expo/ios/Podfile | 12 +- example/with-expo/ios/Podfile.lock | 1371 +- example/with-expo/ios/Podfile.properties.json | 3 +- .../ios/withexpo.xcodeproj/project.pbxproj | 61 +- example/with-expo/ios/withexpo/AppDelegate.mm | 17 +- ...twork@2x.png => App-Icon-1024x1024@1x.png} | Bin .../AppIcon.appiconset/App-Icon-20x20@1x.png | Bin 583 -> 0 bytes .../AppIcon.appiconset/App-Icon-20x20@2x.png | Bin 1562 -> 0 bytes .../AppIcon.appiconset/App-Icon-20x20@3x.png | Bin 2642 -> 0 bytes .../AppIcon.appiconset/App-Icon-29x29@1x.png | Bin 1028 -> 0 bytes .../AppIcon.appiconset/App-Icon-29x29@2x.png | Bin 2492 -> 0 bytes .../AppIcon.appiconset/App-Icon-29x29@3x.png | Bin 4307 -> 0 bytes .../AppIcon.appiconset/App-Icon-40x40@1x.png | Bin 1562 -> 0 bytes .../AppIcon.appiconset/App-Icon-40x40@2x.png | Bin 3806 -> 0 bytes .../AppIcon.appiconset/App-Icon-40x40@3x.png | Bin 6565 -> 0 bytes .../AppIcon.appiconset/App-Icon-60x60@2x.png | Bin 6565 -> 0 bytes .../AppIcon.appiconset/App-Icon-60x60@3x.png | Bin 10430 -> 0 bytes .../AppIcon.appiconset/App-Icon-76x76@1x.png | Bin 3499 -> 0 bytes .../AppIcon.appiconset/App-Icon-76x76@2x.png | Bin 8492 -> 0 bytes .../App-Icon-83.5x83.5@2x.png | Bin 9469 -> 0 bytes .../AppIcon.appiconset/Contents.json | 116 +- example/with-expo/ios/withexpo/Info.plist | 12 +- .../ios/withexpo/Supporting/Expo.plist | 4 + .../ios/withexpo/withexpo-Bridging-Header.h | 3 + example/with-expo/package-lock.json | 25810 +++----- example/with-expo/package.json | 22 +- ios/RNWifi.m | 55 +- lib/types/index.d.ts | 20 + package-lock.json | 4 +- package.json | 2 +- 120 files changed, 17593 insertions(+), 71435 deletions(-) delete mode 100644 example/RNWifiReborn/.buckconfig create mode 100644 example/RNWifiReborn/.bundle/config delete mode 100644 example/RNWifiReborn/.flowconfig delete mode 100644 example/RNWifiReborn/.gitattributes delete mode 100644 example/RNWifiReborn/App.js create mode 100644 example/RNWifiReborn/App.tsx create mode 100644 example/RNWifiReborn/Gemfile create mode 100644 example/RNWifiReborn/Gemfile.lock create mode 100644 example/RNWifiReborn/README.md rename example/RNWifiReborn/__tests__/{App-test.js => App.test.tsx} (73%) delete mode 100644 example/RNWifiReborn/android/app/BUCK delete mode 100644 example/RNWifiReborn/android/app/build_defs.bzl delete mode 100644 example/RNWifiReborn/android/app/src/main/java/com/rnwifireborn/MainActivity.java create mode 100644 example/RNWifiReborn/android/app/src/main/java/com/rnwifireborn/MainActivity.kt delete mode 100644 example/RNWifiReborn/android/app/src/main/java/com/rnwifireborn/MainApplication.java create mode 100644 example/RNWifiReborn/android/app/src/main/java/com/rnwifireborn/MainApplication.kt create mode 100644 example/RNWifiReborn/android/app/src/main/res/drawable/rn_edit_text_material.xml create mode 100644 example/RNWifiReborn/components/ConnectToSSID.tsx create mode 100644 example/RNWifiReborn/components/CurrentSSID.tsx create mode 100644 example/RNWifiReborn/components/Disconnect.tsx create mode 100644 example/RNWifiReborn/components/Section.tsx create mode 100644 example/RNWifiReborn/ios/.xcode.env delete mode 100644 example/RNWifiReborn/ios/RNWifiReborn-tvOS/Info.plist delete mode 100644 example/RNWifiReborn/ios/RNWifiReborn-tvOSTests/Info.plist delete mode 100644 example/RNWifiReborn/ios/RNWifiReborn.xcodeproj/xcshareddata/xcschemes/RNWifiReborn-tvOS.xcscheme delete mode 100644 example/RNWifiReborn/ios/RNWifiReborn.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist delete mode 100644 example/RNWifiReborn/ios/RNWifiReborn/AppDelegate.m create mode 100644 example/RNWifiReborn/ios/RNWifiReborn/AppDelegate.mm delete mode 100644 example/RNWifiReborn/ios/RNWifiReborn/Base.lproj/LaunchScreen.xib create mode 100644 example/RNWifiReborn/ios/RNWifiReborn/LaunchScreen.storyboard create mode 100644 example/RNWifiReborn/ios/RNWifiReborn/RNWifiReborn.entitlements create mode 100644 example/RNWifiReborn/jest.config.js delete mode 100644 example/RNWifiReborn/package-lock.json create mode 100644 example/RNWifiReborn/tsconfig.json create mode 100644 example/RNWifiReborn/yarn.lock delete mode 100644 example/with-expo/.prettierrc delete mode 100644 example/with-expo/android/app/src/debug/java/com/rnwifi/withexpo/ReactNativeFlipper.java delete mode 100644 example/with-expo/android/app/src/main/java/com/rnwifi/withexpo/MainActivity.java create mode 100644 example/with-expo/android/app/src/main/java/com/rnwifi/withexpo/MainActivity.kt delete mode 100644 example/with-expo/android/app/src/main/java/com/rnwifi/withexpo/MainApplication.java create mode 100644 example/with-expo/android/app/src/main/java/com/rnwifi/withexpo/MainApplication.kt delete mode 100644 example/with-expo/android/app/src/release/java/com/rnwifi/withexpo/ReactNativeFlipper.java delete mode 100644 example/with-expo/index.js rename example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/{ItunesArtwork@2x.png => App-Icon-1024x1024@1x.png} (100%) delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-20x20@1x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-20x20@2x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-20x20@3x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-29x29@1x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-29x29@2x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-29x29@3x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-40x40@1x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-40x40@2x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-40x40@3x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-60x60@2x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-60x60@3x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-76x76@1x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-76x76@2x.png delete mode 100644 example/with-expo/ios/withexpo/Images.xcassets/AppIcon.appiconset/App-Icon-83.5x83.5@2x.png create mode 100644 example/with-expo/ios/withexpo/withexpo-Bridging-Header.h diff --git a/.tool-versions b/.tool-versions index 6841e4c2..1a7fc0c3 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,3 +1,3 @@ -nodejs 16.18.0 -ruby 2.7.4 -java zulu-11.52.13 +nodejs 18.17.0 +ruby 3.2.2 +java oracle-17 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ecc2c49..f680d599 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,27 @@ -## [4.10.2](https://github.com/JuanSeBestia/react-native-wifi-reborn/compare/v4.10.0...v4.10.1) (2024-02-07) +# [4.12.0](https://github.com/JuanSeBestia/react-native-wifi-reborn/compare/v4.11.0...v4.12.0) (2024-03-26) + + +### Bug Fixes + +* **android:** re-add onLost method ([be1d872](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/be1d872d3be8c9ed84a92ea9f7c0ab20935ea388)) +* IOS method ([b5bdb35](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/b5bdb35f5c51c35e2b8a4d50a9474cd396e40407)) +* removed assert password != null ([93d737a](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/93d737aef7dd5a1b1d2dcb0e5bbf2bc796c05138)) +* use enum for error ([7b7587c](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/7b7587c27ff8b4544a4b05cb62e93740188cce2c)) + + +### Features + +* add new method connectToProtectedWifiSSID ([1813073](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/1813073561a837e3a7a5eedd413fa76f003702ae)) +* add nullable password ([75ff05f](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/75ff05fd37185c2e2b5302a2c1e373447e0e4f2d)) +* **android:** add timeout parameter ([9426913](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/94269130b3ac7589accd653b8ac2948083618d72)) +* update README ([8970ba4](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/8970ba47d368e189e9f6af4d2eb060617ecfb872)) + +# [4.11.0](https://github.com/JuanSeBestia/react-native-wifi-reborn/compare/v4.10.1...v4.11.0) (2024-02-12) + ### Features -* **iOS:** 🌟 Add connectToProtectedSSIDPrefixOnce method +* **iOS, Loop patch:** check the connected WiFi network repeatedly until it's the one we requested ([#301](https://github.com/JuanSeBestia/react-native-wifi-reborn/issues/301)) ([e7088be](https://github.com/JuanSeBestia/react-native-wifi-reborn/commit/e7088be14546c2da79e225aad1295e32728bb558)) ## [4.10.1](https://github.com/JuanSeBestia/react-native-wifi-reborn/compare/v4.10.0...v4.10.1) (2023-11-06) diff --git a/README.md b/README.md index 1f999fc1..edff93bc 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,7 @@ The plugin provides props for extra customization. Every time you change the pro ```javascript import WifiManager from "react-native-wifi-reborn"; -WifiManager.connectToProtectedSSID(ssid, password, isWep).then( +WifiManager.connectToProtectedWifiSSID(ssid, password, isWep).then( () => { console.log("Connected successfully!"); }, @@ -171,6 +171,17 @@ _The api documentation is in progress._ The following methods work on both Android and iOS +### ```### NEW VERSION WITH OPTIONAL PARAMETERS ###``` +``` +connectToProtectedWifiSSID({ + ssid: string; + password: string | null; + isWEP?: boolean; + isHidden?: boolean; + timeout?: number + ;}): Promise +``` + ### `connectToProtectedSSID(SSID: string, password: string, isWEP: boolean, isHidden: boolean): Promise` Returns a promise that resolves when connected or rejects with the error when it couldn't connect to the wifi network. @@ -197,6 +208,10 @@ Used on iOS. If true, the network is WEP Wi-Fi; otherwise it is a WPA or WPA2 pe Type: `boolean` Used on Android. If true, the network is a hidden Wi-Fi network. +#### timeout - ```ONLY NEW VERSION``` +TypeL `number` +Used on Android to set a timeout in seconds. Default 15 seconds. + #### Errors: * iOS: * `unavailableForOSVersion`: Starting from iOS 11, NEHotspotConfigurationError is available. @@ -216,7 +231,7 @@ Used on Android. If true, the network is a hidden Wi-Fi network. * `didNotFindNetwork`: If the wifi network is not in range, the security type is unknown and WifiUtils doesn't support connecting to the network. * `authenticationErrorOccurred`: Authentication error occurred while trying to connect. The password could be incorrect or the user could have a saved network configuration with a different password! * `android10ImmediatelyDroppedConnection` : Firmware bugs on OnePlus prevent it from connecting on some firmware versions. More info: https://github.com/ThanosFisherman/WifiUtils/issues/63. - * `timeoutOccurred`: Could not connect in the timeout window. + * `timeoutOccurred`: Could not connect in the timeout window. - ```ONLY NEW VERSION``` * Both: * `unableToConnect`: When an unknown error occurred. diff --git a/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java b/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java index b84a9148..88bd0d3a 100644 --- a/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java +++ b/android/src/main/java/com/reactlibrary/rnwifi/RNWifiModule.java @@ -18,6 +18,8 @@ import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiNetworkSpecifier; +import android.os.Handler; +import android.os.Looper; import android.util.Log; import android.provider.Settings; import android.os.Build; @@ -56,6 +58,8 @@ public class RNWifiModule extends ReactContextBaseJavaModule { private final ReactApplicationContext context; private static String TAG = "RNWifiModule"; + private static final int TIMEOUT_MILLIS = 15000; + RNWifiModule(ReactApplicationContext context) { super(context); @@ -208,6 +212,7 @@ public void openWifiSettings() { this.context.startActivity(intent); } + /** * Use this to connect with a wifi network. * Example: wifi.findAndConnect(ssid, password, false); @@ -221,7 +226,7 @@ public void openWifiSettings() { */ @ReactMethod public void connectToProtectedSSID(@NonNull final String SSID, @NonNull final String password, final boolean isWep, final boolean isHidden, final Promise promise) { - if(!assertLocationPermissionGranted(promise)) { + if(!assertLocationPermissionGranted(promise)) { return; } @@ -231,11 +236,45 @@ public void connectToProtectedSSID(@NonNull final String SSID, @NonNull final St } this.removeWifiNetwork(SSID, promise, () -> { - connectToWifiDirectly(SSID, password, isHidden, promise); + connectToWifiDirectly(SSID, password, isHidden, TIMEOUT_MILLIS, promise); + }); + } + + + /** + * Use this to connect with a wifi network. + * Example: wifi.findAndConnect(ssid, password, false); + * The promise will resolve with the message 'connected' when the user is connected on Android. + * + * @param options to connect with a wifi network + * @param promise to send success/error feedback + */ + @ReactMethod + public void connectToProtectedWifiSSID(@NonNull ReadableMap options, final Promise promise) { + if(!assertLocationPermissionGranted(promise)) { + return; + } + + if (!wifi.isWifiEnabled() && !wifi.setWifiEnabled(true)) { + promise.reject(ConnectErrorCodes.couldNotEnableWifi.toString(), "On Android 10, the user has to enable wifi manually."); + return; + } + + String ssid = options.getString("ssid"); + String password = options.getString("password"); + boolean isHidden = options.getBoolean("isHidden"); + int secondsTimeout = options.hasKey("timeout") ? options.getInt("timeout") * 1000 : TIMEOUT_MILLIS; + + + this.removeWifiNetwork(ssid, promise, () -> { + assert ssid != null; + connectToWifiDirectly(ssid, password, isHidden, secondsTimeout, promise); }); } + + /** * Returns if the device is currently connected to a WiFi network. */ @@ -406,7 +445,7 @@ public void reScanAndLoadWifiList(final Promise promise) { boolean wifiStartScan = wifi.startScan(); Log.d(TAG, "wifi start scan: " + wifiStartScan); - if (wifiStartScan == true) { + if (wifiStartScan) { final WifiScanResultReceiver wifiScanResultReceiver = new WifiScanResultReceiver(wifi, promise); getReactApplicationContext().registerReceiver(wifiScanResultReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } else { @@ -415,9 +454,9 @@ public void reScanAndLoadWifiList(final Promise promise) { } } - private void connectToWifiDirectly(@NonNull final String SSID, @NonNull final String password, final boolean isHidden, final Promise promise) { + private void connectToWifiDirectly(@NonNull final String SSID, @NonNull final String password, final boolean isHidden, final int timeout, final Promise promise) { if (isAndroidTenOrLater()) { - connectAndroidQ(SSID, password, isHidden, promise); + connectAndroidQ(SSID, password, isHidden,timeout, promise); } else { connectPreAndroidQ(SSID, password, promise); } @@ -454,7 +493,7 @@ 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 boolean isHidden, final Promise promise) { + private void connectAndroidQ(@NonNull final String SSID, @NonNull final String password, final boolean isHidden, final int timeout, final Promise promise) { WifiNetworkSpecifier.Builder wifiNetworkSpecifier = new WifiNetworkSpecifier.Builder() .setIsHiddenSsid(isHidden) .setSsid(SSID); @@ -470,34 +509,45 @@ private void connectAndroidQ(@NonNull final String SSID, @NonNull final String p .build(); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + + final Handler timeoutHandler = new Handler(Looper.getMainLooper()); + final Runnable timeoutRunnable = () -> { + promise.reject(ConnectErrorCodes.timeoutOccurred.toString(), "Connection timeout"); + DisconnectCallbackHolder.getInstance().unbindProcessFromNetwork(); + DisconnectCallbackHolder.getInstance().disconnect(); + }; - ConnectivityManager.NetworkCallback networkCallback = new - ConnectivityManager.NetworkCallback() { - @Override - public void onAvailable(Network network) { - super.onAvailable(network); - DisconnectCallbackHolder.getInstance().bindProcessToNetwork(network); - connectivityManager.setNetworkPreference(ConnectivityManager.DEFAULT_NETWORK_PREFERENCE); - if (!pollForValidSSID(3, SSID)) { - promise.reject(ConnectErrorCodes.android10ImmediatelyDroppedConnection.toString(), "Firmware bugs on OnePlus prevent it from connecting on some firmware versions."); - return; - } - promise.resolve("connected"); - } + timeoutHandler.postDelayed(timeoutRunnable, timeout); - @Override - public void onUnavailable() { - super.onUnavailable(); - promise.reject(ConnectErrorCodes.didNotFindNetwork.toString(), "Network not found or network request cannot be fulfilled."); - } + ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() { + @Override + public void onAvailable(@NonNull Network network) { + super.onAvailable(network); + timeoutHandler.removeCallbacks(timeoutRunnable); + DisconnectCallbackHolder.getInstance().bindProcessToNetwork(network); + connectivityManager.setNetworkPreference(ConnectivityManager.DEFAULT_NETWORK_PREFERENCE); + if (!pollForValidSSID(3, SSID)) { + promise.reject(ConnectErrorCodes.android10ImmediatelyDroppedConnection.toString(), "Firmware bugs on OnePlus prevent it from connecting on some firmware versions."); + return; + } + promise.resolve("connected"); + } + + @Override + public void onUnavailable() { + super.onUnavailable(); + timeoutHandler.removeCallbacks(timeoutRunnable); + promise.reject(ConnectErrorCodes.didNotFindNetwork.toString(), "Network not found or network request cannot be fulfilled."); + } + + @Override + public void onLost(@NonNull Network network) { + super.onLost(network); + DisconnectCallbackHolder.getInstance().unbindProcessFromNetwork(); + DisconnectCallbackHolder.getInstance().disconnect(); + } + }; - @Override - public void onLost(@NonNull Network network) { - super.onLost(network); - DisconnectCallbackHolder.getInstance().unbindProcessFromNetwork(); - DisconnectCallbackHolder.getInstance().disconnect(); - } - }; DisconnectCallbackHolder.getInstance().addNetworkCallback(networkCallback, connectivityManager); DisconnectCallbackHolder.getInstance().requestNetwork(nr); } diff --git a/example/RNWifiReborn/.buckconfig b/example/RNWifiReborn/.buckconfig deleted file mode 100644 index 934256cb..00000000 --- a/example/RNWifiReborn/.buckconfig +++ /dev/null @@ -1,6 +0,0 @@ - -[android] - target = Google Inc.:Google APIs:23 - -[maven_repositories] - central = https://repo1.maven.org/maven2 diff --git a/example/RNWifiReborn/.bundle/config b/example/RNWifiReborn/.bundle/config new file mode 100644 index 00000000..848943bb --- /dev/null +++ b/example/RNWifiReborn/.bundle/config @@ -0,0 +1,2 @@ +BUNDLE_PATH: "vendor/bundle" +BUNDLE_FORCE_RUBY_PLATFORM: 1 diff --git a/example/RNWifiReborn/.eslintrc.js b/example/RNWifiReborn/.eslintrc.js index 40c6dcd0..187894b6 100644 --- a/example/RNWifiReborn/.eslintrc.js +++ b/example/RNWifiReborn/.eslintrc.js @@ -1,4 +1,4 @@ module.exports = { root: true, - extends: '@react-native-community', + extends: '@react-native', }; diff --git a/example/RNWifiReborn/.flowconfig b/example/RNWifiReborn/.flowconfig deleted file mode 100644 index 4afc766a..00000000 --- a/example/RNWifiReborn/.flowconfig +++ /dev/null @@ -1,75 +0,0 @@ -[ignore] -; We fork some components by platform -.*/*[.]android.js - -; Ignore "BUCK" generated dirs -/\.buckd/ - -; Ignore polyfills -node_modules/react-native/Libraries/polyfills/.* - -; These should not be required directly -; require from fbjs/lib instead: require('fbjs/lib/warning') -node_modules/warning/.* - -; Flow doesn't support platforms -.*/Libraries/Utilities/LoadingView.js - -[untyped] -.*/node_modules/@react-native-community/cli/.*/.* - -[include] - -[libs] -node_modules/react-native/Libraries/react-native/react-native-interface.js -node_modules/react-native/flow/ - -[options] -emoji=true - -esproposal.optional_chaining=enable -esproposal.nullish_coalescing=enable - -module.file_ext=.js -module.file_ext=.json -module.file_ext=.ios.js - -munge_underscores=true - -module.name_mapper='^react-native$' -> '/node_modules/react-native/Libraries/react-native/react-native-implementation' -module.name_mapper='^react-native/\(.*\)$' -> '/node_modules/react-native/\1' -module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> '/node_modules/react-native/Libraries/Image/RelativeImageStub' - -suppress_type=$FlowIssue -suppress_type=$FlowFixMe -suppress_type=$FlowFixMeProps -suppress_type=$FlowFixMeState - -suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(\\)? *\\(site=[a-z,_]*react_native\\(_ios\\)?_\\(oss\\|fb\\)[a-z,_]*\\)?)\\) -suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(\\)? *\\(site=[a-z,_]*react_native\\(_ios\\)?_\\(oss\\|fb\\)[a-z,_]*\\)?)\\)?:? #[0-9]+ -suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError - -[lints] -sketchy-null-number=warn -sketchy-null-mixed=warn -sketchy-number=warn -untyped-type-import=warn -nonstrict-import=warn -deprecated-type=warn -unsafe-getters-setters=warn -inexact-spread=warn -unnecessary-invariant=warn -signature-verification-failure=warn -deprecated-utility=error - -[strict] -deprecated-type -nonstrict-import -sketchy-null -unclear-type -unsafe-getters-setters -untyped-import -untyped-type-import - -[version] -^0.105.0 diff --git a/example/RNWifiReborn/.gitattributes b/example/RNWifiReborn/.gitattributes deleted file mode 100644 index d42ff183..00000000 --- a/example/RNWifiReborn/.gitattributes +++ /dev/null @@ -1 +0,0 @@ -*.pbxproj -text diff --git a/example/RNWifiReborn/.gitignore b/example/RNWifiReborn/.gitignore index ad572e63..0cab2ac6 100644 --- a/example/RNWifiReborn/.gitignore +++ b/example/RNWifiReborn/.gitignore @@ -20,6 +20,7 @@ DerivedData *.hmap *.ipa *.xcuserstate +ios/.xcode.env.local # Android/IntelliJ # @@ -28,6 +29,10 @@ build/ .gradle local.properties *.iml +*.hprof +.cxx/ +*.keystore +!debug.keystore # node.js # @@ -35,12 +40,6 @@ node_modules/ npm-debug.log yarn-error.log -# BUCK -buck-out/ -\.buckd/ -*.keystore -!debug.keystore - # fastlane # # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the @@ -48,12 +47,20 @@ buck-out/ # For more information about the recommended setup visit: # https://docs.fastlane.tools/best-practices/source-control/ -*/fastlane/report.xml -*/fastlane/Preview.html -*/fastlane/screenshots +**/fastlane/report.xml +**/fastlane/Preview.html +**/fastlane/screenshots +**/fastlane/test_output # Bundle artifact *.jsbundle -# CocoaPods +# Ruby / CocoaPods /ios/Pods/ +/vendor/bundle/ + +# Temporary files created by Metro to check the health of the file watcher +.metro-health-check* + +# testing +/coverage diff --git a/example/RNWifiReborn/.prettierrc.js b/example/RNWifiReborn/.prettierrc.js index 5c4de1a4..2b540746 100644 --- a/example/RNWifiReborn/.prettierrc.js +++ b/example/RNWifiReborn/.prettierrc.js @@ -1,6 +1,7 @@ module.exports = { + arrowParens: 'avoid', + bracketSameLine: true, bracketSpacing: false, - jsxBracketSameLine: true, singleQuote: true, trailingComma: 'all', }; diff --git a/example/RNWifiReborn/.watchmanconfig b/example/RNWifiReborn/.watchmanconfig index 9e26dfee..0967ef42 100644 --- a/example/RNWifiReborn/.watchmanconfig +++ b/example/RNWifiReborn/.watchmanconfig @@ -1 +1 @@ -{} \ No newline at end of file +{} diff --git a/example/RNWifiReborn/App.js b/example/RNWifiReborn/App.js deleted file mode 100644 index 36941b56..00000000 --- a/example/RNWifiReborn/App.js +++ /dev/null @@ -1,159 +0,0 @@ -/** - * Sample React Native App - * https://github.com/facebook/react-native - * - * @format - * @flow - */ - -import React, {useState, useEffect} from 'react'; -import { PermissionsAndroid, Button } from 'react-native'; -import { - SafeAreaView, - StyleSheet, - ScrollView, - View, - StatusBar, - Text, -} from 'react-native'; - -import {Header, Colors} from 'react-native/Libraries/NewAppScreen'; -import WifiManager from 'react-native-wifi-reborn'; - -const App = () => { - const [connected, setConnected] = useState({connected: false, ssid: 'S4N'}); - const [ssid, setSsid] = useState(''); - const password ="tanenbaum-1981"; - - const initWifi = async () => { - try { - const ssid = await WifiManager.getCurrentWifiSSID(); - setSsid(ssid); - console.log('Your current connected wifi SSID is ' + ssid); - } catch (error) { - setSsid('Cannot get current SSID!' + error.message); - console.log('Cannot get current SSID!', {error}); - } - } - - const requestLocationPermission = async () => { - try { - const granted = await PermissionsAndroid.request( - PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, - { - title: "React Native Wifi Reborn App Permission", - message: - "Location permission is required to connect with or scan for Wifi networks. ", - buttonNeutral: "Ask Me Later", - buttonNegative: "Cancel", - buttonPositive: "OK" - } - ); - if (granted === PermissionsAndroid.RESULTS.GRANTED) { - initWifi(); - } else { - console.log("Location permission denied"); - } - } catch (err) { - console.warn(err); - } - }; - - const connectWithWifi = async () => { - try { - const data = await WifiManager.connectToProtectedSSID( - ssid, - password, - false, - false, - ); - console.log('Connected successfully!', {data}); - setConnected({connected: true, ssid}); - } catch (error) { - setConnected({connected: false, error: error.message}); - console.log('Connection failed!', {error}); - } - }; - - const scanExample = async () => { - try { - const data = await WifiManager.reScanAndLoadWifiList() - console.log(data); - } catch (error) { - console.log(error); - } - } - - - useEffect(() => { - requestLocationPermission(); - }, []); - - return ( - <> - - - -
- - ssid - - {JSON.stringify(ssid)} - - - - Connected - - {JSON.stringify(connected)} - - -