Sunday, July 20, 2008

windows mobile get CellID

Programmatically getting the CellID from your Windows Mobile phone

http://dalelane.co.uk/blog/?p=241


he said..

I don’t know how the FindMe app is implemented, but after a little bit of playing tonight, it seems fairly easy to grab something similar to the “Cell” string they are using from the RIL_GetCellTowerInfo function in the Radio Interface Layer (RIL) API.
  1. Call RIL_Initialize to get a RIL handle
  2. Call RIL_GetCellTowerInfo to get the information about the cell tower the phone is connected to

    The “Cell” string that FindMe displays seems to be a combination of the dwCellID, dwLocationAreaCode and dwMobileCountryCode fields

  3. Call RIL_Deinitialize to release the RIL handle

and a code.. just try next week..

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace CellId
{
public class RIL
{
// string used to store the CellID string
private static string celltowerinfo = "";

/*
* Uses RIL to get CellID from the phone.
*/
public static string GetCellTowerInfo()
{
// initialise handles
IntPtr hRil = IntPtr.Zero;
IntPtr hRes = IntPtr.Zero;

// initialise result
celltowerinfo = "";

// initialise RIL
hRes = RIL_Initialize(1, // RIL port 1
new RILRESULTCALLBACK(rilResultCallback), // function to call with result
null, // function to call with notify
0, // classes of notification to enable
0, // RIL parameters
out hRil); // RIL handle returned

if (hRes != IntPtr.Zero)
{
return "Failed to initialize RIL";
}

// initialised successfully

// use RIL to get cell tower info with the RIL handle just created
hRes = RIL_GetCellTowerInfo(hRil);

// wait for cell tower info to be returned
waithandle.WaitOne();

// finished - release the RIL handle
RIL_Deinitialize(hRil);

// return the result from GetCellTowerInfo
return celltowerinfo;
}


// event used to notify user function that a response has
// been received from RIL
private static AutoResetEvent waithandle = new AutoResetEvent(false);


public static void rilResultCallback(uint dwCode,
IntPtr hrCmdID,
IntPtr lpData,
uint cbData,
uint dwParam)
{
// create empty structure to store cell tower info in
RILCELLTOWERINFO rilCellTowerInfo = new RILCELLTOWERINFO();

// copy result returned from RIL into structure
Marshal.PtrToStructure(lpData, rilCellTowerInfo);

// get the bits out of the RIL cell tower response that we want
celltowerinfo = rilCellTowerInfo.dwCellID + "-" +
rilCellTowerInfo.dwLocationAreaCode + "-" +
rilCellTowerInfo.dwMobileCountryCode;

// notify caller function that we have a result
waithandle.Set();
}



// -------------------------------------------------------------------
// RIL function definitions
// -------------------------------------------------------------------

/*
* Function definition converted from the definition
* RILRESULTCALLBACK from MSDN:
*
* http://msdn2.microsoft.com/en-us/library/aa920069.aspx
*/
public delegate void RILRESULTCALLBACK(uint dwCode,
IntPtr hrCmdID,
IntPtr lpData,
uint cbData,
uint dwParam);


/*
* Function definition converted from the definition
* RILNOTIFYCALLBACK from MSDN:
*
* http://msdn2.microsoft.com/en-us/library/aa922465.aspx
*/
public delegate void RILNOTIFYCALLBACK(uint dwCode,
IntPtr lpData,
uint cbData,
uint dwParam);

/*
* Class definition converted from the struct definition
* RILCELLTOWERINFO from MSDN:
*
* http://msdn2.microsoft.com/en-us/library/aa921533.aspx
*/
public class RILCELLTOWERINFO
{
public uint cbSize;
public uint dwParams;
public uint dwMobileCountryCode;
public uint dwMobileNetworkCode;
public uint dwLocationAreaCode;
public uint dwCellID;
public uint dwBaseStationID;
public uint dwBroadcastControlChannel;
public uint dwRxLevel;
public uint dwRxLevelFull;
public uint dwRxLevelSub;
public uint dwRxQuality;
public uint dwRxQualityFull;
public uint dwRxQualitySub;
public uint dwIdleTimeSlot;
public uint dwTimingAdvance;
public uint dwGPRSCellID;
public uint dwGPRSBaseStationID;
public uint dwNumBCCH;
}

// -------------------------------------------------------------------
// RIL DLL functions
// -------------------------------------------------------------------

/* Definition from: http://msdn2.microsoft.com/en-us/library/aa919106.aspx */
[DllImport("ril.dll")]
private static extern IntPtr RIL_Initialize(uint dwIndex,
RILRESULTCALLBACK pfnResult,
RILNOTIFYCALLBACK pfnNotify,
uint dwNotificationClasses,
uint dwParam,
out IntPtr lphRil);

/* Definition from: http://msdn2.microsoft.com/en-us/library/aa923065.aspx */
[DllImport("ril.dll")]
private static extern IntPtr RIL_GetCellTowerInfo(IntPtr hRil);

/* Definition from: http://msdn2.microsoft.com/en-us/library/aa919624.aspx */
[DllImport("ril.dll")]
private static extern IntPtr RIL_Deinitialize(IntPtr hRil);
}
}

3 comments:

Anonymous said...

IntPtr คืออะไรอ่ะฮับ ?

Radu Motisan said...

Polling with GetTowerCellInfo kills the battery quickly.

You need to use callback mechanisms when reading the CellID (LAC+CID) like this:
http://teksoftco.com/index.php?section=press&cat=2009#090611

Samples in C#/C++ and VB are included.

Hope this helps.

Anonymous said...

Good fill someone in on and this post helped me alot in my college assignement. Thank you seeking your information.