0

我已使用以下功能检查 Windows Mobile 模拟器中的网络状态。无论是否存在以太网,它都会返回“未知”。

function checkConnection() {
var networkState = navigator.network.connection.type;

var states = {};
states[Connection.UNKNOWN]  = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI]     = 'WiFi connection';
states[Connection.CELL_2G]  = 'Cell 2G connection';
states[Connection.CELL_3G]  = 'Cell 3G connection';
states[Connection.CELL_4G]  = 'Cell 4G connection';
states[Connection.NONE]     = 'No network connection';

alert('Connection type: ' + states[networkState]);

}

checkConnection();
4

1 回答 1

1

在 Windows Phone 上,为了获取当前接口,您可以使用类 'NetworkInterfaceList' :

NetworkInterfaceInfo CurrentInterface;
var interfacesList = new NetworkInterfaceList();

foreach (NetworkInterfaceInfo specificInterface in interfacesList)
{
    CurrentInterface = specificInterface;
    break;
}

您可以订阅网络更改的事件:

DeviceNetworkInformation.NetworkAvailabilityChanged += NetworkChanged;

private void NetworkChanged(object sender, NetworkNotificationEventArgs e)
{
    CurrentInterface = e.NetworkInterface;
}

得到当前界面后,就可以得到真实的网络类型:

switch (CurrentInterface.InterfaceType)
{
    case NetworkInterfaceType.Ethernet:
        // ETHERNET (USB connected)
        break;
    case NetworkInterfaceType.MobileBroadbandCdma:
        switch (pNetworkInterfaceInfo.InterfaceSubtype)
        {
            case NetworkInterfaceSubType.Cellular_GPRS:
                // 2G
                break;
            case NetworkInterfaceSubType.Cellular_EDGE:
                // 2GP
                break;
            case NetworkInterfaceSubType.Cellular_EVDO:
            case NetworkInterfaceSubType.Cellular_EVDV:
                // ?
                break;
            case NetworkInterfaceSubType.Cellular_1XRTT:
                // ?
                break;
            case NetworkInterfaceSubType.Cellular_3G:
                // 3G
                break;
            case NetworkInterfaceSubType.Cellular_HSPA:
                // 3GP
                break;
            case NetworkInterfaceSubType.Unknown:
                // Unknonwn ? 4G is Unknown.
                break;
        }
        break;
    case NetworkInterfaceType.MobileBroadbandGsm:
        switch (pNetworkInterfaceInfo.InterfaceSubtype)
        {
            case NetworkInterfaceSubType.Cellular_GPRS:
                // 2G
                break;
            case NetworkInterfaceSubType.Cellular_EDGE:
                // 2GP
                break;
            case NetworkInterfaceSubType.Cellular_EVDO:
            case NetworkInterfaceSubType.Cellular_EVDV:
                // ??
                break;
            case NetworkInterfaceSubType.Cellular_1XRTT:
                // ??
                break;
            case NetworkInterfaceSubType.Cellular_3G:
                // 3G
                break;
            case NetworkInterfaceSubType.Cellular_HSPA:
                // 3GP
                break;
            case NetworkInterfaceSubType.Unknown:
                // Unknown? 4G is Unknown
                break;
        }
        break;
    case NetworkInterfaceType.Wireless80211:
        // WIFI
        break;
}

请注意,对于 Windows Phone 7 项目,我们没有“4G 类型”,当我使用设备测试 4G 时,NetworkInterfaceType 是未知的......但是,如果我们在 32G 或 3G 中遇到网络问题,则为未知也..

我没有使用 Windows Phone 8 项目进行测试,但理论上,它适用于 4G。

此外,您可以在此处了解有关网络类型的更多详细信息。

PS:为了测试网络,没有什么比用真机测试更好的了,因此,一个真实的网络。

于 2013-08-14T13:21:54.713 回答