简体   繁体   中英

Windows 10 Mobile UWP app prefers cellular data over wifi. How to handle multiple network connections

I have Windows 10 Mobile application. I noticed the other day that some of my server communication and network access calls/code were all failing. The mobile device I was using had Cellular data and Wifi options both turned on in the settings. I was connected to my Wifi just fine. However, the cellular data was not activated on this device. It shows cellular is turned on and shows some bars but the service is not activated with the carrier.

During troubleshooting, I turned off the cellular data option in settings and once I did that, all of my server and network access code started working again (ie. not throwing exceptions).

I have been searching for a while and have not been able to find the answer to my assumption that Windows 10 Mobile, when both cellular data and wifi are turned on will default to using cellular first, even if cellular is not working. Is this true? Maybe a better way to ask the question is, how does the device handle when you have two data connection sources enabled? How does it choose which one to use?

Maybe the follow up question also is, how do I force it to use one or the other? I have seen during me research, alot of code on how to detect which network connection is active but not much information on what happens if more than one are active and how to force the OS to use one or the other. This could come in handy if I want to seamlessly switch my calls from wifi to cellular. For example, prefer wifi and use wifi but if you go out of range of wifi, then switch to cellular.

Thanks!

When we connect the Wifi, the UWP app will use the WIFI no matter the Cellular data is turn on.

You can use the following code to check your UWP app use cellular data or Wifi, you can also check the signal level of the current network.

ConnectionProfile profile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
if (profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
{
    Debug.WriteLine("InternetAccess");
}
var signal = profile.GetSignalBars();
Debug.WriteLine("signal is:"+signal);
if (profile.IsWlanConnectionProfile)
{
    Debug.WriteLine("Wifi");
}
else if (profile.IsWwanConnectionProfile)
{
    Debug.WriteLine("Cellular");
}

When Cellular data and Wifi options both turned on in the settings and they are connected, ConnectionProfile.IsWwanConnectionProfile will return true.

When the Wifi is disconnected, it will use the Cellular data automatic. We can not force UWP app use cellular data or Wifi by code. We can add the NetworkInformation.NetworkStatusChanged , it occurs when the network status changes for a connection. When the Wifi is disconnected, the event will be fired and ConnectionProfile.IsWwanConnectionProfile will return true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM