×
Namespaces

Variants
Actions

搜索GPS设备

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
huwell 在 09 Oct 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS000822


  • 开发伙伴平台:

S60 3rd Edition

S60 3rd Edition, FP1


  • 详细描述

下列实例代码演示了如何检测移动手机的GPS设备,并选择该设备来做GPS定位


如果代码同时找到一个外部蓝牙GPS设备和一个内部的GPS设备,那它将选择内部的。


MMP文件

需要下列能力和链接库

CAPABILITY Location 
 
LIBRARY Lbs.lib


头文件

#include <lbs.h>


源文件

// Finds and selects GPS device
TPositionModuleId CYourClass::GetPositionModuleId()
{
TInt error = KErrNone;
 
RPositionServer ps; // TODO: Move to the class member variable
error = ps.Connect();
if (error != KErrNone)
{
return KNullUid;
}
 
TUint numModules = 0;
error = ps.GetNumModules(numModules);
if (error != KErrNone)
{
ps.Close(); // TODO: No need to close RPositionServer if it is moved
// to class member variable, then it must close in class
// destructor.
return KNullUid;
}
 
TPositionModuleId currentModuleId = KNullUid;
TPositionModuleId selectedModuleId = KNullUid;
for (TInt moduleIndex = 0; moduleIndex < numModules; moduleIndex++)
{
TPositionModuleInfo moduleInfo;
error = ps.GetModuleInfoByIndex(moduleIndex, moduleInfo);
if (error != KErrNone)
{
ps.Close(); // TODO: No need to close RPositionServer if it is
// moved to class member variable, then it must close
// in class destructor.
return selectedModuleId;
}
 
currentModuleId = moduleInfo.ModuleId();
 
TPositionModuleStatus moduleStatus;
error = ps.GetModuleStatus(moduleStatus, currentModuleId);
if (error != KErrNone ||
moduleStatus.DeviceStatus() == TPositionModuleStatus::EDeviceDisabled)
{
continue;
}
 
TPositionModuleInfo::TDeviceLocation deviceLoc =
moduleInfo.DeviceLocation();
if (deviceLoc == TPositionModuleInfo::EDeviceInternal)
{
if (selectedModuleId == KNullUid)
{
// Found first internal position module, select that
selectedModuleId = currentModuleId;
}
else
{
// Check external module should be switched to internal
TPositionModuleInfo selectedModuleInfo;
error = ps.GetModuleInfoById(selectedModuleId,
selectedModuleInfo);
if (error == KErrNone && selectedModuleInfo.DeviceLocation()
== TPositionModuleInfo::EDeviceExternal)
{
// Change external device to internal
selectedModuleId = currentModuleId;
}
}
}
else if (deviceLoc == TPositionModuleInfo::EDeviceExternal)
{
// Just pick the first external device, if none is currently
// selected
if (selectedModuleId == KNullUid)
{
// Found first enternal position module, select that
selectedModuleId = currentModuleId;
}
}
}
 
ps.Close(); // TODO: No need to close RPositionServer if it is moved to
// class member variable, then it must close in class
// destructor.
return selectedModuleId;
}
 
 
// Use selected GPS device
void CYourClass::OpenPositioner(TPositionModuleId aDevice)
{
RPositioner positioner; // TODO: Move to the class member variable
RPositionServer ps; // TODO: Move to the class member variable
 
if (aDevice != KNullUid)
{
if (ps.Connect() == KErrNone)
{
// Selected GPS device goes as 'aDevice' parameter
positioner.Open(ps,aDevice);
...
positioner.Close();
ps.Close();
}
}
}
This page was last modified on 15 December 2011, at 09:16.
76 page views in the last 30 days.