【问题标题】:Windows store and phone detecting Internet availableWindows 商店和手机检测互联网可用
【发布时间】:2016-01-13 19:43:35
【问题描述】:

我遇到了一个边缘案例的问题,希望有人能提供帮助。我有一个 Windows 8.1 通用应用程序,并且创建了一个商店和电话包,并尝试在它们之间共享尽可能多的代码。由于我们的应用程序需要互联网访问才能执行某些功能,因此我们需要一种可靠的方法来确定在我们尝试并失败某个功能之前。

所以最初我的代码只是查看了 NetworkInformation.GetInternetConnectionProfile(),然后比较了 GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess。这在大多数情况下都有效,但我们在手机上发现了一个案例,该设备在运营商的数据计划中但随后被移除,因此它现在只能执行 wifi 数据。在这种情况下,即使有 wifi,手机似乎仍然报告真实已关闭,然后无法连接,但从未报告没有可用的互联网。

所以我们将逻辑更改为以下。

       var internetConnection = NetworkInformation.GetInternetConnectionProfile();

        // If null then no internet and internet connection is null
        // If it is not null, then you have to look for any GetLanIdentfiers.  This means there is a lan adapter setup.
        // lastly you have to make sure the interface type is either an Ethernet or IEEE 802.11 interface.
        if ( internetConnection != null
           && internetConnection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess
           && ( ( internetConnection.NetworkAdapter.IanaInterfaceType == IEEE_802_11_INTERFACE 
           && NetworkInformation.GetLanIdentifiers().Any() )
           || internetConnection.NetworkAdapter.IanaInterfaceType == ETHERNET_INTERFACE ) )
        {
           return true;
        }

        return false;

正如 cmets 所说,如果 GetInternetConnectionProfile() 的返回值为 null,则没有可用的连接。如果有,那么我会检查 IneternetAccess。然后我检查 802.11 的 IanaInterfaceType && 任何 LanIdentifier 或以太网接口。

在使用运营商数据计划但不再使用的手机上,这确实可以正确检测到 wifi 不可用。然而,在现场,我们现在有一些商店应用程序客户报告应用程序认为上面的代码没有 wifi。我们当然可以添加一个#If 来以一种或另一种方式包装逻辑,但在某些时候我们想转移到新的通用应用程序项目,在该项目中使用#if 应该非常罕见,所以我想避免这种情况。

我们无法在我们的任何测试设备上复制此问题,因此这可能是我们实验室中没有的本地环境配置。有没有其他人想出一种可靠的方法来确定手机和商店应用程序上是否有 wifi?

【问题讨论】:

    标签: windows-phone-8.1 windows-store-apps


    【解决方案1】:

    NetworkInformation 类本身有两个属性,分别称为 IsWwanConnectionProfileIsWlanConnectionProfile

    第一个属性为您提供指示连接配置文件是否为 WWAN(移动)连接的值,第二个属性获得一个指示连接配置文件是否为 WLAN (WiFi) 连接的值。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多