我在2008-2009为 Windows mobile 6.1 创建了一个 GPS程序,可以在后台实时跟踪位置。它在这些设备上运行良好多年。出于某种原因,相同的代码在 Windows Mobile 6.5 上从未完美运行。
经过几个小时的操作(大多数时候没人使用设备),我从“WaitForMultipleObjects”函数收到一个“Timeout”(代码258):
this.GPSEvent_WaitValue = WaitForMultipleObjects(2, this.GPSEvent_Handles, 0, 45000);
同样,这可以工作几个小时,突然间,如果没有以下情况,就不可能获得另一个职位: 的更新: - 重启设备(GoogleMap确认没有GPS设备!)
这与Windows Mobile睡眠和减慢我的线程有关。
这是核心代码(改编自Microsoft SDK示例):
/// <summary>
/// When "WindowsMobile" wake up the program to check for a new position
/// </summary>
private void OnNextGPSEvent_Callback()
{
int SecondsToNextWakeUp = ETL.Mobile.Device.ScheduledCallback.MINIMUM_SECONDTONEXTWAKEUP;
switch (this.SleepingState)
{
case SleepingStateType.SleepingForNextPosition:
// Get position
this.GPSEvent_WaitValue = (WaitForEventThreadResultType)WaitForMultipleObjects(2, this.GPSEvent_Handles, 0, 45000);
switch (this.GPSEvent_WaitValue)
{
case WaitForEventThreadResultType.Event_LocationChanged:
// Got a new position
this.FireLocationChanged(this.GetCurrentPosition());
// Manage device shutdown (save battery)
if (this.PositionFrequency > MIN_SECONDS_FREQUENCY_FORDEVICE_SHUTDOWN)
{
// Close device
this.CloseDevice();
SecondsToNextWakeUp = (this.PositionFrequency - GPSDEVICE_LOAD_SECONDS_LOAD_TIME);
this.SleepingState = SleepingStateType.SleepingBeforeDeviceWakeUp;
}
else
{
// Default Wait Time
this.SleepingState = SleepingStateType.SleepingForNextPosition;
}
break;
case WaitForEventThreadResultType.Event_StateChanged:
break;
case WaitForEventThreadResultType.Timeout:
case WaitForEventThreadResultType.Failed:
case WaitForEventThreadResultType.Stop:
// >>>>>>>>>>>>>> This is where the error happens <<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>> This is where the error happens <<<<<<<<<<<<<<<<<<<<<<<<<<<
// >>>>>>>>>>>>>> This is where the error happens <<<<<<<<<<<<<<<<<<<<<<<<<<<
// Too many errors
this.ConsecutiveErrorReadingDevice++;
if (this.ConsecutiveErrorReadingDevice > MAX_ERRORREADINGDEVICE)
{
this.CloseDevice();
SecondsToNextWakeUp = (this.PositionFrequency - GPSDEVICE_LOAD_SECONDS_LOAD_TIME);
this.SleepingState = SleepingStateType.SleepingBeforeDeviceWakeUp;
}
else
{
// Default Wait Time
this.SleepingState = SleepingStateType.SleepingForNextPosition;
}
break;
}
#endregion
break;
case SleepingStateType.SleepingBeforeDeviceWakeUp:
this.OpenDevice();
SecondsToNextWakeUp = GPSDEVICE_LOAD_SECONDS_LOAD_TIME;
this.SleepingState = SleepingStateType.SleepingForNextPosition;
break;
}
if (this.IsListeningGPSEvent)
{
// Ajustement du prochain rappel
this.NextGPSEvent_Callback.SecondToNextWakeUp = SecondsToNextWakeUp;
this.NextGPSEvent_Callback.RequestWakeUpCallback();
}
}
/// <summary>
///Create Thread
/// </summary>
private void StartListeningThreadForGPSEvent()
{
// We only want to create the thread if we don't have one created already and we have opened the gps device
if (this._GPSEventThread == null)
{
// Create and start thread to listen for GPS events
this._GPSEventThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.ListeningThreadForGPSEvent));
this._GPSEventThread.Start();
}
}
private void ListeningThreadForGPSEvent()
{
this.GPSEvent_WaitValue = WaitForEventThreadResultType.Stop;
this.IsListeningGPSEvent = true;
// Allocate handles worth of memory to pass to WaitForMultipleObjects
this.GPSEvent_Handles = Helpers.LocalAlloc(12);
Marshal.WriteInt32(this.GPSEvent_Handles, 0, this._StopHandle.ToInt32());
Marshal.WriteInt32(this.GPSEvent_Handles, 4, this._NewLocationHandle.ToInt32());
Marshal.WriteInt32(this.GPSEvent_Handles, 8, this._GPSDeviceStateChanged.ToInt32());
this.Start_NextGPSEvent_Timer(this.PositionFrequency);
this.SleepingState = SleepingStateType.SleepingBeforeDeviceWakeUp;
this.OnNextGPSEvent_Callback();
}
答案 0 :(得分:0)
Windows Mobile 5/6 SDK附带的C#GPS示例是否存在同样的问题?如果是这样,那么这可能是驱动程序问题,您需要与硬件制造商或OEM合作。
您可以通过轮询GPS驱动程序而不是依赖于中间驱动程序界面的位置更改回调来缓解此问题。
没有重置GPS硬件的标准方法。当程序正在使用它并且在没有程序正在使用它时“关闭”(或在非常低功率模式下)时,GPS处于“打开”状态。如果您的GPS在WWAN芯片上,您可以通过关闭和打开WWAN调制解调器来重置它。或者,一些GPS芯片接受ASCII或二进制串行命令来重置GPS本身,但这些是芯片组特定的。您需要与GPS芯片组制造商核实这些代码。
我假设您的GPSID重置代码来自here,与强制所有GPSID客户端断开连接相同,这会使GPS处于低功耗状态,但实际上可能无法重置硬件。