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

          在Windows Mobile的手機(jī)上面, RIL提供了訪問(wèn)Radio模塊的接口, 下面以一個(gè)簡(jiǎn)單的示例說(shuō)明如何在C#中通過(guò)RIL獲得基站信息。
          需要注意的是,下面的代碼可能無(wú)法在模擬器上面運(yùn)行,因?yàn)槿鄙俦匾念?lèi)庫(kù),在真機(jī)上沒(méi)啥問(wèn)題。
          第一步. 定義必要的數(shù)據(jù)結(jié)構(gòu)和回調(diào)函數(shù)
          1. 包含基站信息的RILCELLTOWERINFO類(lèi)
                  public class RILCELLTOWERINFO
                  {
                      public uint cbSize;
                      public uint dwParams;
                      public uint dwMobileCountryCode;//中國(guó)的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í)候是不可缺少的 
           
          第二步. 通過(guò)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);
           
          第三步. 通過(guò)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類(lèi)用于存放數(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中國(guó)應(yīng)該是460,我這里也沒(méi)有獲取到
            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)格來(lái)說(shuō)在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類(lèi)用于存放數(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;//中國(guó)的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;
              }


          ---------------------------------------------------------
          專(zhuān)注移動(dòng)開(kāi)發(fā)

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2011-06-19 18:22 TiGERTiAN 閱讀(2312) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Windows Mobile
          主站蜘蛛池模板: 界首市| 慈溪市| 大渡口区| 黄山市| 贵南县| 山西省| 襄汾县| 泸州市| 富宁县| 金平| 淄博市| 台东市| 青州市| 油尖旺区| 济阳县| 新建县| 儋州市| 莱西市| 梅河口市| 彭州市| 江都市| 乐陵市| 榕江县| 邵阳县| 海南省| 沙洋县| 兰溪市| 布拖县| 平和县| 娱乐| 类乌齐县| 栾川县| 车险| 苍溪县| 平安县| 静安区| 资中县| 疏附县| 霞浦县| 海盐县| 五常市|