Searching for GPS devices using Symbian C++
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
The article is believed to be still valid for the original topic scope.
The article is believed to be still valid for the original topic scope.
Contents |
Overview
The following example goes through the GPS devices of the mobile phone and selects the device used for GPS positioning.
If the code finds both an external Bluetooth GPS device and an internal GPS device, it selects the internal one.
MMP file
The following capabilities and libraries are required:
CAPABILITY Location
LIBRARY Lbs.lib
Header file
#include <lbs.h>Source file
// 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();
}
}
}


(no comments yet)