下午好
我在使用移动宽带网络 API (MBN API) 时遇到了一个奇怪的问题。使用 netshell 并运行 netsh mbn>show interfaces 时,我收到 1 个接口。这是正确的,因为我的系统上有一个宽带连接。
当我转到我的代码(适用于其他系统)时
IMbnInterface[] mobileInterfaces = mbnInfMgrInterface.GetInterfaces() as IMbnInterface[];
我没有收到任何接口。
这不应该以某种方式联系起来吗?有没有我忘记的特定设置?
我的 readstate 函数是:
private void ReadStateMobileNetwork()
{
Log.Info("Reading mobile network state");
try
{
MbnInterfaceManager mbnInfMgr = new MbnInterfaceManager();
IMbnInterfaceManager mbnInfMgrInterface = mbnInfMgr as IMbnInterfaceManager;
if (mbnInfMgrInterface != null)
{
IMbnInterface[] mobileInterfaces = mbnInfMgrInterface.GetInterfaces() as IMbnInterface[];
Log.Info("Interfaces: " + mobileInterfaces);
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 _signalStrength);
SignalStrength = Convert.ToInt32(((float)SignalStrength / 33) * 100);
MBN_PROVIDER provider = mobileInterfaces[0].GetHomeProvider();
MobileProvider = provider.providerName.ToString();
if (String.IsNullOrEmpty(SIMkaartnumber))
{
try
{
IMbnSubscriberInformation subInfo = mobileInterfaces[0].GetSubscriberInformation();
if (subInfo != null)
{
SIMkaartnumber = subInfo.SimIccID;
}
else
{
Connected = false;
SIMkaartnumber = "";
MobileProvider = "Sim unreadable";
SignalStrength = 0;
State = MobileNetworkMonitorState.UnableToReadSimInfo;
}
}
catch (Exception)
{
Connected = false;
SIMkaartnumber = "";
MobileProvider = "Sim unreadable";
SignalStrength = 0;
State = MobileNetworkMonitorState.UnableToReadSimInfo;
}
}
// Register the connection events
uint cookie;
MbnConnectionManager connMgr = new MbnConnectionManager();
IConnectionPointContainer connPointContainer = connMgr as IConnectionPointContainer;
Guid IID_IMbnConnectionEvents = typeof(IMbnConnectionEvents).GUID;
IConnectionPoint connPoint;
connPointContainer.FindConnectionPoint(ref IID_IMbnConnectionEvents, out connPoint);
// Check whether the connection is active
IMbnConnection connection = mobileInterfaces[0].GetConnection();
if (connection != null)
{
MBN_ACTIVATION_STATE state;
connection.GetConnectionState(out state, out profileName);
switch (state)
{
case MBN_ACTIVATION_STATE.MBN_ACTIVATION_STATE_NONE:
Connected = false;
MobileProvider = "";
State = MobileNetworkMonitorState.Unknown;
break;
case MBN_ACTIVATION_STATE.MBN_ACTIVATION_STATE_ACTIVATING:
Connected = false;
State = MobileNetworkMonitorState.Connecting;
break;
case MBN_ACTIVATION_STATE.MBN_ACTIVATION_STATE_ACTIVATED:
Connected = true;
State = MobileNetworkMonitorState.Connected;
break;
case MBN_ACTIVATION_STATE.MBN_ACTIVATION_STATE_DEACTIVATED:
Connected = false;
State = MobileNetworkMonitorState.NoConnection;
break;
case MBN_ACTIVATION_STATE.MBN_ACTIVATION_STATE_DEACTIVATING:
Connected = false;
State = MobileNetworkMonitorState.Disconnecting;
break;
default:
Connected = false;
State = MobileNetworkMonitorState.Unknown;
StateInfo = state.ToString();
break;
}
}
else
{
Connected = false;
MobileProvider = "No connection";
SignalStrength = 0;
State = MobileNetworkMonitorState.ConnectionNotFound;
}
}
else
{
Connected = false;
MobileProvider = "No interfaces";
SignalStrength = 0;
State = MobileNetworkMonitorState.MobileInterfacesNotFound;
}
}
else
{
Connected = false;
MobileProvider = "Interfaces null";
SignalStrength = 0;
State = MobileNetworkMonitorState.mbnInfMgrInterfaceIsNull;
}
}
catch (Exception ex)
{
Log.Debug(ex);
if (ex.Message.Contains("SIM is not inserted."))
{
Connected = false;
MobileProvider = "No SIM";
SIMkaartnumber = "";
State = MobileNetworkMonitorState.NoSimInserted;
}
else if (ex.Message.Contains("0x7FF8FB70"))
{
Connected = false;
MobileProvider = "No device";
State = MobileNetworkMonitorState.DeviceNotFound;
}
else if (ex.Message.Contains("0x80070426"))
{
Connected = false;
MobileProvider = "Service not started";
State = MobileNetworkMonitorState.ServiceNotStarted;
}
else if (ex.Message.Contains("0x80070490"))
{
Connected = false;
MobileProvider = "Unknown problem";
State = MobileNetworkMonitorState.Unknown;
}
else
{
Connected = false;
MobileProvider = "Unknown problem";
State = MobileNetworkMonitorState.Unknown;
StateInfo = ex.Message;
}
SignalStrength = 0;
}
}
谢谢您的帮助。希望有人以前遇到过这个问题并且可以帮助我。