Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/ handle connect wifi error state #1189

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

ghassenbenzahra123
Copy link
Contributor

📝 Summary

Description

  • Handle app state on failure to connect to wifi.

Checklist:

  • Coding Standards: I have reviewed my code to ensure it follows the project's coding standards.
  • Testing: I have tested the changes and they work as expected.
  • Merge Conflicts: I have resolved any merge conflicts with the latest main/development branch.
  • Branch Status: The branch is up-to-date with the target branch (main/development).

@@ -35,13 +39,17 @@ class _OnBoardingTimeZoneSelectorState extends State<OnBoardingTimeZoneSelector>
@override
void dispose() {
searchController.dispose();
countryListFocusNode.dispose();
timezoneListFocusNode.dispose();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unnecessary focus it is not used

late List<Country> countriesList;
late List<String> selectedCountryTimezones;
final TextEditingController searchController = TextEditingController();
final FocusNode countryListFocusNode = FocusNode();
final FocusNode timezoneListFocusNode = FocusNode();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unnecessary focus it is not used

Comment on lines +148 to +149
val networkSSID = call.argument<String>("ssid")
val password = call.argument<String>("password")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check if they are null ? for not null exceptions

Comment on lines +175 to +178
while (wifiManager.getConnectionInfo().networkId == -1 && connectionAttempts < 3) {
Thread.sleep(500)
connectionAttempts++
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i used ai to get more improvement for this part.
it is not recommended to use while loop. it will lead to many memory and infi problems.

fun connectToNetworkWPA(call: MethodCall, result: MethodChannel.Result) {
    val networkSSID = call.argument<String>("ssid")
    val password = call.argument<String>("password")
    
    if (networkSSID == null) {
        result.error("INVALID_ARGUMENT", "SSID cannot be null", null)
        return
    }

    val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    val connectivityManager = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

    val conf = WifiConfiguration().apply {
        SSID = "\"$networkSSID\""
        status = WifiConfiguration.Status.ENABLED
        allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP)
        allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP)
        allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
        allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
        if (password.isNullOrEmpty()) {
            allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
        } else {
            preSharedKey = "\"$password\""
            allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK)
        }
    }

    val networkCallback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            super.onAvailable(network)
            connectivityManager.unregisterNetworkCallback(this)
            result.success(true)
        }

        override fun onUnavailable() {
            super.onUnavailable()
            connectivityManager.unregisterNetworkCallback(this)
            result.success(false)
        }
    }

    val networkId = wifiManager.addNetwork(conf)
    if (networkId == -1) {
        result.success(false)
        return
    }

    wifiManager.disconnect()
    wifiManager.enableNetwork(networkId, true)
    wifiManager.reconnect()

    val request = NetworkRequest.Builder()
        .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
        .build()

    connectivityManager.registerNetworkCallback(request, networkCallback)

    // Set a timeout
    Handler(Looper.getMainLooper()).postDelayed({
        connectivityManager.unregisterNetworkCallback(networkCallback)
        result.success(false)
    }, 30000) // 30 seconds timeout
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interface NetworkCallback {
    fun onConnected()
    fun onFailed(reason: String)
}

@YassinNouh21
Copy link
Contributor

@ghassenbenzahra123 make sure to make dart linter analysis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants