Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Read Windows Mobile Broadband connection properties

First up, here's the background:

We have a Windows Forms application (written in C#, .NET Framework 3.5) currently running on full Windows 7 tablets, which have a 3G module built in that is used for data connectivity. The data connection is configured as a normal mobile broadband connection in Windows (so Windows manages the connectivity itself), and the connection shows up in Control Panel > Network and Internet > Network Connections and it works fine - the application is able to communicate over the internet with our web service. We will be moving onto a different device (likely a full Windows 8-based tablet) at some point in the future.

Now, what I need to do is read the connection status of this Mobile Broadband connection; i.e. get the signal strength, and the carrier name (e.g. Vodafone UK). I've found a way to do this using the Mobile Broadband API part of the Windows 7 SDK (see here and here), however this appears to be OS specific as it doesn't work on Windows 8 - or at least not with the device I have here.

Is there a generic way to read the mobile broadband connection properties using the .NET framework?

Alternatively, does anyone know of a Windows 8 SDK which contains a Mobile Broadband API like the Windows 7 one I'm currently using?

Thanks in advance.

Update - I've got this working on a range of different Win 7 / Win 8 devices now. Even the Lenovo device is working OK. I'll post up example code for the main bits (Reading connection status, configuring the connection, checking the SIM status) as answers; the code is a little too long to go into the question, annoyingly.

like image 503
Paul F Avatar asked Sep 02 '13 16:09

Paul F


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Configuring a connection programmatically (you will need the APN details):

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
                    IMbnSubscriberInformation subInfo = mobileInterfaces[0].GetSubscriberInformation();

                    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));
                                context.AppendChild(xdoc.CreateElement("AccessString", xdoc["MBNProfile"].NamespaceURI));
                                XmlElement userLogonCred = (XmlElement)context.AppendChild(xdoc.CreateElement("UserLogonCred", xdoc["MBNProfile"].NamespaceURI));
                                userLogonCred.AppendChild(xdoc.CreateElement("UserName", xdoc["MBNProfile"].NamespaceURI));
                                userLogonCred.AppendChild(xdoc.CreateElement("Password", xdoc["MBNProfile"].NamespaceURI));
                                context.AppendChild(xdoc.CreateElement("Compression", xdoc["MBNProfile"].NamespaceURI));
                                context.AppendChild(xdoc.CreateElement("AuthProtocol", 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);
        }
like image 129
Paul F Avatar answered Oct 12 '22 16:10

Paul F