利用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函数连接其他的无线热点又不能改变当前连接的无线热点。

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

 

011          #region 无线网卡控制DLL引用
02 2         /// <summary>
03 3         /// 获取设备电源状态
04 4         /// </summary>
05 5         /// <param name="pvDevice">设备在注册表中的键值</param>
06 6         /// <param name="Flags">Power_Name</param>
07 7         /// <param name="state">电源状态枚举,来自OpenNETCF.WindowsCE命名空间下的DevicePowerState</param>
08 8         /// <returns></returns>
09 9         [DllImport("coredll.dll")]
1010         protected static extern int GetDevicePower(string pvDevice, int Flags, ref DevicePowerState state);
1111 
1212         /// <summary>
1313         /// 设备电源管理,相信大家查阅MSDN后就不会陌生了
1414         /// </summary>
1515         /// <param name="pvDevice">设备在注册表中的键值</param>
1616         /// <param name="dwDeviceFlags">Power_Name</param>
1717         /// <param name="DeviceState">电源状态枚举,来自OpenNETCF.WindowsCE命名空间下的DevicePowerState</param>
1818         /// <returns></returns>
1919         [DllImport("coredll.dll", SetLastError = true)]
2020         private static extern int SetDevicePower(string pvDevice, int dwDeviceFlags, DevicePowerState DeviceState);
2121         /// <summary>
2222         /// 电源状态通知,用于更新顶部那个状态信息图标,参数同上
2323         /// </summary>
2424         /// <param name="device">设备在注册表中的键值</param>
2525         /// <param name="state">电源状态枚举,来自OpenNETCF.WindowsCE命名空间下的DevicePowerState</param>
2626         /// <param name="flags">Power_Name</param>
2727         /// <returns></returns>
2828         [DllImport("coredll.dll")]
2929         public static extern int DevicePowerNotify(string device, DevicePowerState state, int flags);
3030         #endregion
3131 
3232 //关闭无线网卡
3333                 DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}//" + "JAGUARGSP1", DevicePowerState.Off, POWER_NAME);
3434                 SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}//" + "JAGUARGSP1", POWER_NAME, DevicePowerState.Off);
3535                 Application.DoEvents();
3636 //打开无线网卡
3737                 DevicePowerNotify("{98C5250D-C29A-4985-AE5F-AFE5367E5006}//" + "JAGUARGSP1", DevicePowerState.FullOn, POWER_NAME);
3838                 SetDevicePower("{98C5250D-C29A-4985-AE5F-AFE5367E5006}//" + "JAGUARGSP1", POWER_NAME, DevicePowerState.FullOn);
3939                 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.

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

 

 

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

 

 

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

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

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值