zoukankan      html  css  js  c++  java
  • 【WM】利用OpenNetCF切换不同无线AP连接

    最近做了一个项目,利用天宝手持机实现数据无线传输!由于是利用无线热点建网,人操作手持机活动范围较大,为了保证手持机能够一直连接到信号增益最强的无线热点,因此需要实现无线热点的自动搜索,连接,切换,锁定等功能。经过几天的努力,终于利用OpenNetCF实现了上述所有功能。(几天的辛苦没白付出,很给力!)

    无线热点的自动搜索,连接等功能,园子里已经有人写过了。

    大家可以参考:Jack Fan的文章http://www.cnblogs.com/longqi293/archive/2009/10/25/1589486.html这篇文章带我入门,给我很大帮助。感谢Jack Fan

    http://www.cnblogs.com/aga-j/archive/2010/08/09/1795375.html这篇文章很基础,也很不错!

    但是无线热点的切换真是让我头疼,它本身自带的函数没有类似于Disconnect和Reconnect这种函数(不知其他的第三方控件有没有),直接用ConnectToPreferredNetwork函数连接其他的无线热点又不能改变当前连接的无线热点。

    最初的想法是利用重启无线网卡的方式。

    1          #region 无线网卡控制DLL引用
     2         /// <summary>
     3         /// 获取设备电源状态
     4         /// </summary>
     5         /// <param name="pvDevice">设备在注册表中的键值</param>
     6         /// <param name="Flags">Power_Name</param>
     7         /// <param name="state">电源状态枚举,来自OpenNETCF.WindowsCE命名空间下的DevicePowerState</param>
     8         /// <returns></returns>
     9         [DllImport("coredll.dll")]
    10         protected static extern int GetDevicePower(string pvDevice, int Flags, ref DevicePowerState state);
    11 
    12         /// <summary>
    13         /// 设备电源管理,相信大家查阅MSDN后就不会陌生了
    14         /// </summary>
    15         /// <param name="pvDevice">设备在注册表中的键值</param>
    16         /// <param name="dwDeviceFlags">Power_Name</param>
    17         /// <param name="DeviceState">电源状态枚举,来自OpenNETCF.WindowsCE命名空间下的DevicePowerState</param>
    18         /// <returns></returns>
    19         [DllImport("coredll.dll", SetLastError = true)]
    20         private static extern int SetDevicePower(string pvDevice, int dwDeviceFlags, DevicePowerState DeviceState);
    21         /// <summary>
    22         /// 电源状态通知,用于更新顶部那个状态信息图标,参数同上
    23         /// </summary>
    24         /// <param name="device">设备在注册表中的键值</param>
    25         /// <param name="state">电源状态枚举,来自OpenNETCF.WindowsCE命名空间下的DevicePowerState</param>
    26         /// <param name="flags">Power_Name</param>
    27         /// <returns></returns>
    28         [DllImport("coredll.dll")]
    29         public static extern int DevicePowerNotify(string device, DevicePowerState state, int flags);
    30         #endregion
    31 
    32 //关闭无线网卡
    33                 DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\" + "JAGUARGSP1", DevicePowerState.Off, POWER_NAME);
    34                 SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\" + "JAGUARGSP1", POWER_NAME, DevicePowerState.Off);
    35                 Application.DoEvents();
    36 //打开无线网卡
    37                 DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\" + "JAGUARGSP1", DevicePowerState.FullOn, POWER_NAME);
    38                 SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}\\" + "JAGUARGSP1", POWER_NAME, DevicePowerState.FullOn);
    39                 Application.DoEvents();
    

    代码如有不明白,可参考Jack Fan的文章。

    无线网卡重启后,利用ConnectToPreferredNetwork函数可以连接至指定的无线热点,但是这样做的缺点是重新连接时间较长。

    作为一个业余编程的人,我看了都不是很满意。

    冥冥之中,上天没有抛弃我,赐予我无穷的力量,终于让我找到了一种比较平滑的无线热点切换方式。

    那就是利用AddPreferredNetwork这种方法,重新添加一遍无线热点。根据OpenNetCF帮助文档得到其摘要如下:

    Sets wireless settings associated with a given interface and AP, adding to, rather than replacing the preferred list of APs. This version of the method is designed for the case where *all* of the options are going to be set, where no initial configuration exists at all and where existing items in the preferred list should be maintained. After this method executes, if it is successful, the specified SSID will be at the top, highest- priority, end of the preferred list.

    我就不翻译了,也不是很复杂,其实意会更能理解其中的含义!(其实我的英文也不是很好!呵呵!)

    string WIFIAP ;//指定AP的SSID
    m_wzc.AddPreferredNetwork(m_nearbyAPs.FindBySSID(WIFIAP));
    
    m_wzc.ConnectToPreferredNetwork(WIFIAP);

    我个人认为,重新添加一个PreferredNetwork,其实新添加的AP无需进行任何改变,就是对无线网卡做了一个欺骗,把想要连接的AP调整到了最高级。

    这样再利用ConnectToPreferredNetwork进行连接,无线网卡就自动连接至指定的AP了。

    当然既然能够随意切换无线AP,那么锁定AP也就不是问题了!

    虽然只有两句话,但是这个问题困扰了我很长时间,希望这个方法对你也能有所帮助。

     

  • 相关阅读:
    maven 项目 spring mvc + jdbc 配置文件
    (BUG已修改,最优化)安卓ListView异步加载网络图片与缓存软引用图片,线程池,只加载当前屏之说明
    listview图片错位
    Android在线更新 远程安装程序
    Android中Socket大文件断点上传
    如何用JAVA生成注册序列号
    php学习随记3
    Chrome改动浏览器User Agent
    andoid电阻触摸移植
    【有奖征资源,分享有内涵】贡献你的CSDN博文和下载资源,不断更新中
  • 原文地址:https://www.cnblogs.com/MobileBo/p/1816213.html
Copyright © 2011-2022 走看看