随笔-348  评论-598  文章-0  trackbacks-0

在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_GetCellTowerInfo,RIL_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,  0, 0,                                      
                                  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 上面都不是必须被实现的,使用时需考虑到这一点。

实现代码:
    public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

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


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

        [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);

        
private static AutoResetEvent waithandle = new AutoResetEvent(false);
        
private static RILCELLTOWERINFO rilCellTowerInfo;
        
private IntPtr hRes;
        
private IntPtr hRil;
        
public static void rilResultCallback(uint dwCode,
                                             IntPtr hrCmdID,
                                             IntPtr lpData,
                                             
uint cbData,
                                             
uint dwParam)
        {
            
//构造一个RILCELLTOWERINFO类用于存放数据
            rilCellTowerInfo = new RILCELLTOWERINFO();
            Marshal.PtrToStructure(lpData, rilCellTowerInfo);
            
//回调通知
            waithandle.Set();
        }
 
 

        
private void button1_Click(object sender, EventArgs e)
        {
            hRes 
= RIL_Initialize(1,                                        // RIL port 1
                                 new RILRESULTCALLBACK(rilResultCallback), // 返回调用结果的回调函数
                                 null00,
                                 
out hRil);    
            
//返回RIL实例的handle
            RIL_GetCellTowerInfo(hRil);
            
//等待回调函数返回
            waithandle.WaitOne();
            
//释放RIL handle
            RIL_Deinitialize(hRil);

        }
    }

    
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;
    }


---------------------------------------------------------
专注移动开发

Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
posted on 2011-06-19 18:22 TiGERTiAN 阅读(2286) 评论(0)  编辑  收藏 所属分类: Windows Mobile

只有注册用户登录后才能发表评论。


网站导航: