在Windows Mobile的手机上面, RIL提供了访问Radio模块的接口, 下面以一个简单的示例说明如何在C#中通过RIL获得基站信息。 [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), // 返回调用结果的回调函数 null, 0, 0, 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; } |
|