You Are Here:

Community: Wiki

This page was last modified on 18 February 2009, at 01:05.

Obtaining the device IMEI Synchronously

From Forum Nokia Wiki

Due to the dangerous nature of obtaining the IMEI using a nested active scheduler, it often makes more sense to use a separate thread and get the IMEI on that.

Note that you should actually use an active object with the CAknWaitDialog to get the IMEI, but doing this is often impractical, so instead here is a solution that spawns a thread and gets the IMEI from the spawned thread.

Here is the code to get the IMEI

TBuf<32> imei;
User::LeaveIfError(GetIMEI(imei));

Header file

Here is the code for the active object

class CIMEILoader : public CActive
{
public:
CIMEILoader(TDes& aIMEI);
~CIMEILoader();
 
void ConstructL();
 
void Start();
 
void IMEI(TDes& aIMEI);
 
private:
void RunL();
void DoCancel();
 
private:
CTelephony* iTelephony;
TDes& iIMEI;
CTelephony::TPhoneIdV1 iId;
};

Source file

CIMEILoader::CIMEILoader(TDes& aIMEI)
: CActive(EPriorityNormal),
iIMEI(aIMEI)
{
CActiveScheduler::Add(this);
}
 
CIMEILoader::~CIMEILoader()
{
Cancel();
 
delete iTelephony;
iTelephony = NULL;
}
 
void CIMEILoader::ConstructL()
{
iTelephony = CTelephony::NewL();
}
 
void CIMEILoader::Start()
{
CTelephony::TPhoneIdV1Pckg pkg(iId);
iTelephony->GetPhoneId(iStatus, pkg);
SetActive();
}
 
void CIMEILoader::RunL()
{
CActiveScheduler::Stop();
iIMEI.Copy(iId.iSerialNumber);
}
 
void CIMEILoader::DoCancel()
{
iTelephony->CancelAsync(CTelephony::EGetPhoneIdCancel);
}

Here is the code for the thread

LOCAL_C void TelephonyHandlerL(TDes& aIMEI)
{
CActiveScheduler* sched = new (ELeave) CActiveScheduler();
CleanupStack::PushL(sched);
CActiveScheduler::Install(sched);
 
CIMEILoader* loader = new (ELeave) CIMEILoader(aIMEI);
CleanupStack::PushL(loader);
 
loader->ConstructL();
loader->Start();
CActiveScheduler::Start();
 
// Make sure that if we completed it was successfully
User::LeaveIfError(loader->iStatus.Int());
 
// When loader completes the function resumes here
CleanupStack::PopAndDestroy(2, sched);
}
 
 
LOCAL_C TInt HandlerThreadProc(TAny* aAny)
{
__UHEAP_MARK; // Heap checking
 
CTrapCleanup* cleanup=CTrapCleanup::New();
TInt err=KErrNoMemory;
if (cleanup)
{
TRAP(err, TelephonyHandlerL(REINTERPRET_CAST(TDes&, *aAny)));
delete cleanup;
}
 
__UHEAP_MARKEND;
 
return err;
}


Finally the helper launch the thread and get the IMEI

const TInt KTelMinHeap = 0x1000;
const TInt KTelMaxHeap = 0x4000;
_LIT(KThreadName, "TelephonyHelper");
 
TInt GetIMEI(TDes& aIMEI)
{
RThread theThread;
TInt err = theThread.Create(KThreadName, HandlerThreadProc, KDefaultStackSize, KTelMinHeap, KTelMaxHeap, &aIMEI);
if (err == KErrNone)
{
TRequestStatus status;
theThread.Logon(status);
theThread.Resume();
User::WaitForRequest(status);
 
theThread.Close();
err = status.Int();
}
 
return err;
}

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fHowE5ftoE5fcreateE5faE5fCountryE2dInfoE5fWidgetE5fE28WE52TE29X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZseriesE5f60Q qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZuserE5ftagQSxs60X qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx4E2E0000X