WiFiFlutter
WiFiFlutter copied to clipboard
Unable to connect with the IoT wifi endpoint
Hi there,
I am developing the app strictly for Android 11 and want to connect to wifi through the app. It says and shows that it is connected to the wifi (when I go to the wifi settings it is said that wifi is connected through the app and there is an exclamation mark next to the wifi icon). When I try to send the simple get request to the IoT device I am connected to, it doesn’t work (although I can successfully send that same request when I connect manually to the wifi endpoint). I am not sure if there is something I am doing wrong about it or I understand the documentation incorrectly.
Here is the code snippet of the function that triggers the connection process:
Future<String> connect(String ssid) async {
try {
bool result = await WiFiForIoTPlugin.connect(ssid, withInternet: false);
print("connecting");
if (result) {
return "Successfully connected";
} else {
return "There was an error connecting to that wifi!";
}
} catch (e) {
throw Exception(e);
}
}
Thank you for your response.
need to use forceWifiUsage(true) and disable it again - when you make network call with the wifi connected.
Sorry, could you add some more context to that? I also use a webview to show the interface of the connected IoT device, so I am not really sure where to use this forceWifiUsage and when to disable it.
forceWifiUsage when enabled, binds the app processes to the network that is connected with connect method, therefore to make network calls via the connected network you need to enable it and since the network restricts "the Internet" usage, you need to disable it when making network call to "the Internet"
My code was working normally, and then i just receive the log "ConnectivityManager.CallbackHandler(25762): callback not found for CALLBACK_IP_CHANGED message".
It's a weird behavior, the Future dosent wait anymore just go through the rest of the code.
My code is like this:
bool result = await WiFiForIoTPlugin.connect(ssid: 'wifi_ssid: 'wifi_password', security: NetworkSecurity.WPA, );
In the morning it was working normally. When calling the .connect, the cell phone's wifi was disabled and I tried to connect to the network I informed. Now it just goes straight through and returns false. I haven't really touched anything from the morning version to the now, it's very strange behavior
@daadu I dont get the "forceWifiUsage" after i connect to the wifi network i have to call this method?
@MLuan any clue in the logs? Why it is not connecting.
forceWifiUsage needs to be called to access network through the connected wifi, also while it is enabled network calls through main network (with internet) does not work so you should disable it when you want to use that. This is the reason it is not enabled by default by the app.
Hello, @daadu, first thanks for the great plugin.
I think that you forgot to add the forceWifiUsage documentation in the dart-side like is described on java-side.
https://github.com/flutternetwork/WiFiFlutter/blob/ca0c28525007f5bcdb708814d9fd8c7449cf6369/packages/wifi_iot/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java#L714-L721
Maybe this can solve the issues like #154 and #160.
Do you agree?
@lucaslannes I have added it in readme, for wifi_iot the docs are not at par. New plugins will have proper API docs.
Calling forceWifiUsage(true) after a success connection (using withInternet: false) solved my problem to connect to the IoT socket server. Without enabling the forceWifiUsage on Android 10+ the app cannot connect to the IoT device through Socket.connect(ip, port).
But testing on Android less than 10 (in my case Android 8), there is no need to call forceWifiUsage(true) to establish the socket connection.
@lucaslannes Android API (esp. for WiFi) is mess, so try to be as redundant as possible!
Do we have any update on this issue? How can we use it.
Any updates yet?