C# .NET Connect and Disconnect Modem Using MBN API Mobile Broadband Adaptor

Complete example showing how to use the Microsoft MBN API to get a mobile broadband adaptor to connect and disconnect the 3G Modem

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;

// Add a reference to COM Tab: 'Definition: UCM Extension API for MBN Type Library' 
using MbnApi; 

namespace MBN_Test
{
    class ConnectionEventsSink : IMbnConnectionEvents
    {
        public ConnectionEventsSink() { }
        public void OnConnectComplete (IMbnConnection newConnection,  uint requestID,  int status)
        {
            Console.WriteLine("OnConnectComplete");
        }
        public void OnConnectStateChange(IMbnConnection newConnection)
        {
            MBN_ACTIVATION_STATE activationState;
            string profileName;
            newConnection.GetConnectionState(out activationState, out profileName);
            Console.WriteLine("OnConnectStateChange - " + profileName + " - " + activationState);
        }

        public void OnDisconnectComplete(IMbnConnection newConnection, uint requestID, int status)
        {
            Console.WriteLine("OnDisconnectComplete");
        }

        public void OnVoiceCallStateChange(IMbnConnection newConnection)
        {
            Console.WriteLine("OnVoiceCallStateChange");
        }

    }
    
    class Program
    {
        static void Main(string[] args)
        {
            // Get first interface ID
            MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager();
            IMbnInterfaceManager infMgr = (IMbnInterfaceManager)mbnInfMgr;
            IMbnInterface[] interfaces = (IMbnInterface[])infMgr.GetInterfaces();
            IMbnInterface inf = interfaces[0];


            MbnConnectionManager mbnConnMgr = new MbnConnectionManager();
            IMbnConnectionManager conMgr = (IMbnConnectionManager)mbnConnMgr;


            IConnectionPointContainer icpc;
            icpc = (IConnectionPointContainer)mbnConnMgr;

            Guid IID_IMbnConnectionEvents = typeof(IMbnConnectionEvents).GUID;
            IConnectionPoint icp;
            icpc.FindConnectionPoint(ref IID_IMbnConnectionEvents, out icp);

            ConnectionEventsSink connEvtsSink = new ConnectionEventsSink();
            uint cookie;
            icp.Advise(connEvtsSink, out cookie);

            // Make sure the radio is turned on before here
            uint requestID = 0;
            IMbnConnection conn = inf.GetConnection();

            // CONNECT HERE - AND WAIT FOR EVENTS
            Console.WriteLine("Connect - wait for events");
            conn.Connect(MBN_CONNECTION_MODE.MBN_CONNECTION_MODE_PROFILE, "voda UK", out requestID);
            Console.WriteLine("  RequestID: " + requestID.ToString());

            // WAIT 60 SECONDS FOR EVENTS TO FIRE
            System.Threading.Thread.Sleep(60000); 

            // DISCONNECT HERE
            Console.WriteLine("Disconnect - wait for events");
            conn.Disconnect(out requestID);
            Console.WriteLine("  RequestID: " + requestID.ToString());
            
            Console.ReadKey();

        }
    }
}