隨筆-348  評(píng)論-598  文章-0  trackbacks-0

          在Windows Mobile的手機(jī)上面, RIL提供了訪問Radio模塊的接口, 下面以一個(gè)簡(jiǎn)單的示例說明如何在C#中通過RIL獲得基站信息。
          需要注意的是,下面的代碼可能無法在模擬器上面運(yùn)行,因?yàn)槿鄙俦匾念悗欤谡鏅C(jī)上沒啥問題。
          第一步. 定義必要的數(shù)據(jù)結(jié)構(gòu)和回調(diào)函數(shù)
          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調(diào)用結(jié)果的回調(diào)函數(shù)RILRESULTCALLBACK
                  public delegate void RILRESULTCALLBACK(uint dwCode,
                                                         IntPtr hrCmdID,
                                                         IntPtr lpData,
                                                         uint cbData,
                                                         uint dwParam);
           
          3.在RIL主動(dòng)發(fā)出notify的時(shí)候回調(diào)的提醒函數(shù)RILNOTIFYCALLBACK
           
                  public delegate void RILNOTIFYCALLBACK(uint dwCode,
                                                         IntPtr lpData,
                                                         uint cbData,
                                                         uint dwParam);
          注意:這個(gè)提醒函數(shù)后面不會(huì)用到,但它是作為必要的Native函數(shù)的參數(shù),在pinvoke的時(shí)候是不可缺少的 
           
          第二步. 通過pinvoke引用必要的RIL Native函數(shù) 
          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.初始化一個(gè)RIL的實(shí)例并返回它的Handle
                      hRes = RIL_Initialize(1,                                        // RIL port 1
                                            new RILRESULTCALLBACK(rilResultCallback), // 返回調(diào)用結(jié)果的回調(diào)函數(shù)
                                            null,  0, 0,                                      
                                            out hRil);                                //返回RIL實(shí)例的handle
           
          2.定義回調(diào)函數(shù)
                  private static AutoResetEvent waithandle = new AutoResetEvent(false);
                  public static void rilResultCallback(uint dwCode,
                                                       IntPtr hrCmdID,
                                                       IntPtr lpData,
                                                       uint cbData,
                                                       uint dwParam)
                  {
                      //構(gòu)造一個(gè)RILCELLTOWERINFO類用于存放數(shù)據(jù)
                       rilCellTowerInfo = new RILCELLTOWERINFO();
                      Marshal.PtrToStructure(lpData, rilCellTowerInfo);
                      //回調(diào)通知
                      waithandle.Set();}
           
          3.調(diào)用RIL_GetCellTowerInfo并釋放當(dāng)前RIL實(shí)例的handle
          RIL_GetCellTowerInfo(hRil);
                      //等待回調(diào)函數(shù)返回
                      waithandle.WaitOne();
                      //釋放RIL handle
                      RIL_Deinitialize(hRil);
           
          結(jié)果與分析:
          以下是在samsungi718+上的測(cè)試結(jié)果:
          -rilCellTowerInfo :
            cbSize 2164262660 uint
            dwBaseStationID 706412084 uint
            dwBroadcastControlChannel 0 uint
            dwCellID 0 uint //其實(shí)這里的cellid在我機(jī)器上獲取不到,確實(shí)非常遺憾
            dwGPRSBaseStationID 706412084 uint
            dwGPRSCellID 158440 uint
            dwIdleTimeSlot 33993204 uint
            dwLocationAreaCode 706412076 uint
            dwMobileCountryCode 0 uint //這個(gè)MCC中國應(yīng)該是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在各個(gè)機(jī)型上面的實(shí)現(xiàn)程度不一樣,文中提到的RIL相關(guān)函數(shù)嚴(yán)格來說在Windows Mobile 上面都不是必須被實(shí)現(xiàn)的,使用時(shí)需考慮到這一點(diǎn)。

          實(shí)現(xiàn)代碼:
              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)
                  {
                      
          //構(gòu)造一個(gè)RILCELLTOWERINFO類用于存放數(shù)據(jù)
                      rilCellTowerInfo = new RILCELLTOWERINFO();
                      Marshal.PtrToStructure(lpData, rilCellTowerInfo);
                      
          //回調(diào)通知
                      waithandle.Set();
                  }
           
           

                  
          private void button1_Click(object sender, EventArgs e)
                  {
                      hRes 
          = RIL_Initialize(1,                                        // RIL port 1
                                           new RILRESULTCALLBACK(rilResultCallback), // 返回調(diào)用結(jié)果的回調(diào)函數(shù)
                                           null00,
                                           
          out hRil);    
                      
          //返回RIL實(shí)例的handle
                      RIL_GetCellTowerInfo(hRil);
                      
          //等待回調(diào)函數(shù)返回
                      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;
              }


          ---------------------------------------------------------
          專注移動(dòng)開發(fā)

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2011-06-19 18:22 TiGERTiAN 閱讀(2312) 評(píng)論(0)  編輯  收藏 所屬分類: Windows Mobile
          主站蜘蛛池模板: 东丽区| 邵武市| 吉林省| 航空| 阜新| 咸宁市| 广丰县| 卢湾区| 垣曲县| 沐川县| 仲巴县| 普陀区| 山东省| 株洲市| 赤水市| 长顺县| 手游| 改则县| 娄底市| 肇源县| 孙吴县| 高淳县| 拉萨市| 高唐县| 东乌珠穆沁旗| 海口市| 随州市| 历史| 丹巴县| 勃利县| 奉节县| 秦皇岛市| 三台县| 宝应县| 新泰市| 轮台县| 林州市| 弥勒县| 共和县| 镇江市| 宁津县|