How can I check 3G, Wi-Fi, EDGE, cellular networks in Windows Phone 7?

How can I test 3G, Wi-Fi, EDGE, cellular networks in Windows Phone 7 using C #?

+7
source share
6 answers

If you can use the Mango SDK (7.1), and if your script involves using sockets, there is a trivial way to get the NetworkInterfaceType / SubType information for the connection you just made:

NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface(); var type = netInterfaceInfo.InterfaceType; var subType = netInterfaceInfo.InterfaceSubtype; 

No need to use the NetworkInterface.NetworkInterfaceType property (which, as you know, takes up to 30 seconds to return); there is no need to run host name resolution to determine the type of network; no need to listen to network change events.

Of course, this works best when combined with DeviceNetworkInformation.IsNetworkAvailable or NetworkInterface.GetIsNetworkAvailable () - these calls return immediately regardless of whether you are online or not. If so, first plug in the socket and ask questions when it's plugged in :-)

Final note: be careful with Mango DeviceNetworkInformation.IsWiFiEnabled - I thought it would return if I was on a Wi-Fi network, but instead it will turn Wi-Fi on or off in the phone settings ... not super useful.

+6
source

take a look at fake tools, they have a PhoneNetworking class for this:

http://wildermuth.com/2011/03/05/Phoney_Tools_Updated_(WP7_Open_Source_Library )

open source you can check the source code

+2
source

Starting with the release of Mango (beta 2 and RC), this information is now available, but it requires you to really establish a connection, apparently because it does not check until something needs it.

You can either perform DNS resolution (see below) or use the GetCurrentNetworkInterface WebRequest extension method, which throws an InvalidOperationException if the request is not already connected.

There are also some events in Microsoft.Phone.Net.NetworkInformation , but I wonโ€™t be surprised if these events do not fire until a connection is established.

Interestingly, you can also prefer or require for each connection using SetNetworkPreference and SetNetworkRequirement , although it does not go beyond wifi and cell.

 DeviceNetworkInformation.ResolveHostNameAsync( new DnsEndPoint("microsoft.com", 80), new NameResolutionCallback(nrr => { var info = nrr.NetworkInterface; var type = info.InterfaceType; var subType = info.InterfaceSubtype; }), null); 

The enumeration values โ€‹โ€‹for NetworkInterfaceType (wifi / gsm) and NetworkInterfaceSubType (edge โ€‹โ€‹/ 3g) are available on MSDN.

+2
source

Without socket:

 var currentList = new NetworkInterfaceList().Where(i => i.InterfaceState == ConnectState.Connected).Select(i => i.InterfaceSubtype); if (currentList.Contains(NetworkInterfaceSubType.WiFi)) Debug.WriteLine("WiFi"); if (currentList.Intersect(new NetworkInterfaceSubType[] { NetworkInterfaceSubType.Cellular_EVDO, NetworkInterfaceSubType.Cellular_3G, NetworkInterfaceSubType.Cellular_HSPA, NetworkInterfaceSubType.Cellular_EVDV, }).Any()) Debug.WriteLine("3G"); if (currentList.Intersect(new NetworkInterfaceSubType[] { NetworkInterfaceSubType.Cellular_GPRS, NetworkInterfaceSubType.Cellular_1XRTT, NetworkInterfaceSubType.Cellular_EDGE, }).Any()) Debug.WriteLine("2G"); 
+2
source

Unfortunately, the api does not provide very limited information about the type of network connection you have. You can find out if you are on 3G, cellular or Ethernet (i.e., on a USB connection to a PC), but thatโ€™s all you get.

Check out this for more information. The best way to test your network connectivity on WP7

+1
source

To get network data for the Windows Phone application, that is, it is connected to an Ethernet, Wi-Fi or cellular network, while also receiving a subtype of ie 2G or 3G network, the following program can be used.

 Using Microsoft.Phone.Net.NetworkInformation Using Microsoft.Phone.net.NetworkInfromation var Newlist = new NetworkInterfaceList(); foreach (NetworkInterfaceInfo x in Newlist) { if(x.InterfaceState==ConnectState.Connected) { if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.WiFi)) { Interface = x.InterfaceType.ToString(); SubInterface = x.InterfaceSubtype.ToString(); break; } else if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EVDO) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_3G) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_HSPA) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EVDV)) { Interface = x.InterfaceType.ToString(); SubInterface= "3G Network"; break; } else if(x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_GPRS) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_1XRTT) || x.InterfaceSubtype.Equals(NetworkInterfaceSubType.Cellular_EDGE)) { Interface = x.InterfaceType.ToString(); SubInterface= "2G Network"; break; } else { Interface = "Ethernet"; SubInterface= "Unknown" ; break; } } else { Interface="not connected"; SubInterface="unknown"; } 

Here, the interface and SubInterface provide network information.

0
source

All Articles