WiFiFlutter icon indicating copy to clipboard operation
WiFiFlutter copied to clipboard

[wifi_iot] Nothings happens when setting 'withInternet: true'

Open rdmo94 opened this issue 3 years ago • 35 comments

I'm using this library to connect to a Raspberry Pi connected to a wifi-network. I recently upgraded the device i'm testing on, and I am currently running android 11.

The call to connect() worked fine until I upgraded to a newer device and android version. Whenever i set the withInternet flag to false, it connects as intended, but as a temporary connection without internet access.

If i set withInternet: true nothing happens, but the method connect returns true, as if I'm connected successfully. I'm using WPA for security.

I should have the required permission set in my AndroidManifest, but i might be missing something. Can anyone help?

rdmo94 avatar Jan 18 '22 15:01 rdmo94

There are two different API for newer version of Android. For connection with internet you can look for "Wifi Suggestion API". You can check it's docs.

Also I've not tested this API for sometime, might review it in some time. Until, then encourage you to check the docs and code.

daadu avatar Jan 18 '22 16:01 daadu

Do you see any "approve dialogue"?

daadu avatar Jan 18 '22 16:01 daadu

Only when withInternet is set to false. Otherwise I see no dialogue or anything.

rdmo94 avatar Jan 19 '22 16:01 rdmo94

@rdmo94 Sounds like an issue, will look into it this weekend (no promise).

daadu avatar Jan 20 '22 10:01 daadu

Let me know if you need any further details!

rdmo94 avatar Jan 20 '22 14:01 rdmo94

Hi, even I’m having the same issue, if I use connect method with withInternet: true I’m getting a response as " E/WifiIotPlugin( 3997): status: 0 " and with the method findAndConnect getting "PlatformException(Error, Invalid BSSID representation, , null)". This is with android 11 but with android 8 and 9 working fine.

manu-hr avatar Jan 24 '22 05:01 manu-hr

@daadu Any updates yet? I'm getting the same message as @manu-hr, but not sure if it is considered an error. I can confirm, that it working fine for Android versions below 11.

rdmo94 avatar Jan 24 '22 15:01 rdmo94

Additionally I also experienced this error quite some times: java.lang.IllegalArgumentException: NetworkCallback was not registered, with a stack trace related to WifiIotPlugin. I'm not sure why and when it occurs, but it could be due to wifi being turned off.

rdmo94 avatar Jan 24 '22 15:01 rdmo94

Screenshot 2022-01-25 at 12 13 55

For reference

I can confirm, that this exception occurs when trying to connect to a wifi network, when wifi is turned off. For me, this crashes the entire app.

rdmo94 avatar Jan 25 '22 11:01 rdmo94

Hi, even I’m having the same issue, if I use connect method with withInternet: true I’m getting a response as " E/WifiIotPlugin( 3997): status: 0 " and with the method findAndConnect getting "PlatformException(Error, Invalid BSSID representation, , null)". This is with android 11 but with android 8 and 9 working fine.

@daadu As for findAndConnect method the ssid and bssid parameters are interchanged, please look into it.

Even after that with WifiNetworkSuggestion API, I'm getting the connection status "STATUS_NETWORK_SUGGESTIONS_SUCCESS" but Nothing changing in the wifi connection of the device. I'm getting the response also as true without waiting any time and no dialogues are popping up. If you have any suggestions please let me know.

manu-hr avatar Jan 28 '22 07:01 manu-hr

I did some debugging, looks like the network added via the NetworkSuggestion API [which is the only way to get "internet enabled" wifi connection ] is just a "suggestion" to android - I found out that the android connects to the wifi - if you open the Wifi pick list from notification drawer or settings - looks like the android needs to be "triggered" to check the nely added suggestion (looks like a bug in Android side) - one workaround I found is to call await WiFiForIoTPlugin.loadWifiList() after connect(..., withInternet: true) - this will trigger android to look for app suggested network - and the connection is established.

@manu-hr Thanks for reporting the mac-adress issue, it is fixed with #245.

daadu avatar Jan 30 '22 12:01 daadu

@rdmo94 @manu-hr Try the workaround I suggested and let me know if it works - will add this caveat in "README".

daadu avatar Jan 30 '22 12:01 daadu

@rdmo94 @manu-hr Try the workaround I suggested and let me know if it works - will add this caveat in "README".

No, It is not working for me. But as you mentioned if I open the wifi picklist it is connecting only if there is no other available wifi -connections.

manu-hr avatar Jan 31 '22 06:01 manu-hr

Seems, like things are not in our hands - read.

Did you try registerWifiNetwork - see if that fits your use-case. The API is like user adding this network to the wifi list himself.

daadu avatar Jan 31 '22 06:01 daadu

Moreover, I have prototyped removing "Network Suggestion API" at all - some SO answeres suggested using the "Network Specifier API" for internet connection as well.

You can test this against daadu:wifi_iot/poc/remove-wifi-suggestion-api branch + with forceWifiUsage(true) - see if that works - by fetching https://google.com or something - make sure the cellular data is turned off completely - just to make sure that the requests are routing via Wifi connection. Although, I think this should not work though, but many SO answeres suggest this. Give it a try.

daadu avatar Jan 31 '22 06:01 daadu

@daadu Thanks a lot for your effort. I tried your suggested workaround with await WiFiForIoTPlugin.loadWifiList() without any change. Registering the network before connecting has no effect either. Still nothing happens when trying to connect. As this is to be used in a business context, a colleague of mine will put some effort into fixing this. I'll let you know, if we come up with a permanent solution.

rdmo94 avatar Feb 01 '22 09:02 rdmo94

Have the same issue here.

When withInternet set to false I get the special native connection dialog that allows me to connect to my raspberry pi access point.

However, when connected without internet, my Web-RTC negotiation via websocket is blocked, which makes this mode useless for me. Somehow Android thinks we "need internet" to access local addresses like 192.168.4.1 etc. This is evident even in chrome. If I connect via my app, and enter any IP-address in the address field, chrome will report that I am offline. This really seems like an oversight or overzealous security measure by the Android OS.

When I set withInternet to true, the connect call returns success, but isn't actually connected.

andreasmpet avatar Feb 04 '22 13:02 andreasmpet

@Nailer did you use forceWifiUsage(true) after connection? That might be the issue with withInternet: false.

With withInternet: true there seems to be an issue, which is under investigation.

daadu avatar Feb 05 '22 06:02 daadu

I did not try that. I will try it and see if it helps.

On Sat, Feb 5, 2022, 07:00 Harsh Bhikadia @.***> wrote:

@Nailer https://github.com/Nailer did you use forceWifiUsage(true) after connection? That might be the issue with withInternet: false.

With withInternet: true there seems to be an issue, which is under investigation.

— Reply to this email directly, view it on GitHub https://github.com/flutternetwork/WiFiFlutter/issues/235#issuecomment-1030534536, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAATJZS3DSEQRBIAFPGLV3DUZS4JVANCNFSM5MHNQUCQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

andreasmpet avatar Feb 05 '22 15:02 andreasmpet

Any updates on this? Having the same issue.

quoc-huynh-cosee avatar Mar 15 '22 08:03 quoc-huynh-cosee

The issue still persists. In my project we've had to built our own native tool for newer Android versions using the WifiNetworkSuggestion.Builder. Using this approach Android will present a list of wifi suggestions, as the user has to manually select the wifi to connect to. Not sure why they changed this, but it appears there is no other way if you want internet connection.

rdmo94 avatar Mar 15 '22 09:03 rdmo94

The issue still persists. In my project we've had to built our own native tool for newer Android versions using the WifiNetworkSuggestion.Builder. Using this approach Android will present a list of wifi suggestions, as the user has to manually select the wifi to connect to. Not sure why they changed this, but it appears there is no other way if you want internet connection.

Can you give me some references?

quoc-huynh-cosee avatar Mar 15 '22 09:03 quoc-huynh-cosee

https://developer.android.com/reference/android/net/wifi/WifiNetworkSuggestion.Builder here is a reference to the Android API used.

private fun connectToWifi(ssid: String?, password: String?): Boolean {
        if(ssid == null || password == null) return false;
        val suggestion = WifiNetworkSuggestion.Builder()
            .setSsid(ssid)
            .setWpa2Passphrase(password)
            .build();

        val suggestions = listOf(suggestion);
        val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager;
        val status = wifiManager.addNetworkSuggestions(suggestions);
        val panelIntent = Intent(Panel.ACTION_WIFI)
        startActivityForResult(panelIntent, 545)
        return status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS
}

Here is the code for the function we call from flutter. It's written in kotlin. You would have to create a channel for calling the function from flutter. Here is some flutter documentation on that: https://docs.flutter.dev/development/platform-integration/platform-channels

Basically I reference this function through a named channel, which is pretty straight forward. The reference from flutter looks like this: await _channel.invokeMethod( "connect", {"ssid": ssid, "password": password});

rdmo94 avatar Mar 15 '22 09:03 rdmo94

https://developer.android.com/reference/android/net/wifi/WifiNetworkSuggestion.Builder here is a reference to the Android API used.

private fun connectToWifi(ssid: String?, password: String?): Boolean {
        if(ssid == null || password == null) return false;
        val suggestion = WifiNetworkSuggestion.Builder()
            .setSsid(ssid)
            .setWpa2Passphrase(password)
            .build();

        val suggestions = listOf(suggestion);
        val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager;
        val status = wifiManager.addNetworkSuggestions(suggestions);
        val panelIntent = Intent(Panel.ACTION_WIFI)
        startActivityForResult(panelIntent, 545)
        return status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS
}

Here is the code for the function we call from flutter. It's written in kotlin. You would have to create a channel for calling the function from flutter. Here is some flutter documentation on that: https://docs.flutter.dev/development/platform-integration/platform-channels

Basically I reference this function through a named channel, which is pretty straight forward. The reference from flutter looks like this: await _channel.invokeMethod( "connect", {"ssid": ssid, "password": password});

Is there any way to disconnect? @rdmo94

jassimjinnah avatar Jun 23 '22 06:06 jassimjinnah

https://developer.android.com/reference/android/net/wifi/WifiNetworkSuggestion.Builder here is a reference to the Android API used.

private fun connectToWifi(ssid: String?, password: String?): Boolean {
        if(ssid == null || password == null) return false;
        val suggestion = WifiNetworkSuggestion.Builder()
            .setSsid(ssid)
            .setWpa2Passphrase(password)
            .build();

        val suggestions = listOf(suggestion);
        val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager;
        val status = wifiManager.addNetworkSuggestions(suggestions);
        val panelIntent = Intent(Panel.ACTION_WIFI)
        startActivityForResult(panelIntent, 545)
        return status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS
}

Here is the code for the function we call from flutter. It's written in kotlin. You would have to create a channel for calling the function from flutter. Here is some flutter documentation on that: https://docs.flutter.dev/development/platform-integration/platform-channels Basically I reference this function through a named channel, which is pretty straight forward. The reference from flutter looks like this: await _channel.invokeMethod( "connect", {"ssid": ssid, "password": password});

Is there any way to disconnect? @rdmo94

private fun disconnectFromWifi(): Boolean {
        var wifiManager: WifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager;
        var status = wifiManager.removeNetworkSuggestions(mutableListOf<WifiNetworkSuggestion>());
        if(status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
            return true;
        } else {
            return false;
        }
    }

This is how i do it. And i have a channel for calling the function.

rdmo94 avatar Jun 23 '22 08:06 rdmo94

https://developer.android.com/reference/android/net/wifi/WifiNetworkSuggestion.Builder here is a reference to the Android API used.

private fun connectToWifi(ssid: String?, password: String?): Boolean {
        if(ssid == null || password == null) return false;
        val suggestion = WifiNetworkSuggestion.Builder()
            .setSsid(ssid)
            .setWpa2Passphrase(password)
            .build();

        val suggestions = listOf(suggestion);
        val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager;
        val status = wifiManager.addNetworkSuggestions(suggestions);
        val panelIntent = Intent(Panel.ACTION_WIFI)
        startActivityForResult(panelIntent, 545)
        return status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS
}

Here is the code for the function we call from flutter. It's written in kotlin. You would have to create a channel for calling the function from flutter. Here is some flutter documentation on that: https://docs.flutter.dev/development/platform-integration/platform-channels Basically I reference this function through a named channel, which is pretty straight forward. The reference from flutter looks like this: await _channel.invokeMethod( "connect", {"ssid": ssid, "password": password});

Is there any way to disconnect? @rdmo94

private fun disconnectFromWifi(): Boolean {
        var wifiManager: WifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager;
        var status = wifiManager.removeNetworkSuggestions(mutableListOf<WifiNetworkSuggestion>());
        if(status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
            return true;
        } else {
            return false;
        }
    }

This is how i do it. And i have a channel for calling the function.

This doesn't disconnect the network. Unfortunately, we have to manually switch on and off the wifi to get disconnected from the network. Is there any way to override this manual process using program? @rdmo94

jassimjinnah avatar Jun 23 '22 11:06 jassimjinnah

Can anyone help me using example to remove network suggestions along with disconnect ? In documentation, it's mentioned that we should use constants. But it seems unfamiliar. It would be helpful if any example code is posted . @daadu @rdmo94

jassimjinnah avatar Jun 25 '22 20:06 jassimjinnah

@jassim18 with the plugin - if the network was connected (network suggestion added) with plugin then you can remove them with disconnect

daadu avatar Jun 27 '22 06:06 daadu

@jassim18 with the plugin - if the network was connected (network suggestion added) with plugin then you can remove them with disconnect

Hey @daadu . Thanks for checking out. But I need a working code example. I couldn't establish disconnect in my project. The internet gets disconnected only when I switch off the wifi and on it again. That means it just forgets and doesn't disconnect using the function. Could you just post the disconnect function example code that can work ?

jassimjinnah avatar Jun 30 '22 16:06 jassimjinnah

@jassim18 with the plugin - if the network was connected (network suggestion added) with plugin then you can remove them with disconnect

Hey @daadu . Thanks for checking out. But I need a working code example. I couldn't establish disconnect in my project. The internet gets disconnected only when I switch off the wifi and on it again. That means it just forgets and doesn't disconnect using the function. Could you just post the disconnect function example code that can work ?

@rdmo94 @daadu Is anyone of you still there ? Stuck with this issue

jassimjinnah avatar Jul 19 '22 06:07 jassimjinnah