连接WiFi工具类

 1 public class WifiConnect {
 2
 3     WifiManager wifiManager;
 4
 5     // 构造函数
 6     public WifiConnect(WifiManager wifiManager) {
 7         this.wifiManager = wifiManager;
 8     }
 9
10     // 提供一个外部接口,传入要连接的无线网
11     public boolean Connect(String SSID, String Password, int Type) {
12
13         WifiConfiguration wifiConfig = this
14                 .CreateWifiInfo(SSID, Password, Type);
15
16         WifiConfiguration tempConfig = this.IsExsits(SSID);
17         if (tempConfig != null) {
18             wifiManager.removeNetwork(tempConfig.networkId);
19         }
20
21         int netID = wifiManager.addNetwork(wifiConfig);
22         boolean flag = wifiManager.enableNetwork(netID, true);
23         return flag;
24     }
25
26     // 查看以前是否也配置过这个网络
27     private WifiConfiguration IsExsits(String SSID) {
28         List<WifiConfiguration> existingConfigs = wifiManager
29                 .getConfiguredNetworks();
30         for (WifiConfiguration existingConfig : existingConfigs) {
31             if (existingConfig.SSID.equals("\"" + SSID + "\"")) {
32                 return existingConfig;
33             }
34         }
35         return null;
36     }
37
38     // 配置WifiConfiguration
39     public WifiConfiguration CreateWifiInfo(String SSID, String Password,
40             int Type) {
41         WifiConfiguration config = new WifiConfiguration();
42         config.allowedAuthAlgorithms.clear();
43         config.allowedGroupCiphers.clear();
44         config.allowedKeyManagement.clear();
45         config.allowedPairwiseCiphers.clear();
46         config.allowedProtocols.clear();
47         config.SSID = "\"" + SSID + "\"";
48
49         WifiConfiguration tempConfig = this.IsExsits(SSID);
50         if (tempConfig != null) {
51             wifiManager.removeNetwork(tempConfig.networkId);
52         }
53
54         if (Type == 1) // WIFICIPHER_NOPASS
55         {
56             config.wepKeys[0] = "";
57             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
58             config.wepTxKeyIndex = 0;
59         }
60         if (Type == 2) // WIFICIPHER_WEP
61         {
62             config.hiddenSSID = true;
63             config.wepKeys[0] = "\"" + Password + "\"";
64             config.allowedAuthAlgorithms
65                     .set(WifiConfiguration.AuthAlgorithm.SHARED);
66             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
67             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
68             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
69             config.allowedGroupCiphers
70                     .set(WifiConfiguration.GroupCipher.WEP104);
71             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
72             config.wepTxKeyIndex = 0;
73         }
74         if (Type == 3) // WIFICIPHER_WPA
75         {
76             config.preSharedKey = "\"" + Password + "\"";
77             config.hiddenSSID = true;
78             config.allowedAuthAlgorithms
79                     .set(WifiConfiguration.AuthAlgorithm.OPEN);
80             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
81             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
82             config.allowedPairwiseCiphers
83                     .set(WifiConfiguration.PairwiseCipher.TKIP);
84             // config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
85             config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
86             config.allowedPairwiseCiphers
87                     .set(WifiConfiguration.PairwiseCipher.CCMP);
88             config.status = WifiConfiguration.Status.ENABLED;
89         }
90         return config;
91     }
92
93 }
时间: 2024-10-28 17:45:31

连接WiFi工具类的相关文章

jdbc连接用工具类

封装的是链接部分和关流部分 mysql8.0.13 public class JDBCUtils { private JDBCUtils(){} private static Connection con; static{ try { Class.forName("com.mysql.jdbc.Driver"); //2获得连接 对象 String url ="jdbc:mysql://localhost:3306/rwx?useSSl=false&serverTim

JDBC连接Oracle工具类

import java.sql.*;import java.util.ResourceBundle; /** * jdbc工具类,负责: * 1. 加载/注册数据库驱动程序 * 2. 获取数据库连接 * 3. 释放数据库资源(Connection, Statement, ResultSet) */public class JdbcUtil { private static final String DRIVER = getValue("jdbc.driver"); private st

C3P0连接池工具类实现步骤及方法

C3P0连接池的工具类 使用C3P0获得连接对象连接池有一个规范接口 javax.sal.DataSourse 接口定义了一个从连接池中获得连接的方法getConnection(); 步骤导入jar包在成员变量位置创建一个静态的ComboPooledDtatSource 对象在静态代码块中使用ComboPooledDtatSource 对象 setxxxx方法 设置数据库连接定义一个静态方法 ComboPooledDtatSource 对象中获得数据库连接 Coonection释放资源(归还):

Java 连接 SqlServer工具类

1.下载 server2008R2驱动jar包 下载jar包 http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=21599 2.代码实现 1)实体: package entity; import java.io.Serializable; import java.util.Date; public class User implements Serializable{ private int id; private Stri

android 判断网络连接的工具类

1 package com.way.util; 2 3 import android.content.Context; 4 import android.net.ConnectivityManager; 5 import android.net.NetworkInfo.State; 6 7 public class NetUtil { 8 public static final int NETWORN_NONE = 0; 9 public static final int NETWORN_WIF

python 连接redis工具类

#!/usr/bin/python # coding=utf-8 __author__ = 'shuangjiang' import redis import sys default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) class myRedis: def __init__(self,host

J2EE开发框架搭建(5) - Java项目开发常用工具类

工具类下项目中的目录位置: 1. 中文转化成拼音.首字母  ,ChineseCharToPinYin,使用这个类的时候必须要加入pinyin.jar,pinyin.jar已经放到hqhop-framework-web项目的lib目录中: 使用方式: ChineseCharToPinYin只提供了两个方法: public static String getPinYin(String src) {.....}      将汉字转换为全拼 public static String getPinYinH

Android加载网络图片的工具类

ImageView加载网络的图片 HttpUtil.java package com.eiice.httpuimagetils; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.util.Log; /** *

操作Wifi的工具类

有时候需要打开关闭wifi,或者是坚持状态什么的,这里我写了个工具类来完成各种操作,今后还会添加其他的功能.比如链接某个加密/不加密的wifi WifiAdmin.java package com.kale.socketactivity; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.net.wifi.ScanResult; import andr