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

Clarify How To Check If Network is up #592

Open
thegreatco opened this issue Dec 8, 2023 · 1 comment
Open

Clarify How To Check If Network is up #592

thegreatco opened this issue Dec 8, 2023 · 1 comment
Labels

Comments

@thegreatco
Copy link

When using config files to configure WiFi settings, simply adding a handler to NetworkConnected as currently described is not enough.

In this case, you might want to register the NetworkConnected event in the Initialize() method to start any network related tasks once the device joins your network.

The event may fire before hooking up the handler. To ensure the application isn't needlessly blocked, users need to do something like:

var wifi = Device.NetworkAdapters.Primary<IWiFiNetworkAdapter>();
while (!wifi.IsConnected)
{
    await Task.Delay(100);
}

This could live in Initialize() or Run() depending on the customer's use case.

@patridge
Copy link
Collaborator

Just to check, what was the situation here? Were you relying on the auto-connect system in config?

Not sure if it's worth the Task and async/await overhead, but I've used this helper method before to keep the code clean when using the wifi.Connect(...) method directly. You could probably rig it up to rely on config connecting instead, though.

If it was written in a way that assumes .Connect is called elsewhere or config is used to connect, then you could delay the await until you absolutely needed to have network access to continue.

var wifi = Device.NetworkAdapters.Primary<IWiFiNetworkAdapter>();

// Do whatever other initialization work you can before your first network call is required.
...

await wifi.WaitForNetworkConnection(...);

Since connecting to wifi isn't a fast process on a good day, it's probably worth firing it off and then only awaiting in your app when it's needed.

Unfortunately, this doesn't handle wifi drops and reconnects at all. So, maybe neither solution is sufficient. 🤷

@patridge patridge added the p1 label Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants