[CF.Skills]C#中如何通过RIL获得基站信息

在Windows Mobile的手机上面, RIL提供了访问Radio模块的接口, 下面以一个简单的示例说明如何在C#中通过RIL获得基站信息.

第一步. 定义必要的数据结构和回调函数

1. 包含基站信息的RILCELLTOWERINFO类

        public class RILCELLTOWERINFO
        
{
            
public uint cbSize;
            
public uint dwParams;
            
public uint dwMobileCountryCode;//中国的MCC为460
            
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;
        }

 

2.用于异步返回RIL调用结果的回调函数RILRESULTCALLBACK

        public delegate void RILRESULTCALLBACK(uint dwCode,
                                               IntPtr hrCmdID,
                                               IntPtr lpData,
                                               
uint cbData,
                                               
uint dwParam);

 

3.在RIL主动发出notify的时候回调的提醒函数RILNOTIFYCALLBACK

 

        public delegate void RILNOTIFYCALLBACK(uint dwCode,
                                               IntPtr lpData,
                                               
uint cbData,
                                               
uint dwParam);

注意:这个提醒函数后面不会用到,但它是作为必要的Native函数的参数,在pinvoke的时候是不可缺少的 

 

第二步. 通过pinvoke引用必要的RIL Native函数 

RIL_Initialize   , RIL_GetCellTowerInfoRIL_Deinitialize

        [DllImport("ril.dll")]
        
private static extern IntPtr RIL_Initialize(uint dwIndex,
                                                    RILRESULTCALLBACK pfnResult,
                                                    RILNOTIFYCALLBACK pfnNotify,
                                                    
uint dwNotificationClasses,
                                                    
uint dwParam,
                                                    
out IntPtr lphRil);

        [DllImport(
"ril.dll")]
        
private static extern IntPtr RIL_GetCellTowerInfo(IntPtr hRil);

        [DllImport(
"ril.dll")]
        
private static extern IntPtr RIL_Deinitialize(IntPtr hRil);

 

第三步. 通过RIL_GetCellTowerInfo获取基站信息

1.初始化一个RIL的实例并返回它的Handle

            hRes = RIL_Initialize(1,                                        // RIL port 1
                                  new RILRESULTCALLBACK(rilResultCallback), // 返回调用结果的回调函数
                                  null,  00,                                      
                                  
out hRil);                                //返回RIL实例的handle

 

2.定义回调函数

        private static AutoResetEvent waithandle = new AutoResetEvent(false);

        
public static void rilResultCallback(uint dwCode,
                                             IntPtr hrCmdID,
                                             IntPtr lpData,
                                             
uint cbData,
                                             
uint dwParam)
        
{
            
//构造一个RILCELLTOWERINFO类用于存放数据
             rilCellTowerInfo = new RILCELLTOWERINFO();
            Marshal.PtrToStructure(lpData, rilCellTowerInfo);

            
//回调通知
            waithandle.Set();}

 

3.调用RIL_GetCellTowerInfo并释放当前RIL实例的handle

RIL_GetCellTowerInfo(hRil);

            
//等待回调函数返回
            waithandle.WaitOne();

            
//释放RIL handle
            RIL_Deinitialize(hRil);

 

结果与分析:

以下是在samsungi718+上的测试结果:

-rilCellTowerInfo :
  cbSize 2164262660 uint
  dwBaseStationID 706412084 uint
  dwBroadcastControlChannel 0 uint
  dwCellID 0 uint //其实这里的cellid在我机器上获取不到,确实非常遗憾
  dwGPRSBaseStationID 706412084 uint
  dwGPRSCellID 158440 uint
  dwIdleTimeSlot 33993204 uint
  dwLocationAreaCode 706412076 uint
  dwMobileCountryCode 0 uint //这个MCC中国应该是460,我这里也没有获取到
  dwMobileNetworkCode 33993204 uint
  dwNumBCCH 706411928 uint
  dwParams 0 uint
  dwRxLevel 4 uint
  dwRxLevelFull 0 uint
  dwRxLevelSub 706412004 uint
  dwRxQuality 706411908 uint
  dwRxQualityFull 158172 uint
  dwRxQualitySub 67853664 uint
  dwTimingAdvance 0 uint

需要注意的是这里的CellTowerInfo在各个机型上面的实现程度不一样,文中提到的RIL相关函数严格来说在Windows Mobile 上面都不是必须被实现的,使用时需考虑到这一点。

欢迎大家在评论中补充更多机型的测试结果。

Enjoy & Merry Xmas!

黄季冬

更新 完整的代码下载地址:http://files.cnblogs.com/fox23/CellIDSample.rar

 

 

posted on 2008-12-24 11:57  J.D Huang  阅读(9445)  评论(30编辑  收藏

统计