简体   繁体   English

如何在不打开套接字的情况下访问Windows Phone 7特定的网络类型(例如EDGE,3G等)?

[英]How to access Windows Phone 7 specific network type (e.g. EDGE, 3G, etc.) without opening a socket?

As I understand, before the Mango SDK update (7.1), you were only able to access a rather broad network type via the property NetworkInterface.NetworkInterfaceType . 据我了解,在Mango SDK更新(7.1)之前,您只能通过属性NetworkInterface.NetworkInterfaceType访问相当广泛的网络类型。 This would return an enumeration like Wireless80211 , MobileBroadbandGSM , or MobileBroadbandCDMA . 这将返回类似Wireless80211MobileBroadbandGSMMobileBroadbandCDMA的枚举。

After the release of the Mango SDK, we are now able to access the NetworkInterfaceSubtype via an open socket using a call similar to this: socket.GetCurrentNetworkInterface(); 在Mango SDK发布之后,我们现在可以使用类似于以下的调用通过开放套接字访问NetworkInterfaceSubtype: socket.GetCurrentNetworkInterface(); A property of the returned object ( NetworkInterfaceInfo.InterfaceSubtype ) will give you more specific network information such as Cellular_EDGE , Cellular_HSPA , or Cellular_EVDV . 返回对象的属性( NetworkInterfaceInfo.InterfaceSubtype )将为您提供更具体的网络信息,如Cellular_EDGECellular_HSPACellular_EVDV This is the information I need. 这是我需要的信息。

The most efficient way I have found to access this is to open an async host name resolution request and grab the information in the async callback function, such as below (borrowed from this post: How can I check for 3G, wifi, EDGE, Cellular Networks in Windows Phone 7? ): 我发现访问它的最有效方法是打开异步主机名解析请求并获取异步回调函数中的信息,如下所示(借用这篇文章: 我如何检查3G,wifi,EDGE,Cellular Windows Phone 7中的网络? ):

DeviceNetworkInformation.ResolveHostNameAsync(
        new DnsEndPoint("microsoft.com", 80), 
        new NameResolutionCallback(nrr =>
            {
                var info = nrr.NetworkInterface;
                var subType = info.InterfaceSubtype;
            }), null);

What I'm looking for is a way to access this NetworkSubtype information without having to actually open a data connection. 我正在寻找的是一种访问此NetworkSubtype信息的方法,而无需实际打开数据连接。 The reason I need a passive method for querying this information is that I need to know when the network type changes, but continually opening a data connection in a loop that queries this could potentially prevent that change from taking place. 我需要一种被动方法来查询这些信息的原因是我需要知道网络类型何时发生变化,但是在循环中不断打开数据连接,查询这可能会阻止发生这种变化。

UPDATE 1: I have found through testing that, as Richard Szalay suggested, the DeviceNetworkInformation.NetworkAvailabilityChanged event does indeed fire when the handset switches network technologies (ie 3G to EDGE, or WiFi to HSPA), and you do have access to the NetworkInterfaceSubtype . 更新1:我通过测试发现,正如Richard Szalay所说,当手机切换网络技术(即3G到EDGE,或WiFi到HSPA)时, DeviceNetworkInformation.NetworkAvailabilityChanged事件确实会触发,并且您确实可以访问NetworkInterfaceSubtype I unfortunately have also found that when switching from WiFi to a cellular network technology (eg HSPA, EDGE) that the reported network subtype can often be inaccurate. 遗憾的是,我还发现,当从WiFi切换到蜂窝网络技术(例如HSPA,EDGE)时,报告的网络子类型通常可能是不准确的。 For instance, if you switch from WiFi to HSPA, the event arguments may still report a connection to WiFi when it gets fired, and no second event is fired to report HSPA. 例如,如果您从WiFi切换到HSPA,事件参数可能会在触发时报告与WiFi的连接,并且不会触发报告HSPA的第二个事件。 You are thus given the wrong connection type. 因此,您的连接类型错误。 This unreliability may make using this trigger ultimately useless, but I am going to do some network testing (without WiFi) to see if this issue is confined to WiFi switching. 这种不可靠性可能使得使用此触发器最终无用,但我将进行一些网络测试(没有WiFi)以查看此问题是否仅限于WiFi切换。 I'm hoping that it's just an issue with the WiFi radio, and that cellular network switching is reported accurately. 我希望这只是WiFi无线电的一个问题,并且准确报告了蜂窝网络切换。 I'll update this when I know more. 当我知道更多时,我会更新这个。

UPDATE 2: I have found through a lot of (driving around) testing that while the DeviceNetworkInformation.NetworkAvailabilityChanged event will get you the network changes, it does not seem possible to determine exactly what raises/triggers the event. 更新2:我已经通过大量(驾驶)测试发现,虽然DeviceNetworkInformation.NetworkAvailabilityChanged事件将使您获得网络更改,但似乎无法准确确定引发/触发事件的内容。 For instance, if you're recording the network interface each time the event is triggered, you could end up with results like: HSPA, EDGE, EDGE, EDGE, GPRS, GPRS, HSPA. 例如,如果您在每次触发事件时都记录网络接口,则最终可能会得到以下结果:HSPA,EDGE,EDGE,EDGE,GPRS,GPRS,HSPA。 The event argument object has a variable named NotificationType that is supposed to tell you the reason it was triggered, but this is always set to CharacteristicUpdate in my tests, so I have no idea why it is being triggered multiple times for the same network type (eg EDGE, EDGE, EDGE). 事件参数对象有一个名为NotificationType的变量,它应该告诉你它被触发的原因,但是在我的测试中总是设置为CharacteristicUpdate ,所以我不知道为什么它会被同一网络类型多次触发(例如EDGE,EDGE,EDGE)。 For my purposes, I am just recording the changes that have not already been recorded and ignoring the multiples. 就我的目的而言,我只记录尚未记录的变化并忽略倍数。 It is not ideal (and seems slightly less than trustworthy), but it's better than nothing, I suppose. 它不是理想的(而且似乎稍微不值得信赖),但我认为它总比没有好。

I posted the answer you grabbed the code from, and I did a bit of research for that question (including going through the reflected source of the WP7 framework). 我发布了你从中获取代码的答案,我对这个问题进行了一些研究(包括浏览WP7框架的反映来源)。

Unfortunately NetworkSubType is not publically exposed from any location that is not the result of an open connection, with host name resolution being the simplest. 遗憾的是, NetworkSubType不会从任何不是开放连接的位置公开暴露,主机名解析最简单。

The only thing I can recommend is doing a test to determine if DeviceNetworkInformation.NetworkAvailabilityChanged is fired when your data type changes (say, from 3G to H). 我唯一可以推荐的是进行测试,以确定当数据类型发生变化时(例如,从3G变为H),是否会触发DeviceNetworkInformation.NetworkAvailabilityChanged If so, you can perform another resolution at that time (though even that may prove too costly). 如果是这样,那么你可以在那时执行另一个解决方案(尽管这可能证明成本太高)。 If not, I'm afraid you're out of luck. 如果没有,我担心你运气不好。

Register to DeviceNetworkInformation.NetworkAvailabilityChanged then get the list of NetworkInterfaceSubtype this way: 注册到DeviceNetworkInformation.NetworkAvailabilityChanged然后以这种方式获取NetworkInterfaceSubtype列表:

var currentList = new NetworkInterfaceList().Where(i => i.InterfaceState == ConnectState.Connected).Select(i => i.InterfaceSubtype);
if (currentList.Contains(NetworkInterfaceSubtype.WiFi))
    Debug.WriteLine("WiFi");
if (currentList.Intersect(new NetworkInterfaceSubType[]
{
    NetworkInterfaceSubtype.Cellular_EVDO,
    NetworkInterfaceSubtype.Cellular_3G,
    NetworkInterfaceSubtype.Cellular_HSPA,
    NetworkInterfaceSubtype.Cellular_EVDV,
}).Any())
    Debug.WriteLine("3G");
if (currentList.Intersect(new NetworkInterfaceSubType[]
{
    NetworkInterfaceSubtype.Cellular_GPRS,
    NetworkInterfaceSubtype.Cellular_1XRTT,
    NetworkInterfaceSubtype.Cellular_EDGE,
}).Any())
    Debug.WriteLine("2G");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Silverlight中获取默认滚动条的子元素(例如,repeatbutton,thumb等)? - How to get child element of default scrollbar(e.g. repeatbutton, thumb, etc.) in silverlight? 如何在Windows Phone 7中检查3G,wifi,EDGE,蜂窝网络? - How can I check for 3G, wifi, EDGE, Cellular Networks in Windows Phone 7? 如何从hWnd获取Windows 10商店应用程序的“应用程序名称”(例如Edge) - How to get the “Application Name” from hWnd for Windows 10 Store Apps (e.g. Edge) 如何使用 xamarin Essential 确定网络类型是 2g、3g 还是 4g - how to determine if network type is 2g, 3g or 4g using xamarin essential 封装“类成员”的术语(例如,字段,属性,方法等) - Encapsulating term for 'kind of class member' (e.g. field, property, method, etc.) 如何在Windows Phone中获取父容器,例如PhoneApplicationPage? - How can I get the parent container, e.g. PhoneApplicationPage, in Windows Phone? 如何在Windows应用程序中为歌曲设置专辑封面(例如,没有tag-lib#)? - How can I set album art for a song in a Windows App (e.g. without tag-lib#)? 如何在运行时检查操作系统版本,例如在 Windows 或 Linux 上,而不使用条件编译语句 - How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement url 参数传递电话号码例如+1234,如何检索它例如abc.com/save/xyz/+123 - url parameter passing phone number e.g. +1234 , how to retrieve it e.g. abc.com/save/xyz/+123 如何在Windows中获取产品密钥(例如Adobe,Office)? - How to Get Product Keys (e.g. Adobe, Office) in Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM