我正在尝试使用Mobile Broadband API启动和运行。我在 C# 中使用它,使用此处找到的说明。
但是我有一个问题:当调制解调器设备被锁定时(即需要 PIN),我想以编程方式设置 pin 并继续建立连接,但后者失败并显示“需要 Pin”,即使我刚刚正确设置了 PIN。
移动宽带 API 提供了IMbnPin
设置 pin 的接口,但这是一个异步操作,因此您必须注册OnEnterComplete
事件(IMbnPinEvents
接口的一部分),它表示操作已完成。我认为这应该足够了,但显然不是。
下面的类是一个最小的、独立的示例,用于演示该问题。您可以在这样的控制台应用程序中使用它:
var testInstance = new MbnTest();
testInstance.Test("XXXX"); // replace with actual PIN code
(您还需要一个 Interop dll 来编译它,您可以在此处找到)
测试类包含一个 ManualResetEvent 字段,用于在 Enter 和 OnEnterComplete 方法之间进行协调。
在Test方法中,订阅IMbnPinEvents,实例化IMbnPin,调用(异步)Enter
方法,调用ManualResetEvent.WaitOne
阻塞当前线程。在 OnEnterComplete 中,我可以验证引脚是否已正确设置,然后发出信号,ManualResetEvent
以便测试方法继续执行。如果我在此之后立即继续TryToGetConnectionState()
通话,则会收到异常 E_MBN_PIN_REQUIRED (0x80548210)。如果我使用 Console.ReadLine() 等待“足够长”,一切正常。所以看起来我需要等待另一个事件,但我找不到哪个事件。
有任何想法吗?我错过了什么?
// warning: this is sample code, needs to be better structured for production use
class MbnTest : IMbnPinEvents
{
private readonly ManualResetEvent _resetEventPin = new ManualResetEvent(false);
public void Test(string pinCode)
{
var interfacemanager = (IMbnInterfaceManager)new MbnInterfaceManager();
SubscribeToPinEvents(interfacemanager);
var mbnPin = GetPin(interfacemanager);
uint requestId;
Trace.WriteLine("Setting PIN");
mbnPin.Enter(pinCode, out requestId);
Trace.WriteLine("Waiting for OnEnterComplete");
// wait for the OnEnterComplete event
_resetEventPin.WaitOne();
Trace.WriteLine("press enter to retrieve connection state");
Console.ReadLine();
TryToGetConnectionState();
}
void IMbnPinEvents.OnEnterComplete(IMbnPin pin, ref MBN_PIN_INFO pinInfo, uint requestID, int status)
{
// reports MBN_PIN_STATE_NONE which means no pin is required
Trace.WriteLine(string.Format("OnEnterComplete: pin state = {0}", pinInfo.pinState));
// signal the ManualResetEvent to unblock the thread waiting for the Enter Pin operation to complete
_resetEventPin.Set();
}
private void SubscribeToPinEvents(IMbnInterfaceManager interfacemanager)
{
Trace.WriteLine("Subscribing to IMbnPinEvents");
var guidPinEvents = typeof (IMbnPinEvents).GUID;
var connectionPointContainer = (IConnectionPointContainer) interfacemanager;
IConnectionPoint connectionPoint;
connectionPointContainer.FindConnectionPoint(ref guidPinEvents, out connectionPoint);
uint cookie;
connectionPoint.Advise(this, out cookie);
}
private static IMbnPin GetPin(IMbnInterfaceManager interfacemanager)
{
IMbnInterface mbnInterface = interfacemanager.GetInterfaces().OfType<IMbnInterface>().First();
Trace.WriteLine(string.Format("mbnInterface: {0}", mbnInterface.GetReadyState()));
var pinMgr = (IMbnPinManager)mbnInterface;
var mbnPin = pinMgr.GetPin(MBN_PIN_TYPE.MBN_PIN_TYPE_PIN1);
return mbnPin;
}
private static void TryToGetConnectionState()
{
Trace.WriteLine("Retrieving mbn connection");
var connectionManager = (IMbnConnectionManager)new MbnConnectionManager();
var mbnConnection = connectionManager.GetConnections().OfType<IMbnConnection>().First();
Trace.WriteLine(string.Format("connection: {0}", mbnConnection.ConnectionID));
MBN_ACTIVATION_STATE state;
string profilename;
mbnConnection.GetConnectionState(out state, out profilename);
}
void IMbnPinEvents.OnChangeComplete(IMbnPin pin, ref MBN_PIN_INFO pinInfo, uint requestID, int status)
{
throw new NotImplementedException();
}
void IMbnPinEvents.OnEnableComplete(IMbnPin pin, ref MBN_PIN_INFO pinInfo, uint requestID, int status)
{
throw new NotImplementedException();
}
void IMbnPinEvents.OnDisableComplete(IMbnPin pin, ref MBN_PIN_INFO pinInfo, uint requestID, int status)
{
throw new NotImplementedException();
}
void IMbnPinEvents.OnUnblockComplete(IMbnPin pin, ref MBN_PIN_INFO pinInfo, uint requestID, int status)
{
throw new NotImplementedException();
}
}