-
Notifications
You must be signed in to change notification settings - Fork 182
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(wifi_iot): Fix connection to registered network on Android < 10 (Q) (Extended) #294
base: master
Are you sure you want to change the base?
fix(wifi_iot): Fix connection to registered network on Android < 10 (Q) (Extended) #294
Conversation
…connected to the network before, a new connection request with a wrong password would succeed. (Tested on Android 8.1 OnePlus 5T) Changed the WIFI behavior when disconnecting from an app-created network on Android < Q to be loosely similar to observed behvaior on Android 11 (OnePlus 7 Pro) where the device will try to reconnect to the previously connected network.
@lmmfranco Can you review these changes as well? |
packages/wifi_iot/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java
Outdated
Show resolved
Hide resolved
packages/wifi_iot/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java
Outdated
Show resolved
Hide resolved
@daadu if these changes look good, then I suggest moving forward with them. So far, I don't see anything egregious with the changes, except for minor nits. |
break; | ||
} | ||
|
||
enabled = moWiFi.enableNetwork(updateNetwork, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this updated enabled
value isn't getting used after updating it here. Is it even necessary to assign it to a value if it won't get read after this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WiFiFlutter/packages/wifi_iot/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java
Lines 1586 to 1596 in 796f784
if(connected == true) { | |
break; | |
} else { | |
disconnect = moWiFi.disconnect(); | |
if (!disconnect) { | |
break; | |
} | |
enabled = moWiFi.enableNetwork(updateNetwork, true); | |
break; | |
} |
@KhatibFX Can you explain this change block? why is it necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WiFiFlutter/packages/wifi_iot/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java
Lines 1586 to 1596 in 796f784
if(connected == true) { break; } else { disconnect = moWiFi.disconnect(); if (!disconnect) { break; } enabled = moWiFi.enableNetwork(updateNetwork, true); break; } @KhatibFX Can you explain this change block? why is it necessary?
So after netState == SupplicantState.COMPLETED
- we check if connected, if not then disconnect and "enable network". I am not sure what the reason behind it.
Also, can the code be simplified? - avoiding break
in each if-else branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the reason here is that sometimes after supplicantstate.completed, it turns out the connection failed. On newer android versions, the phone will automatically revert back to the WiFi connection that was being used before trying to connect to this new network. On older Android versions, it will just remain disconnected and won't attempt to reconnect to the previously connected network. This part of the code attempts to do that reconnection step, to make the experience similar to newer Android versions. From my testing, it wasn't enough to just ask to enableNetwork of the previous network. I had to fully reset the connection by disconnecting and then reenabling the created network.
There may be a better way to rewrite this block better, but I am not amazing in Java so I'm not sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KhatibFX Sorry, for delaying the review.
I have attached some comments, please check them and give your views on it.
String ssid = conf.SSID; | ||
Random random = new Random(System.currentTimeMillis()); | ||
//loop in the rare case that the generated ssid already exists | ||
for (int i = 0; i < 20; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this retry loop necessary? shouldn't failing/abort make more sense after one attempt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I remember, I sometimes had it fail on me without explicit reason, but it has been a while since I tried, so I'm not sure why. The loop doesn't take that long in that failure case.
for (int i = 0; i < 20; i++) { | ||
int randomInteger = random.nextInt(10000); | ||
//create a valid SSID with max length of 32 | ||
String ssidRandomized = ssid + randomInteger; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ssid
will be wrapped around quote ("
) - that should be trimmed, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be, but it isn't necessary, since we mostly care about the length anyway. It would be "cleaner" to trim it, yes.
break; | ||
} | ||
|
||
enabled = moWiFi.enableNetwork(updateNetwork, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WiFiFlutter/packages/wifi_iot/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java
Lines 1586 to 1596 in 796f784
if(connected == true) { | |
break; | |
} else { | |
disconnect = moWiFi.disconnect(); | |
if (!disconnect) { | |
break; | |
} | |
enabled = moWiFi.enableNetwork(updateNetwork, true); | |
break; | |
} |
@KhatibFX Can you explain this change block? why is it necessary?
@@ -1510,24 +1578,38 @@ private Boolean connectToDeprecated( | |||
if (!enabled) return false; | |||
|
|||
boolean connected = false; | |||
int networkId = -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks unnecessary, it is not being used outside the for loop scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah the declaration can be done inside the for loop instead.
Hi, sorry I have been busy lately and haven't gotten a chance to check or follow up on the comments. I will try to get back to them within the next week or so. And thanks for the great library, of course! |
@KhatibFX Any update? |
@daadu Hey, Sorry for the late response, I just responded to the comments, let me know if I can help further. Thanks. |
397aaa3
to
a527cb8
Compare
Fixed an issue on Android < Q where if the user already successfully connected to the network before, a new connection request with a wrong password would succeed. (Tested on Android 8.1 OnePlus 5T)
Changed the WIFI behavior when disconnecting from an app-created network on Android < Q to be loosely similar to observed behvaior on Android 11 (OnePlus 7 Pro) where the device will try to reconnect to the previously connected network.
This branch was created because the previous contribution from @lmmfranco didn't work for me. I hope it doesn't affect his work either and it can work for everyone who faced this issue.
The code might be a little messy, feel free to edit it out to fix inconsistencies.