微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#阅读Windows Mobile宽带连接属性

首先,这是背景:

我们有一个Windows Forms应用程序(用C#,.NET Framework 3.5编写),当前运行在完整的Windows 7平板电脑上,内置了一个用于数据连接的3G模块。 数据连接在Windows中configuration为正常的移动宽带连接(所以Windows本身就是pipe理连接),连接显示在控制面板>networking和Internet>networking连接中,并且工作正常 – 应用程序能够通过互联网与我们的networking服务。 我们将在未来的某个时间转移到其他设备(可能是完整的基于Windows 8的平板电脑)。

现在,我需要做的是读取移动宽带连接的连接状态。 即获得信号强度,以及运营商名称(如沃达丰英国)。 我已经find了使用Windows 7 SDK的移动宽带API部分的方法(请参阅此处和此处 ),但是这似乎是操作系统特定的,因为它在Windows 8上不起作用 – 或者至less不是设备,我在这里

有没有一种通用的方式来阅读使用.NET框架的移动宽带连接属性

在低于10的Windows版本上安装ClickOnce应用程序时出错

如何在一个或多个for循环中使用现有的variables?

如何检测串口是否在使用.net

在Windows 10上通过代码启用Tablet模式?

Windows服务挂起在启动过程中启动

另外,有没有人知道Windows 8 SDK,其中包含我正在使用的Windows 7移动宽带API?

提前致谢。

更新 – 我现在已经在一系列不同的Win 7 / Win 8设备上工作了。 即使联想设备工作正常。 我将发布主要位的示例代码(读取连接状态,configuration连接,检查SIM卡状态)作为答案; 令人烦恼的是,代码有点太长了。

在VB.Net中从二进制文件提取string

将应用程序更新部署到多个站点

从C#应用程序获取WindowsExplorer中的当前select?

Windows API触发壁纸洗牌

正则expression式的困扰C#

移动宽带API也可在Windows 8桌面上使用。

如果您使用Windows 8 Metro / RT /任何名称,则需要这些WindowsRT API (Windows.Connectivity.networkinformation等)。

以编程方式配置连接 (您将需要APN详细信息):

try { MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager(); IMbnInterfaceManager mbnInfMgrInterface = mbnInfMgr as IMbnInterfaceManager; if (mbnInfMgrInterface != null) { IMbnInterface[] mobileInterfaces = mbnInfMgrInterface.GetInterfaces() as IMbnInterface[]; if (mobileInterfaces != null && mobileInterfaces.Length > 0) { // Just use the first interface IMbnSubscriber@R_496_4045@ion subInfo = mobileInterfaces[0].GetSubscriber@R_496_4045@ion(); if (subInfo != null) { SIMNumber = subInfo.Simiccid; // Get the connection profile MbnConnectionProfileManager mbnConnProfileMgr = new MbnConnectionProfileManager(); IMbnConnectionProfileManager mbnConnProfileMgrInterface = mbnConnProfileMgr as IMbnConnectionProfileManager; if (mbnConnProfileMgrInterface != null) { bool connProfileFound = false; string profileName = String.Empty; try { IMbnConnectionProfile[] mbnConnProfileInterfaces = mbnConnProfileMgrInterface.GetConnectionProfiles(mobileInterfaces[0]) as IMbnConnectionProfile[]; foreach (IMbnConnectionProfile profile in mbnConnProfileInterfaces) { string xmlData = profile.GetProfileXmlData(); if (xmlData.Contains("<Simiccid>" + SIMNumber + "</Simiccid>")) { connProfileFound = true; bool updaterequired = false; // Check if the profile is set to auto connect XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(xmlData); profileName = xdoc["MBNProfile"]["Name"].InnerText; if (xdoc["MBNProfile"]["ConnectionMode"].InnerText != "auto") { xdoc["MBNProfile"]["ConnectionMode"].InnerText = "auto"; updaterequired = true; } // Check the APN settings if (xdoc["MBNProfile"]["Context"] == null) { XmlElement context = (XmlElement)xdoc["MBNProfile"].AppendChild(xdoc.CreateElement("Context",xdoc["MBNProfile"].NamespaceURI)); context.AppendChild(xdoc.CreateElement("Accessstring",xdoc["MBNProfile"].NamespaceURI)); context.AppendChild(xdoc.CreateElement("Compression",xdoc["MBNProfile"].NamespaceURI)); context.AppendChild(xdoc.CreateElement("AuthProtocol",xdoc["MBNProfile"].NamespaceURI)); updaterequired = true; } if (xdoc["MBNProfile"]["Context"]["Accessstring"].InnerText != APNAccessstring) { xdoc["MBNProfile"]["Context"]["Accessstring"].InnerText = APNAccessstring; updaterequired = true; } if (xdoc["MBNProfile"]["Context"]["Compression"].InnerText != APNCompression) { xdoc["MBNProfile"]["Context"]["Compression"].InnerText = APNCompression; updaterequired = true; } if (xdoc["MBNProfile"]["Context"]["AuthProtocol"].InnerText != APNAuthProtocol) { xdoc["MBNProfile"]["Context"]["AuthProtocol"].InnerText = APNAuthProtocol; updaterequired = true; } if (xdoc["MBNProfile"]["Context"]["UserlogonCred"] == null && !String.IsNullOrEmpty(APNUsername)) { XmlElement userlogonCred = (XmlElement)xdoc["MBNProfile"]["Context"].InsertAfter(xdoc.CreateElement("UserlogonCred",xdoc["MBNProfile"].NamespaceURI),xdoc["MBNProfile"]["Context"]["Accessstring"]); userlogonCred.AppendChild(xdoc.CreateElement("UserName",xdoc["MBNProfile"].NamespaceURI)); userlogonCred.AppendChild(xdoc.CreateElement("Password",xdoc["MBNProfile"].NamespaceURI)); updaterequired = true; } if (xdoc["MBNProfile"]["Context"]["UserlogonCred"] != null && xdoc["MBNProfile"]["Context"]["UserlogonCred"]["UserName"].InnerText != APNUsername) { xdoc["MBNProfile"]["Context"]["UserlogonCred"]["UserName"].InnerText = APNUsername; updaterequired = true; } if (xdoc["MBNProfile"]["Context"]["UserlogonCred"] != null && xdoc["MBNProfile"]["Context"]["UserlogonCred"]["Password"] == null && !String.IsNullOrEmpty(APNUsername)) { xdoc["MBNProfile"]["Context"]["UserlogonCred"].AppendChild(xdoc.CreateElement("Password",xdoc["MBNProfile"].NamespaceURI)); } if (xdoc["MBNProfile"]["Context"]["UserlogonCred"] != null && xdoc["MBNProfile"]["Context"]["UserlogonCred"]["Password"].InnerText != APNPassword) { xdoc["MBNProfile"]["Context"]["UserlogonCred"]["Password"].InnerText = APNPassword; updaterequired = true; } if (updaterequired) { // Update the connection profile profile.UpdateProfile(xdoc.OuterXml); } } } } catch (Exception ex) { if (!ex.Message.Contains("Element not found")) { throw ex; } } if (!connProfileFound) { // Create the connection profile XmlDocument xdoc = new XmlDocument(); xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0","utf-8","yes")); XmlElement mbnProfile = (XmlElement)xdoc.AppendChild(xdoc.CreateElement("MBNProfile","http://www.microsoft.com/networking/WWAN/profile/v1")); mbnProfile.AppendChild(xdoc.CreateElement("Name",xdoc["MBNProfile"].NamespaceURI)).InnerText = SIMNumber; mbnProfile.AppendChild(xdoc.CreateElement("IsDefault",xdoc["MBNProfile"].NamespaceURI)).InnerText = "true"; mbnProfile.AppendChild(xdoc.CreateElement("ProfileCreationType",xdoc["MBNProfile"].NamespaceURI)).InnerText = "DeviceProvisioned"; mbnProfile.AppendChild(xdoc.CreateElement("SubscriberID",xdoc["MBNProfile"].NamespaceURI)).InnerText = subInfo.SubscriberID; mbnProfile.AppendChild(xdoc.CreateElement("Simiccid",xdoc["MBNProfile"].NamespaceURI)).InnerText = SIMNumber; mbnProfile.AppendChild(xdoc.CreateElement("HomeProviderName",xdoc["MBNProfile"].NamespaceURI)).InnerText = SIMNumber; mbnProfile.AppendChild(xdoc.CreateElement("AutoConnectOnInternet",xdoc["MBNProfile"].NamespaceURI)).InnerText = "true"; mbnProfile.AppendChild(xdoc.CreateElement("ConnectionMode",xdoc["MBNProfile"].NamespaceURI)).InnerText = "auto"; XmlElement context = (XmlElement)xdoc["MBNProfile"].AppendChild(xdoc.CreateElement("Context",xdoc["MBNProfile"].NamespaceURI)); XmlElement userlogonCred = (XmlElement)context.AppendChild(xdoc.CreateElement("UserlogonCred",xdoc["MBNProfile"].NamespaceURI)); userlogonCred.AppendChild(xdoc.CreateElement("UserName",xdoc["MBNProfile"].NamespaceURI)); xdoc["MBNProfile"]["Context"]["Accessstring"].InnerText = APNAccessstring; xdoc["MBNProfile"]["Context"]["UserlogonCred"]["UserName"].InnerText = APNUsername; xdoc["MBNProfile"]["Context"]["UserlogonCred"]["Password"].InnerText = APNPassword; xdoc["MBNProfile"]["Context"]["Compression"].InnerText = APNCompression; xdoc["MBNProfile"]["Context"]["AuthProtocol"].InnerText = APNAuthProtocol; profileName = xdoc["MBNProfile"]["Name"].InnerText; mbnConnProfileMgrInterface.CreateConnectionProfile(xdoc.OuterXml); } // Register the connection events MbnConnectionManager connMgr = new MbnConnectionManager(); IConnectionPointContainer connPointContainer = connMgr as IConnectionPointContainer; Guid IID_IMbnConnectionEvents = typeof(IMbnConnectionEvents).GUID; IConnectionPoint connPoint; connPointContainer.FindConnectionPoint(ref IID_IMbnConnectionEvents,out connPoint); ConnectionEventsSink connEventsSink = new ConnectionEventsSink(); connPoint.Advise(connEventsSink,out cookie); if (showProgress) { MessageBox.Show("After registering events"); } // Connect IMbnConnection connection = mobileInterfaces[0].GetConnection(); if (connection != null) { MBN_ACTIVATION_STATE state; string connectionProfileName = String.Empty; connection.GetConnectionState(out state,out connectionProfileName); if (state != MBN_ACTIVATION_STATE.MBN_ACTIVATION_STATE_ACTIVATED && state != MBN_ACTIVATION_STATE.MBN_ACTIVATION_STATE_ACTIVATING) { if (String.IsNullOrEmpty(connectionProfileName)) { connectionProfileName = profileName; } uint requestID; connection.Connect(MBN_CONNECTION_MODE.MBN_CONNECTION_MODE_PROFILE,connectionProfileName,out requestID); } else { // Do nothing,already connected } } else { MessageBox.Show("Connection not found."); } } else { MessageBox.Show("mbnConnProfileMgrInterface is null."); } } else { MessageBox.Show("No subscriber info found."); } } else { MessageBox.Show("No mobile interfaces found."); } } else { MessageBox.Show("mbnInfMgrInterface is null."); } } catch (Exception ex) { if (ex.Message.Contains("SIM is not inserted.")) { SIMNumber = "No SIM inserted."; } MessageBox.Show("LoginForm.DataConnection ConfigureWindowsDataConnection Error " + ex.Message); }

读取连接状态

try { MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager(); IMbnInterfaceManager mbnInfMgrInterface = mbnInfMgr as IMbnInterfaceManager; if (mbnInfMgrInterface != null) { IMbnInterface[] mobileInterfaces = mbnInfMgrInterface.GetInterfaces() as IMbnInterface[]; if (mobileInterfaces != null && mobileInterfaces.Length > 0) { // Use the first interface,as there should only be one mobile data adapter IMbnSignal signalDetails = mobileInterfaces[0] as IMbnSignal; Int32.TryParse(signalDetails.GetSignalStrength().ToString(),out Phonesignal); Phonesignal = Convert.ToInt32(((float)Phonesignal / 16) * 100); MBN_PROVIDER provider = mobileInterfaces[0].GetHomeProvider(); PhoneNetwork = provider.providerName.ToString(); if (String.IsNullOrEmpty(SIMNumber)) { try { IMbnSubscriber@R_496_4045@ion subInfo = mobileInterfaces[0].GetSubscriber@R_496_4045@ion(); if (subInfo != null) { SIMNumber = subInfo.Simiccid; } else { SIMNumber = "Unable to read SIM info"; } } catch (Exception) { SIMNumber = "Unable to read SIM info"; } } // Check whether the connection is active IMbnConnection connection = mobileInterfaces[0].GetConnection(); if (connection != null) { MBN_ACTIVATION_STATE state; string profileName = String.Empty; connection.GetConnectionState(out state,out profileName); Connected = (state == MBN_ACTIVATION_STATE.MBN_ACTIVATION_STATE_ACTIVATED); } else { MessageBox.Show("Connection not found."); } } else { MessageBox.Show("No mobile interfaces found."); } } else { MessageBox.Show("mbnInfMgrInterface is null."); } } catch (Exception ex) { if (ex.Message.Contains("SIM is not inserted.")) { SIMNumber = "No SIM inserted."; } else { MessageBox.Show("LoginForm.DataConnection GetwindowsMobileDataStatus " + ex.Message); } Phonesignal = 0; PhoneNetwork = "UnkNown"; }

检查SIM卡是否插入并工作/激活

try { MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager(); IMbnInterfaceManager mbnInfMgrInterface = mbnInfMgr as IMbnInterfaceManager; if (mbnInfMgrInterface != null) { IMbnInterface[] mobileInterfaces = mbnInfMgrInterface.GetInterfaces() as IMbnInterface[]; if (mobileInterfaces != null && mobileInterfaces.Length > 0) { try { MBN_READY_STATE readyState = mobileInterfaces[0].GetReadyState(); switch (readyState) { case MBN_READY_STATE.MBN_READY_STATE_BAD_SIM: MessageBox.Show("The SIM is invalid (PIN Unblock Key retrials have exceeded the limit)."); break; case MBN_READY_STATE.MBN_READY_STATE_DEVICE_BLOCKED: MessageBox.Show("The device is blocked by a PIN or password which is preventing the device from initializing and registering onto the network."); break; case MBN_READY_STATE.MBN_READY_STATE_DEVICE_LOCKED: MessageBox.Show("The device is locked by a PIN or password which is preventing the device from initializing and registering onto the network."); break; case MBN_READY_STATE.MBN_READY_STATE_FAILURE: MessageBox.Show("General device failure."); break; case MBN_READY_STATE.MBN_READY_STATE_INITIALIZED: try { IMbnSubscriber@R_496_4045@ion subInfo = mobileInterfaces[0].GetSubscriber@R_496_4045@ion(); if (subInfo != null) { SIMNumber = subInfo.Simiccid; } else { SIMNumber = "Unable to read SIM info"; } } catch (Exception) { SIMNumber = "Unable to read SIM info"; } IMbnRegistration registration = mobileInterfaces[0] as IMbnRegistration; if (registration != null) { try { MBN_REGISTER_STATE regState = registration.GetRegisterState(); switch (regState) { case MBN_REGISTER_STATE.MBN_REGISTER_STATE_DENIED: // SIM Inactive simInactive = true; MessageBox.Show("The device was denied registration. The most likely cause of this error is an Inactive SIM."); break; case MBN_REGISTER_STATE.MBN_REGISTER_STATE_DEREGISTERED: // Do nothing - this is returned before the device has tried to register break; case MBN_REGISTER_STATE.MBN_REGISTER_STATE_HOME: // Do nothing break; case MBN_REGISTER_STATE.MBN_REGISTER_STATE_NONE: MessageBox.Show("The device registration state is unkNown. This state may be set upon failure of registration mode change requests."); break; case MBN_REGISTER_STATE.MBN_REGISTER_STATE_PARTNER: // Do nothing break; case MBN_REGISTER_STATE.MBN_REGISTER_STATE_ROAMING: // Do nothing break; case MBN_REGISTER_STATE.MBN_REGISTER_STATE_SEARCHING: // Do nothing break; default: MessageBox.Show("GetRegisterState returned an unexpected state: " + regState.ToString()); break; } } catch (Exception ex) { MessageBox.Show("GetRegisterState Error: " + ex.Message); } } break; case MBN_READY_STATE.MBN_READY_STATE_NOT_ACTIVATED: MessageBox.Show("The subscription is not activated."); break; case MBN_READY_STATE.MBN_READY_STATE_OFF: MessageBox.Show("The mobile broadband device stack is off."); break; case MBN_READY_STATE.MBN_READY_STATE_SIM_NOT_INSERTED: MessageBox.Show("The SIM is not inserted."); break; default: MessageBox.Show("GetReadyState returned an unexpected state: " + readyState.ToString()); break; } } catch (Exception ex) { MessageBox.Show("GetReadyState Error: " + ex.Message); } } else { MessageBox.Show("No mobileInterfaces found."); } } else { MessageBox.Show("mbnInfMgrInterface is null."); } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); }

响应连接事件 (事件在上面的例子中被注册):

public class ConnectionEventsSink : IMbnConnectionEvents { public ConnectionEventsSink() { } public void OnConnectComplete(IMbnConnection connection,uint requestID,int status) { // Un-register the connect event - you might not want to do this,depends on your own requirements. Do do this you need the cookie uint from when the events were registered. MbnConnectionManager connMgr = new MbnConnectionManager(); IConnectionPointContainer connPointContainer = connMgr as IConnectionPointContainer; Guid IID_IMbnConnectionEvents = typeof(IMbnConnectionEvents).GUID; IConnectionPoint connPoint; connPointContainer.FindConnectionPoint(ref IID_IMbnConnectionEvents,out connPoint); connPoint.Unadvise(cookie); switch (status) { case 0: MobilebroadbandTest.Connected = true; MessageBox.Show("Connected"); break; case -2141945334: MessageBox.Show("There is no SIM in the device."); break; case -2141945328: MessageBox.Show("A PIN is required for the operation to complete."); break; case -2141945335: MessageBox.Show("The network service subscription has expired."); break; case -2141945337: MessageBox.Show("The provider is not visible. This applies only to manual registration mode."); break; case -2141945340: MessageBox.Show("The connection access string is not correct."); break; case -2141945333: MessageBox.Show("An active voice call is in progress."); break; case -2141945339: MessageBox.Show("There is already an Mobile broadband context active. The Mobile broadband service does not currently support multiple active contexts."); break; case -2141945336: MessageBox.Show("The device radio is off."); break; case -2141945338: MessageBox.Show("No active attached packet service is available."); break; case -2141945326: MessageBox.Show("Generic Failure."); break; case -2141945320: MessageBox.Show("Profile is invalid."); break; case -2141945319: MessageBox.Show("Default profile exist."); break; case -2141945327: MessageBox.Show("PIN is disabled."); break; case -2141945329: MessageBox.Show("Pin is not supported."); break; case -2141945330: MessageBox.Show("Providers not found."); break; case -2141945331: MessageBox.Show("Device is not registered."); break; case -2141945332: MessageBox.Show("Visible provider cache is invalid."); break; case -2141945341: MessageBox.Show("Requested data class is not available."); break; case -2141945342: MessageBox.Show("Bad SIM is inserted."); break; case -2141945343: MessageBox.Show("Context is not activated."); break; default: MessageBox.Show("Unexpected status: " + status.ToString()); break; } } public void OnVoiceCallStateChange(IMbnConnection connection) { // Do nothing } public void OnConnectStateChange(IMbnConnection connection) { // Do nothing } public void OndisconnectComplete(IMbnConnection connection,int status) { // Do nothing }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐