- /**
- *
- * wifi管理类
- *
- *
- */
- public class WifiOperator
- {
- /**
- * wifiManager
- */
- private WifiManager wm;
- /**
- * 上下文
- */
- private Context mContext;
- /**
- * 数据库配置信息
- */
- private NetworkConfigDbHelper configDBHelper;
- /**
- * <默认构造函数>
- * @param context 上下文
- */
- public WifiOperator(Context context)
- {
- mContext = context;
- wm = (WifiManager)mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
- configDBHelper = new NetworkConfigDbHelper(context);
- }
- /**
- * 切换网络
- * @param type 网络类型(1为中心网络 2 为车载网络)
- * @return 结果码 -1-本地设置为空;0-连接成功;1-已经为当前连接
- * @see [类、类#方法、类#成员]
- */
- public int access2Wifi(String type)
- {
- // 获取本地的网络配置信息
- NetworkConfigBean setting = configDBHelper.getNetworkConfigBySettingType(type).get(type);
- if (setting == null)
- {
- // 本地设置为空
- return -1;
- }
- // 无线未打开时,开启无线
- if (!wm.isWifiEnabled() && WifiManager.WIFI_STATE_ENABLING != wm.getWifiState())
- {
- wm.setWifiEnabled(true);
- }
- // 获取本地的配置信息
- String sSSID = "\"" + setting.getSsid() + "\"";
- String sKey = "\"" + setting.getPassword() + "\"";
- int encryptionType = getKeyMgmtType(setting.getEncryptionType());
- List<WifiConfiguration> configurations = wm.getConfiguredNetworks();
- WifiConfiguration config = null;
- boolean isExisted = false;
- int networkId = -1;
- for (int i = configurations.size() - 1; i >= 0; i--)
- {
- config = configurations.get(i);
- if (config.SSID.equals(sSSID))
- {
- networkId = config.networkId;
- isExisted = true;
- break;
- }
- }
- if (!isExisted)
- {
- // 安全类型 无、WEP(128)、WPA(TKIP)、WPA2(AES)
- config = new WifiConfiguration();
- // 名称
- config.SSID = sSSID;
- config.allowedKeyManagement.set(encryptionType);
- if (encryptionType != 0)
- {
- // 密码
- config.preSharedKey = sKey;
- }
- config.hiddenSSID = false;
- config.priority = 30;
- config.status = WifiConfiguration.Status.ENABLED;
- config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
- config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
- config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
- config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
- config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
- config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
- config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
- config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);
- config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
- // 必须添加,否则无线路由无法连接
- config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
- networkId = wm.addNetwork(config);
- if (networkId != -1)
- {
- wm.saveConfiguration();
- }
- }
- else
- {
- // 获取当前的wifi连接
- WifiInfo curConnection = wm.getConnectionInfo();
- if (curConnection != null && sSSID.equals(curConnection.getSSID()))
- {
- // 已经是当前连接
- return 1;
- }
- config.allowedKeyManagement.set(encryptionType);
- if (encryptionType != 0)
- {
- // 密码
- config.preSharedKey = sKey;
- }
- wm.updateNetwork(config);
- }
- if (networkId != -1)
- {
- wm.disconnect();
- wm.enableNetwork(networkId, true);
- }
- return 0;
- }
- /**
- * 获取加密类型
- * @param type 加密类型
- * @return 加密类型
- * @see [类、类#方法、类#成员]
- */
- private int getKeyMgmtType(String type)
- {
- if (type == null)
- {
- return WifiConfiguration.KeyMgmt.NONE;
- }
- if ("WEP".equals(type))
- {
- return WifiConfiguration.KeyMgmt.IEEE8021X;
- }
- else if ("WPA-PSK".equals(type))
- {
- return WifiConfiguration.KeyMgmt.WPA_PSK;
- }
- else if ("WPA2-PSK".equals(type))
- {
- return WifiConfiguration.KeyMgmt.WPA_PSK;
- }
- return WifiConfiguration.KeyMgmt.NONE;
- }
- }
WifiManager自动连接wifi接入点
最新推荐文章于 2024-07-10 14:17:24 发布