[CF.Skills]C#中如何通過RIL獲得基站資訊

來源:互聯網
上載者:User

在Windows Mobile的手機上面, RIL提供了訪問Radio模組的介面, 下面以一個簡單的樣本說明如何在C#中通過RIL獲得基站資訊.

第一步. 定義必要的資料結構和回呼函數

1. 包含基站資訊的RILCELLTOWERINFO類

        public class RILCELLTOWERINFO
{
this.style.display='none'; document.getElementById('Codehighlighter1_46_818_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_46_818_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_46_818_Closed_Text').style.display='inline';
}" id="Codehighlighter1_46_818_Open_Image">{
this.style.display='none'; document.getElementById('Codehighlighter1_46_818_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_46_818_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_46_818_Open_Text').style.display='inline';
}" id="Codehighlighter1_46_818_Closed_Image" style="display: none">        {
            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)
{
this.style.display='none'; document.getElementById('Codehighlighter1_383_871_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_383_871_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_383_871_Closed_Text').style.display='inline';
}" id="Codehighlighter1_383_871_Open_Image">{
this.style.display='none'; document.getElementById('Codehighlighter1_383_871_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_383_871_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_383_871_Open_Text').style.display='inline';
}" id="Codehighlighter1_383_871_Closed_Image" style="display: none">        {
            //構造一個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

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.