C# .Net Set Radio State Using MBN APN Mobile Broadband Adapter

Complete example showing the software setting of the radio state for a 3G Modem using the MBN mobile Broadband API

Note that on some laptops it is also possible to set the ‘hardware’ radio state. i.e. by switching a switch 🙂 – so check its turned on before trying this

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 RadioEventsSink : IMbnRadioEvents
    {
        public RadioEventsSink() { }
        public void OnRadioStateChange(IMbnRadio newInterface)
        {
            //Global.g_IMbnRadio = newInterface;

            Console.WriteLine("OnRadioStateChange");
            Console.WriteLine("   Hardware State: " + newInterface.HardwareRadioState.ToString());
            Console.WriteLine("   Software State: " + newInterface.SoftwareRadioState.ToString());
        }

        public void OnSetSoftwareRadioStateComplete(IMbnRadio newInterface, uint requestID, int Status)
        {
            //Global.g_IMbnRadio = newInterface;

            Console.WriteLine("OnSetSoftwareRadioStateComplete");
            Console.WriteLine("   RequestID: " + requestID.ToString());
            Console.WriteLine("   Hardware State: " + newInterface.HardwareRadioState.ToString());
            Console.WriteLine("   Software State: " + newInterface.SoftwareRadioState.ToString());
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {

            MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager();
            IMbnInterfaceManager infMgr = (IMbnInterfaceManager)mbnInfMgr;

            IMbnInterface[] interfaces = (IMbnInterface[])infMgr.GetInterfaces();
            IMbnInterface inf = interfaces[0];

            IConnectionPointContainer icpc;
            icpc = (IConnectionPointContainer)mbnInfMgr;

            Guid IID_IMbnRadioEvents = typeof(IMbnRadioEvents).GUID;
            IConnectionPoint icp;
            icpc.FindConnectionPoint(ref IID_IMbnRadioEvents, out icp);

            RadioEventsSink radioEvtsSink = new RadioEventsSink();
            uint cookie;
            icp.Advise(radioEvtsSink, out cookie);

            IMbnRadio radio = (IMbnRadio)inf;
            uint requestID = 0;

            //call SetSoftwareRadioState to set the software radio state 
            Console.WriteLine("Set radio off - wait for events");
            radio.SetSoftwareRadioState(MBN_RADIO.MBN_RADIO_OFF, out requestID);
            Console.WriteLine("  RequestID: " + requestID.ToString());

            System.Threading.Thread.Sleep(10000); // wait 10 seconds for events to fire

            Console.WriteLine("Set radio on - wait for events");
            radio.SetSoftwareRadioState(MBN_RADIO.MBN_RADIO_ON, out requestID);
            Console.WriteLine("  RequestID: " + requestID.ToString());
            
            Console.ReadKey();

        }
    }
}