Created
June 18, 2015 21:57
-
-
Save Seloris/f248100e8b04e8678ec4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Linq; | |
using Windows.Networking.Connectivity; | |
namespace NetworkInfo | |
{ | |
public static class NetworkService | |
{ | |
private const uint IANA_WIFI = 71; | |
private const uint IANA_ETHERNET = 6; | |
private const uint IANA_MOBILE = 243; | |
private const uint IANA_MOBILE2 = 244; | |
public enum NetworkCategory | |
{ | |
None, | |
Wifi, | |
_4G, | |
_3G, | |
_2G, | |
Edge, | |
Ethernet, | |
// Connexion mobile mais non identifiée (erreur, etc..) | |
UnknownMobile | |
} | |
public static NetworkCategory GetNetworkCategory() | |
{ | |
try | |
{ | |
// On récupère le ConnectionProfile, qui est null sans connexion (ex : mode avion) | |
var profile = NetworkInformation.GetInternetConnectionProfile(); | |
if (profile == null || profile.NetworkAdapter == null) | |
{ | |
return NetworkCategory.None; | |
} | |
// L'interface type "Iana" (uint) permet d'identifier le type de protocole | |
var interfaceType = profile.NetworkAdapter.IanaInterfaceType; | |
if (interfaceType == IANA_WIFI) | |
{ | |
return NetworkCategory.Wifi; | |
} | |
if (interfaceType == IANA_ETHERNET) | |
{ | |
return NetworkCategory.Ethernet; | |
} | |
// Si l'interface IANA correspond au mobile et que le WwanConnectionProfileDetails n'est pas null, | |
// On approfondit la recherche pour déterminer le type de connexion mobile | |
if ((interfaceType == IANA_MOBILE || interfaceType == IANA_MOBILE2) && profile.WwanConnectionProfileDetails != null) | |
{ | |
// On récupère le WwanDataClass actuel | |
var wwanProfiles = profile.WwanConnectionProfileDetails.GetCurrentDataClass(); | |
// Si None, on renvoit tout de suite un UnkownMobile | |
if(wwanProfiles.Equals(WwanDataClass.None)) | |
{ | |
return NetworkCategory.None; | |
} | |
// On récupère la liste de l'enum WwanDataClass pour boucler dessus | |
var wwanList = EnumListHelper<WwanDataClass>.Get().ToList(); | |
// On exclut None pour ne pas fausser la recherche | |
wwanList.Remove(WwanDataClass.None); | |
WwanDataClass? wwanClass = null; | |
foreach (var wwan in wwanList) | |
{ | |
if (wwanProfiles.HasFlag(wwan)) // ou utiliser le ET logique (wwanProfiles & wwan) == wwan) | |
{ | |
// On récupère le premier qui correspond et on break | |
wwanClass = wwan; | |
break; | |
} | |
} | |
// Si on a une correspondance, on récupère la catégorie associée | |
if (wwanClass != null) | |
{ | |
switch (wwanClass) | |
{ | |
// Edge | |
case WwanDataClass.Edge: | |
return NetworkCategory.Edge; | |
// GPRS : 2G | |
case WwanDataClass.Gprs: | |
return NetworkCategory._2G; | |
// Les WwanDataClass suivants sont du type 3G (voir https://fr.wikipedia.org/wiki/CDMA2000) | |
case WwanDataClass.Cdma1xEvdo: | |
case WwanDataClass.Cdma1xEvdoRevA: | |
case WwanDataClass.Cdma1xEvdv: | |
case WwanDataClass.Cdma1xEvdoRevB: | |
case WwanDataClass.Cdma3xRtt: | |
case WwanDataClass.Cdma1xRtt: | |
// Types HSPA, à ne pas confondre avec HSPA+, donc 3G et non H+ | |
case WwanDataClass.Umts: | |
case WwanDataClass.Hsupa: | |
case WwanDataClass.Hsdpa: | |
return NetworkCategory._3G; | |
// LTE = 4G standard | |
case WwanDataClass.LteAdvanced: | |
case WwanDataClass.CdmaUmb: | |
return NetworkCategory._4G; | |
// Pas de profil ou non identifié | |
case WwanDataClass.Custom: | |
return NetworkCategory.UnknownMobile; | |
default: | |
return NetworkCategory.UnknownMobile; | |
} | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
// WwanConnectionProfileDetails.GetCurrentDataClass() peut lever une exception non gérée | |
return NetworkCategory.UnknownMobile; | |
} | |
return NetworkCategory.None; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment