Windows 10 UWP - detect if the current internet connection is Wifi or Cellular?

前端 未结 2 1645
礼貌的吻别
礼貌的吻别 2020-12-05 08:02

In Windows 10 UWP app how do I detect if the current internet connection is Wifi or Cellular?

相关标签:
2条回答
  • 2020-12-05 08:20

    In UWP you can check network connectivity using the IsWlanConnectionProfile or IsWwanConnectionProfile properties.

    An example would be:

    var temp = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
    
    if (temp.IsWlanConnectionProfile)
    {
         // its wireless
    }else if (temp.IsWwanConnectionProfile)
    {
         // its mobile
    }
    

    I hope this helps.

    0 讨论(0)
  • 2020-12-05 08:27

    Other than just getting the connectivity (that others have mentioned) you can also handle metered connections better.

    How to manage metered network cost constraints

    switch (connectionCost.NetworkCostType)
    {
        case NetworkCostType.Unrestricted:
            //
            break;
        case NetworkCostType.Fixed:
            //
            break;
        case NetworkCostType.Variable:
            //
            break;
        case NetworkCostType.Unknown:
            //
            break;
        default:
            //
            break;
    }
    

    See the networking demo at GitHub.

    if (connectionCost.Roaming || connectionCost.OverDataLimit)
    {
        Cost = NetworkCost.OptIn;
        Reason = connectionCost.Roaming
            ? "Connection is roaming; using the connection may result in additional charge."
            : "Connection has exceeded the usage cap limit.";
    }
    else if (connectionCost.NetworkCostType == NetworkCostType.Fixed
        || connectionCost.NetworkCostType == NetworkCostType.Variable)
    {
        Cost = NetworkCost.Conservative;
        Reason = connectionCost.NetworkCostType == NetworkCostType.Fixed
            ? "Connection has limited allowed usage."
            : "Connection is charged based on usage. ";
    }
    else
    {
        Cost = NetworkCost.Normal;
        Reason = connectionCost.NetworkCostType == NetworkCostType.Unknown
            ? "Connection is unknown"
            : "Connection cost is unrestricted";
    }
    
    0 讨论(0)
提交回复
热议问题