|UPDATE: Actually checking the network connection in this way is very bad. As it turns out accessing NetworkInterface.NetworkInterfaceType is a blocking call which can hang the UI for long periods (over 20 seconds in some cases). See my later post on this topic for more details and a better solution|
With all this talk of the cloud it can be easy to assume that you will always be able to make a connection to the internet from a Windows Phone 7.
Unfortunately as we know this is not always the case – even in built up areas. I live smack bang in the centre of London and yet for at least 50% of my commute to work I cannot get a workable mobile data connection.
So how can you detect what type of connection you have on a Windows Phone 7 device?
Thankfully the answer is easy enough. Turns out there is an App for that…err… I mean there is an API for that.
This is one of the mysterious non-Silverlight API's that are available on the phone.
Typically you will just be interested in whether or not you have a data connection. In which case the following will do the trick:
bool hasNetworkConnection = NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None;
You may also be interested in which type of connection you have, is it via WiFi or 3G? If you are about to download a huge file you might want to make sure you are not about to burn through your users 3G data allowance without them realising it.
NetworkInterfaceType enumeration holds the key working out which connection you have. Oddly enough the enumeration includes a large number of connection types and most of them aren't even possible on the phone. Nevertheless the useful ones are:
||The phone is not connected to any data network|
||The phone is connected to a WiFi hotspot|
||The phone is connected to a GSM network|
||The phone is connected to a CDMA network (US and parts of Asia only)|
||The phone has an ethernet connection (typically this is when the phone is connected to a PC via USB)|
There are three things to remember here:
- If you are checking for a mobile data connection always check for both
MobileBroadbandCdma.Even you are only targeting a market which only has GSM devices can you be 100% sure no one will need to run your app on a CDMA phone? Don't forget app purchases are tied to Live ID's not phones.
- When you are debugging the phone via USB you have always have an ethernet connection with the device so the check above might not return the correct result. You might choose to exclude ethernet connections from the test as follows:
bool hasNetworkConnection = (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None) #if DEBUG && (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.Ethernet); #endif
- Checking the
NetworkInterfaceTypedoesn’t guarantee you will be able to connect to the remote server. Remote calls will often fail even if the phone thinks it has a connection – especially if you are moving. Similarly just because you are connected to a WiFi hotspot it doesn't mean you can access the internet through it. So you still need to program defensively and expect and handle exceptions that are likely to result from a poor connection.
Also remember this only tells you if the phone is connected to a network. You have no way of knowing whether that network is connected to the internet until you actually try to connect to a server.