wifi热点创建和自动连接
1.wifi热点的创建/*** 创建wifi热点* @param ssid 热点信息* @param passwd 密码* @author wanghongbin*/public void startWifiAp(Context context, String ssid, String passwd) {//关闭wificloseWifi();//关闭热点closeWifiAp();//激活热点invokeWifiAp(ssid, passwd);}/*** 激活wifi热点* @param ssid* @param passwd* @author wanghongbin*/private void invokeWifiAp(String ssid, String passwd) {try {Method method1 = mWifiManager.getClass().getMethod("setWifiApEnabled",WifiConfiguration.class, boolean.class);WifiConfiguration netConfig = new WifiConfiguration();netConfig.SSID = ssid;netConfig.preSharedKey = passwd ;//启动热点boolean isSuccess = (Boolean)method1.invoke(mWifiManager, netConfig, true);Log.i(TAG, "whb end startWifiAp isSuccess="+isSuccess);} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();Log.i(TAG, "whb end startWifiAp IllegalArgumentException");} catch (IllegalAcces***ception e) {// TODO Auto-generated catch blocke.printStackTrace();Log.i(TAG, "whb end startWifiAp IllegalAcces***ception");} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();
// onNotifyWifiNotSurpport();Log.i(TAG, "whb end startWifiAp onNotifyWifiNotSurpport");} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();Log.i(TAG, "whb end startWifiAp SecurityException");} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();onNotifyWifiNotSurpport();Log.i(TAG, "whb end startWifiAp NoSuchMethodException");}}// 关闭WIFIpublic void closeWifi() {if (mWifiManager.isWifiEnabled()) {mWifiManager.setWifiEnabled(false);}} //作热点之前先关闭wifi热点服务public void closeWifiAp( ) {if (isWifiApEnabled()) {try {Method method = mWifiManager.getClass().getMethod("getWifiApConfiguration");method.setAccessible(true);WifiConfiguration config = (WifiConfiguration) method.invoke(mWifiManager);Method method2 = mWifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);method2.invoke(mWifiManager, config, false);} catch (NoSuchMethodException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAcces***ception e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}2.连接热点/*** 添加一个网络节点并连接* @param ssid wifi节点名称* @param passwd 密码* @param type : TYPE_WEP TYPE_WPA*/public void addNetwork(Context context, String ssid, String passwd, int type) {closeWifiAp();openWifi();mConfig = createWifiInfo(ssid, passwd, type);SSID = ssid;connectToWifiWithConfiguration(mConfig);}public static final int WAIT_FOR_SCAN_RESULT = 10 * 1000; //10 secondspublic static final int WIFI_SCAN_TIMEOUT = 20 * 1000;/*** 连接指定热点* @param config* @return true : 调用函数成功,具体网络状态还得检测* @author wanghongbin 这个函数害死人阿,网上看了半天也没人说,最后是看的源代码里的WifiConnectionTest.java才明白需要等待,步步等待,步步惊心*/public boolean connectToWifiWithConfiguration(WifiConfiguration config) {String ssid = config.SSID;config.SSID = "\""+ssid+"\"";//If Wifi is not enabled, enable itif (!mWifiManager.isWifiEnabled()) {Log.v(TAG, "Wifi is not enabled, enable it");mWifiManager.setWifiEnabled(true);}List<ScanResult> netList = mWifiManager.getScanResults();if (netList == null) {Log.v(TAG, "scan results are null");// if no scan results are available, start active scanmWifiManager.startScan();boolean mScanResultIsAvailable = false;long startTime = System.currentTimeMillis();while (!mScanResultIsAvailable) {if ((System.currentTimeMillis() - startTime) > WIFI_SCAN_TIMEOUT) {return false;}// wait for the scan results to be availablesynchronized (this) {// wait for the scan result to be availabletry {this.wait(WAIT_FOR_SCAN_RESULT);} catch (InterruptedException e) {e.printStackTrace();}if ((mWifiManager.getScanResults() == null) ||(mWifiManager.getScanResults().size() <= 0)) {continue;}mScanResultIsAvailable = true;}}}netList = mWifiManager.getScanResults();for (int i = 0; i < netList.size(); i++) {ScanResult sr= netList.get(i);if (sr.SSID.equals(ssid)) {Log.v(TAG, "found " + ssid + " in the scan result list");int networkId = mWifiManager.addNetwork(config);// Connect to network by disabling others.mWifiManager.enableNetwork(networkId, true);mWifiManager.saveConfiguration();mWifiManager.reconnect();break;}}List<WifiConfiguration> netConfList = mWifiManager.getConfiguredNetworks();if (netConfList.size() <= 0) {Log.v(TAG, ssid + " is not available");return false;}return true;}public static final int TYPE_NO_PASSWD = 0x11;public static final int TYPE_WEP = 0x12;public static final int TYPE_WPA = 0x13;/*** 连接信息生成配置对象* @param SSID* @param password* @param type* @return* @author wanghongbin*/public WifiConfiguration createWifiInfo(String SSID, String password, int type) {Log.v(TAG, "whb SSID =" + SSID + "## Password =" + password + "## Type = " + type);WifiConfiguration config = new WifiConfiguration();config.SSID = SSID; clearAll(SSID);// 分为三种情况:1没有密码2用wep加密3用wpa加密if (type == TYPE_NO_PASSWD) {// WIFICIPHER_NOPASSconfig.hiddenSSID = false;config.status = WifiConfiguration.Status.ENABLED;config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);config.preSharedKey = null;} else if (type == TYPE_WEP) { // WIFICIPHER_WEPconfig.hiddenSSID = true;config.wepKeys[0] = "\"" + password + "\"";config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);config.wepTxKeyIndex = 0;} else if (type == TYPE_WPA) { // WIFICIPHER_WPAconfig.preSharedKey = "\"" + password + "\"";config.hiddenSSID = false;config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);config.priority = 10000;config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);config.status = WifiConfiguration.Status.ENABLED;}return config;}3.很多之前已经连接上的WIFI,配置信息会自动保存,当你关闭---->打开WIFI时就会自动连接,但有时我们并不希望这样处理,解决方法为在打开WIFI之前将以前所保存的信息清除,代码为
if(isWifiConnect()){WifiInfo info = mWifiManager.getConnectionInfo();mWifiManager.removeNetwork(info.getNetworkId());mWifiManager.saveConfiguration();}或者/*** 移除所有同名节点* @param SSID*/private void clearAll(String SSID) {List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks();//按照networkId从大到小排序Collections.sort(existingConfigs, new ComparatorConfig());for (WifiConfiguration existingConfig : existingConfigs) {LogHelper.i(TAG,"existingConfig.SSID="+existingConfig.SSID+",netID = "+ existingConfig.networkId);if (existingConfig.SSID.equals("\""+SSID+"\"") /*&& existingConfig.preSharedKey.equals("\"" + password + "\"")*/) {mWifiManager.disableNetwork(existingConfig.networkId);mWifiManager.removeNetwork(existingConfig.networkId);}}mWifiManager.saveConfiguration();}
wifi热点创建和自动连接相关推荐
- Wifi热点创建工具配合电脑与安卓手机端实现秒传文件的方法
有什么方法能在有Wifi网或无Wifi网的环境,但没有数据线如何用笔记本或带无线网卡的台式电脑更方便的给安卓手机传送文件呢?我想到之前电脑之间都是拿飞鸽传书传文件,有没有安卓版的?一找,还真有,哈~~ ...
- Ubuntu 创建android手机能够连接的Wifi热点
已在Ubuntu15.04上验证成功: 1.插上网线,开启有线网络. 2.安装hostapd,Ctrl+Alt+T打开终端输入命令行:sudo apt-get install hostapd 3.关闭 ...
- [Android源码]Android源码之高仿飞鸽传书WIFI热点搜索与创建(一)
(本文详情来源:android源码 http://www.eoeandroid.com/thread-296427-1-1.html 转载请注明出处!) [Android源码分享]飞鸽传书的An ...
- linux 假热点制作,创建假的wifi热点
本文教你怎么创建假的wifi热点,然后抓取连接到这个wifi用户的敏感数据.我们还会给周围的无线路由器发送未认证的包,使这些路由器不能正常,强迫用户连接(或自动连接)我们创建的假wifi热点. 这种攻 ...
- 利用Mac创建一个 IPv6 WIFI 热点
标签: iOSIPv6Create NAT64 NetWork创建NAT64网络 2016-05-20 10:58 14156人阅读 评论(11) 收藏 举报 分类: 开发技巧(8) 版权声明:本 ...
- linux源码共享,Android之高仿飞鸽传书热点创建与共享源码
这两天,无意中看到飞鸽传书这个小东东,然后又突然对他的wifi热点创建与共享比较感兴趣,于是乎把他的APK给反编译了,很蛋疼的还原了一下他的这一小模块代码,感觉效果差不多,所以跟大家分享一下,下面我们 ...
- win10开启wifi热点
若电脑可以连接Internet网络,则可以将有线网络共享成为Wifi热点,分享给手机.平板等移动设备使用.若是笔记本电脑可以直接使用的自带的无线网卡进行分享,若是台式电脑则可以安装USB无线网卡来实现 ...
- 三星s7共享网络给linux,三星Galaxy S7WiFi状态下可以共享wifi热点吗【详解】
目前,越来越多的智能手机都具有wifi热点的功能,不仅方便了不少网友之间的网络流量共享,而且一个wifi热点还可以共享到多个设备.而三星最新款手机三星Galaxy S7与S7 Edge在wifi热点功 ...
- 一个WIFI热点的脚本思路,顺记shell知识
本文主要学习ap-hotspot和create_ap脚本,总结了一下WIFI热点创建脚本的思路,另外记录一些shell学习知识点. 一.Linux环境创建WIFI热点脚本思路 1.参数处理 一个好的脚 ...
最新文章
- 金邦黑金刚4G内存 VS Vista系统
- 一张图解释什么是遗传算法_一张图告诉你什么叫真正的满配m416,吃鸡玩家看懵了...
- SharePoint 2013 REST 服务使用简介
- IEEE作者中心!解决你的选刊,模板,选题,写做的一系列困难!!!拿走不谢!!!
- OpenCASCADE绘制测试线束:拓扑命令之历史命令
- 问题 D: 回文数(n进制加法,模拟)
- python找不到csv文件_Python如何读取csv文件
- 树莓派linux共享目录,树莓派局域网文件共享
- 在项目中配置PageHelper插件时遇到类型转换异常
- [转]vue解决刷新页面vuex数据、params参数消失的问题
- 传说中的世界500强面试题-情商
- filetransferdmg魅族下载_filetransfer.dmg下载
- 压缩文件的格式zip和rar有什么区别?
- outlook配置文件添加服务器,Microsoft Outlook卡在加载配置文件?这里如何解决它
- html怎么打五角星符号怎么打,五角星符号怎么打,五角星符号大全
- 电脑怎么写入便签并同步到手机版便签上?
- 思科网络安全 第四章考试答案
- CK-GW208-EC与汇川PLC配置走EtherCAT通讯指南
- equals和==和hashcode的恩怨情仇
- 文件的压缩打包及解压解包