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(wifi_iot): Fix connection to registered network on Android < 10 (Q) (Extended) #294

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

KhatibFX
Copy link

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.

…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.
@KhatibFX KhatibFX changed the title ix(wifi_iot): Fix connection to registered network on Android < 10 (Q) (Extended) fix(wifi_iot): Fix connection to registered network on Android < 10 (Q) (Extended) Jun 21, 2022
@daadu
Copy link
Member

daadu commented Jun 21, 2022

@lmmfranco Can you review these changes as well?

@diegotori
Copy link

@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);

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?

Copy link
Member

@daadu daadu Aug 28, 2022

Choose a reason for hiding this comment

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

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?

Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Author

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.

Copy link
Member

@daadu daadu left a 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++) {
Copy link
Member

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.

Copy link
Author

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;
Copy link
Member

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?

Copy link
Author

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);
Copy link
Member

@daadu daadu Aug 28, 2022

Choose a reason for hiding this comment

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

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;
Copy link
Member

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.

Copy link
Author

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.

@KhatibFX
Copy link
Author

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!

@daadu
Copy link
Member

daadu commented Sep 5, 2022

@KhatibFX Any update?

@KhatibFX
Copy link
Author

@daadu Hey, Sorry for the late response, I just responded to the comments, let me know if I can help further. Thanks.

@daadu daadu force-pushed the master branch 5 times, most recently from 397aaa3 to a527cb8 Compare November 5, 2022 10:43
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.

3 participants