I plan to load only as much data as required in my app. Which means, that when the data is loaded via Wifi, I want to prefetch things. If the data is loaded via mobile plan
The phone can use both 3G and WiFi at the same time. The case I've tested - the phone haven't close existing TCP connections opened while on 3G after it found and connected to a WiFi network. If a new TCP connection is opened it'll use WiFi instead.
There's SocketExtensions.GetCurrentNetworkInterface
extension method from Microsoft.Phone.dll that tell you which interface is used for the specified socket.
Have you tried the System.Net.NetworkInformation namespace on WP8. This package has a static method which returns the network status. You can then switch based on this information
NetworkInterface.NetworkInterfaceType
More details can be found here
Let's solve it by myself:
There is the Data Sense API, which allows one to not only check whether the device is roaming, but also check whether the app approaches or is over the data limit set in Data Sense. The API also works when the provider doesn't allow to usse the Data Sense UI.
In particular, this code from the link above solves everything!
// Get current Internet Connection Profile.
ConnectionProfile internetConnectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
// Check the connection details.
if (internetConnectionProfile.NetworkAdapter.IanaInterfaceType != IANA_INTERFACE_TYPE_WIFI)
{
// Connection is not a Wi-Fi connection.
if (internetConnectionProfile.GetConnectionCost().Roaming)
{
// User is roaming. Don't send data out.
m_bDoNotSendData = true;
}
if (internetConnectionProfile.GetConnectionCost().ApproachingDataLimit)
{
// User is approaching data limit. Send low-resolution images.
m_bSendLowResolutionImage = true;
}
if (internetConnectionProfile.GetConnectionCost().OverDataLimit)
{
// User is over data limit. Don't send data out.
m_bDoNotSendData = true;
}
}
else
{
//Connection is a Wi-Fi connection. Data restrictions are not necessary.
m_bDoNotSendData = false;
m_bSendLowResolutionImage = false;
}
// Optionally, report the current values in a TextBox control.
string cost = string.Empty;
switch (internetConnectionProfile.GetConnectionCost().NetworkCostType)
{
case NetworkCostType.Unrestricted:
cost += "Cost: Unrestricted";
break;
case NetworkCostType.Fixed:
cost += "Cost: Fixed";
break;
case NetworkCostType.Variable:
cost += "Cost: Variable";
break;
case NetworkCostType.Unknown:
cost += "Cost: Unknown";
break;
default:
cost += "Cost: Error";
break;
}
cost += "\n";
cost += "Roaming: " + internetConnectionProfile.GetConnectionCost().Roaming + "\n";
cost += "Over Data Limit: " + internetConnectionProfile.GetConnectionCost().OverDataLimit + "\n";
cost += "Approaching Data Limit : " + internetConnectionProfile.GetConnectionCost().ApproachingDataLimit + "\n";
NetworkStatus.Text = cost;