[CF.Skills]C#中如何通过RIL获得基站信息
在Windows Mobile的手机上面, RIL提供了访问Radio模块的接口, 下面以一个简单的示例说明如何在C#中通过RIL获得基站信息.
第一步. 定义必要的数据结构和回调函数
1. 包含基站信息的RILCELLTOWERINFO类
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
publicclassRILCELLTOWERINFO


{
publicuintcbSize;
publicuintdwParams;
publicuintdwMobileCountryCode;//中国的MCC为460
publicuintdwMobileNetworkCode;
publicuintdwLocationAreaCode;
publicuintdwCellID;
publicuintdwBaseStationID;
publicuintdwBroadcastControlChannel;
publicuintdwRxLevel;
publicuintdwRxLevelFull;
publicuintdwRxLevelSub;
publicuintdwRxQuality;
publicuintdwRxQualityFull;
publicuintdwRxQualitySub;
publicuintdwIdleTimeSlot;
publicuintdwTimingAdvance;
publicuintdwGPRSCellID;
publicuintdwGPRSBaseStationID;
publicuintdwNumBCCH;
}
2.用于异步返回RIL调用结果的回调函数RILRESULTCALLBACK
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
publicdelegatevoidRILRESULTCALLBACK(uintdwCode,
IntPtrhrCmdID,
IntPtrlpData,
uintcbData,
uintdwParam);
3.在RIL主动发出notify的时候回调的提醒函数RILNOTIFYCALLBACK
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
publicdelegatevoidRILNOTIFYCALLBACK(uintdwCode,
IntPtrlpData,
uintcbData,
uintdwParam);
注意:这个提醒函数后面不会用到,但它是作为必要的Native函数的参数,在pinvoke的时候是不可缺少的
第二步. 通过pinvoke引用必要的RIL Native函数
RIL_Initialize, RIL_GetCellTowerInfo,RIL_Deinitialize
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
[DllImport("ril.dll")]
privatestaticexternIntPtrRIL_Initialize(uintdwIndex,
RILRESULTCALLBACKpfnResult,
RILNOTIFYCALLBACKpfnNotify,
uintdwNotificationClasses,
uintdwParam,
outIntPtrlphRil);

[DllImport("ril.dll")]
privatestaticexternIntPtrRIL_GetCellTowerInfo(IntPtrhRil);

[DllImport("ril.dll")]
privatestaticexternIntPtrRIL_Deinitialize(IntPtrhRil);
第三步. 通过RIL_GetCellTowerInfo获取基站信息
1.初始化一个RIL的实例并返回它的Handle
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
hRes=RIL_Initialize(1,//RILport1
newRILRESULTCALLBACK(rilResultCallback),//返回调用结果的回调函数
null,0,0,
outhRil);//返回RIL实例的handle
2.定义回调函数
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
privatestaticAutoResetEventwaithandle=newAutoResetEvent(false);

publicstaticvoidrilResultCallback(uintdwCode,
IntPtrhrCmdID,
IntPtrlpData,
uintcbData,
uintdwParam)


{
//构造一个RILCELLTOWERINFO类用于存放数据
rilCellTowerInfo=newRILCELLTOWERINFO();
Marshal.PtrToStructure(lpData,rilCellTowerInfo);

//回调通知
waithandle.Set();}
3.调用RIL_GetCellTowerInfo并释放当前RIL实例的handle
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
RIL_GetCellTowerInfo(hRil);

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

//释放RILhandle
RIL_Deinitialize(hRil);
结果与分析:
以下是在samsungi718+上的测试结果:
-rilCellTowerInfo:
cbSize2164262660uint
dwBaseStationID706412084uint
dwBroadcastControlChannel0uint
dwCellID0uint //其实这里的cellid在我机器上获取不到,确实非常遗憾
dwGPRSBaseStationID706412084uint
dwGPRSCellID158440uint
dwIdleTimeSlot33993204uint
dwLocationAreaCode706412076uint
dwMobileCountryCode0uint //这个MCC中国应该是460,我这里也没有获取到
dwMobileNetworkCode33993204uint
dwNumBCCH706411928uint
dwParams0uint
dwRxLevel4uint
dwRxLevelFull0uint
dwRxLevelSub706412004uint
dwRxQuality706411908uint
dwRxQualityFull158172uint
dwRxQualitySub67853664uint
dwTimingAdvance0uint
需要注意的是这里的CellTowerInfo在各个机型上面的实现程度不一样,文中提到的RIL相关函数严格来说在Windows Mobile 上面都不是必须被实现的,使用时需考虑到这一点。
欢迎大家在评论中补充更多机型的测试结果。
Enjoy & Merry Xmas!
黄季冬
更新 完整的代码下载地址:http://files.cnblogs.com/fox23/CellIDSample.rar